| 1 | # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| 2 | # contributor license agreements. See the NOTICE file distributed with | ||
| 3 | # this work for additional information regarding copyright ownership. | ||
| 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| 5 | # (the "License"); you may not use this file except in compliance with | ||
| 6 | # the License. You may obtain a copy of the License at | ||
| 7 | # | ||
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | # | ||
| 10 | # Unless required by applicable law or agreed to in writing, software | ||
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | # See the License for the specific language governing permissions and | ||
| 14 | # limitations under the License. | ||
| 15 | # | ||
| 16 | package Apache::TestCommonPost; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings FATAL => 'all'; | ||
| 20 | |||
| 21 | use constant POST_HUGE => $ENV{APACHE_TEST_POST_HUGE} || 0; | ||
| 22 | |||
| 23 | use Apache::TestRequest (); | ||
| 24 | use Apache::TestUtil qw(t_cmp t_debug); | ||
| 25 | use Apache::Test qw(sok); | ||
| 26 | |||
| 27 | BEGIN { | ||
| 28 | my $use_inline = 0; | ||
| 29 | |||
| 30 | eval { | ||
| 31 | #if Inline.pm and libcurl are available | ||
| 32 | #we can make this test about 3x faster, | ||
| 33 | #after the inlined code is compiled that is. | ||
| 34 | require Inline; | ||
| 35 | Inline->import(C => 'DATA', LIBS => ['-lcurl'], | ||
| 36 | #CLEAN_AFTER_BUILD => 0, | ||
| 37 | PREFIX => 'aptest_post_'); | ||
| 38 | *request_init = \&curl_init; | ||
| 39 | *request_do = \&curl_do; | ||
| 40 | $use_inline = 1; | ||
| 41 | } if POST_HUGE; | ||
| 42 | |||
| 43 | if (POST_HUGE) { | ||
| 44 | if ($@) { | ||
| 45 | t_debug "tests will run faster with Inline and curl installed"; | ||
| 46 | print $@; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | t_debug "using Inline and curl client"; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | unless ($use_inline) { | ||
| 54 | t_debug "using LWP client"; | ||
| 55 | #fallback to lwp | ||
| 56 | *request_init = \&lwp_init; | ||
| 57 | *request_do = \&lwp_do; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | sub lwp_init { | ||
| 62 | use vars qw($UA $Location); | ||
| 63 | $UA = Apache::TestRequest::user_agent(); | ||
| 64 | $Location = shift; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub lwp_do { | ||
| 68 | my $length = shift; | ||
| 69 | |||
| 70 | my $request = HTTP::Request->new(POST => $Location); | ||
| 71 | $request->header('Content-length' => $length); | ||
| 72 | |||
| 73 | if (LWP->VERSION >= 5.800) { | ||
| 74 | $request->content_ref(\('a' x $length)); | ||
| 75 | } else { | ||
| 76 | # before LWP 5.800 there was no way to tell HTTP::Message not | ||
| 77 | # to copy the string, there is a settable content_ref since | ||
| 78 | # 5.800 | ||
| 79 | use constant BUF_SIZE => 8192; | ||
| 80 | |||
| 81 | my $remain = $length; | ||
| 82 | my $content = sub { | ||
| 83 | my $bytes = $remain < BUF_SIZE ? $remain : BUF_SIZE; | ||
| 84 | my $buf = 'a' x $bytes; | ||
| 85 | $remain -= $bytes; | ||
| 86 | $buf; | ||
| 87 | }; | ||
| 88 | |||
| 89 | $request->content($content); | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | my $response = $UA->request($request); | ||
| 95 | |||
| 96 | Apache::TestRequest::lwp_trace($response); | ||
| 97 | |||
| 98 | return $response->content; | ||
| 99 | } | ||
| 100 | |||
| 101 | my @run_post_test_small_sizes = | ||
| 102 | #1k..9k, 10k..50k, 100k | ||
| 103 | (1..9, 10..50, 100); | ||
| 104 | |||
| 105 | my @run_post_test_sizes = @run_post_test_small_sizes; | ||
| 106 | |||
| 107 | if (POST_HUGE) { | ||
| 108 | push @run_post_test_sizes, | ||
| 109 | #300k, 500k, 2Mb, 4Mb, 6Mb, 10Mb | ||
| 110 | 300, 500, 2000, 4000, 6000, 10_000; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub Apache::TestCommon::run_post_test_sizes { @run_post_test_sizes } | ||
| 114 | |||
| 115 | sub Apache::TestCommon::run_post_test { | ||
| 116 | my $module = shift; | ||
| 117 | my $sizes = shift || \@run_post_test_sizes; | ||
| 118 | |||
| 119 | my $location = Apache::TestRequest::resolve_url("/$module"); | ||
| 120 | |||
| 121 | request_init($location); | ||
| 122 | |||
| 123 | for my $size (@$sizes) { | ||
| 124 | sok { | ||
| 125 | my $length = ($size * 1024); | ||
| 126 | |||
| 127 | my $str = request_do($length); | ||
| 128 | chomp $str; | ||
| 129 | |||
| 130 | t_cmp($length, $str, "length posted"); | ||
| 131 | }; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 | __DATA__ |