| 1 | package Eve::PsgiStub; | ||
| 2 | |||
| 3 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use Eve::RegistryStub; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | use Eve::HttpRequest::Psgi; | ||
| 9 | use Eve::Registry; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | B<Eve::PsgiStub> - a stub class to easily create mock versions of HTTP requests. | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Eve::PsgiStub; | ||
| 18 | |||
| 19 | my $request = Eve::PsgiStub->get_request( | ||
| 20 | 'method' => $method_string, | ||
| 21 | 'uri' => $uri_string, | ||
| 22 | 'host' => $domain_strin, | ||
| 23 | 'query' => $query_string, | ||
| 24 | 'cookie' => $cookie_string); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | B<Eve::PsgiStub> is a helper abstract factory class that generates | ||
| 29 | HTTP requests for making tests easier. | ||
| 30 | |||
| 31 | =head1 METHODS | ||
| 32 | |||
| 33 | =head2 B<get_request()> | ||
| 34 | |||
| 35 | Returns a B<Eve::HttpRequest::Psgi> object based on arguments. All | ||
| 36 | arguments are optional. | ||
| 37 | |||
| 38 | =head3 Arguments | ||
| 39 | |||
| 40 | =over 4 | ||
| 41 | |||
| 42 | =item C<uri> | ||
| 43 | |||
| 44 | a request URI part string, defaults to C</>, | ||
| 45 | |||
| 46 | =item C<host> | ||
| 47 | |||
| 48 | a request host string, defaults to C<example.localhost>, | ||
| 49 | |||
| 50 | =item C<query> | ||
| 51 | |||
| 52 | a request URI query string part, defaults to an empty string, | ||
| 53 | |||
| 54 | =item C<method> | ||
| 55 | |||
| 56 | a request method string, defaults to C<GET> | ||
| 57 | |||
| 58 | =item C<body> | ||
| 59 | |||
| 60 | a request body, defaults to an empty string | ||
| 61 | |||
| 62 | =item C<cookie> | ||
| 63 | |||
| 64 | a request C<Set-Cookie> string, defaults to an empty string, | ||
| 65 | |||
| 66 | =item C<content_type> | ||
| 67 | |||
| 68 | a request C<content-type> string, defaults to an empty string. | ||
| 69 | |||
| 70 | =back | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub get_request { | ||
| 75 | my ($self, %arg_hash) = @_; | ||
| 76 | Eve::Support::arguments( | ||
| 77 | \%arg_hash, | ||
| 78 | my $uri = '/', | ||
| 79 | my $host = 'example.localhost', | ||
| 80 | my $query = '', | ||
| 81 | my $method = 'GET', | ||
| 82 | my $body = '', | ||
| 83 | my $cookie = '', | ||
| 84 | my $content_type = \undef); | ||
| 85 | |||
| 86 | my $env_hash = { | ||
| 87 | 'psgi.multiprocess' => 1, | ||
| 88 | 'SCRIPT_NAME' => '', | ||
| 89 | 'PATH_INFO' => $uri, | ||
| 90 | 'HTTP_ACCEPT' => | ||
| 91 | 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | ||
| 92 | 'REQUEST_METHOD' => $method, | ||
| 93 | 'psgi.multithread' => '', | ||
| 94 | 'SCRIPT_FILENAME' => '/var/www/some/path', | ||
| 95 | 'SERVER_SOFTWARE' => 'Apache/2.2.20 (Ubuntu)', | ||
| 96 | 'HTTP_USER_AGENT' => 'Mozilla/5.0 Gecko/20100101 Firefox/9.0.1', | ||
| 97 | 'REMOTE_PORT' => '53427', | ||
| 98 | 'QUERY_STRING' => $query, | ||
| 99 | 'SERVER_SIGNATURE' => '<address>Apache/2.2.20 Port 80</address>', | ||
| 100 | 'HTTP_CACHE_CONTROL' => 'max-age=0', | ||
| 101 | 'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.7,ru;q=0.3', | ||
| 102 | 'HTTP_X_REAL_IP' => '127.0.0.1', | ||
| 103 | 'psgi.streaming' => 1, | ||
| 104 | 'MOD_PERL_API_VERSION' => 2, | ||
| 105 | 'PATH' => '/usr/local/bin:/usr/bin:/bin', | ||
| 106 | 'GATEWAY_INTERFACE' => 'CGI/1.1', | ||
| 107 | 'psgi.version' => [ 1, 1 ], | ||
| 108 | 'DOCUMENT_ROOT' => '/var/www/some/other/path', | ||
| 109 | 'psgi.run_once' => '', | ||
| 110 | 'SERVER_NAME' => $host, | ||
| 111 | 'SERVER_ADMIN' => '[no address given]', | ||
| 112 | 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', | ||
| 113 | 'HTTP_CONNECTION' => 'close', | ||
| 114 | 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', | ||
| 115 | 'SERVER_PORT' => '80', | ||
| 116 | 'HTTP_COOKIE' => $cookie, | ||
| 117 | 'REMOTE_ADDR' => '127.0.0.1', | ||
| 118 | 'SERVER_PROTOCOL' => 'HTTP/1.0', | ||
| 119 | 'HTTP_X_FORWARDED_FOR' => '127.0.0.1', | ||
| 120 | 'psgi.errors' => *STDERR, | ||
| 121 | 'REQUEST_URI' => $uri . (length $query ? '?' . $query : ''), | ||
| 122 | 'psgi.nonblocking' => '', | ||
| 123 | 'SERVER_ADDR' => '127.0.0.1', | ||
| 124 | 'psgi.url_scheme' => 'http', | ||
| 125 | 'HTTP_HOST' => $host, | ||
| 126 | 'psgi.input' => | ||
| 127 | bless( do{ \ (my $o = '140160744829088')}, 'Apache2::RequestRec'), | ||
| 128 | 'MOD_PERL' => 'mod_perl/2.0.5'}; | ||
| 129 | |||
| 130 | if ($method eq 'POST' and defined $body and length $body) { | ||
| 131 | $env_hash = { | ||
| 132 | %{$env_hash}, | ||
| 133 | 'CONTENT_LENGTH' => length $body, | ||
| 134 | 'CONTENT_TYPE' => | ||
| 135 | 'application/x-www-form-urlencoded; charset=UTF-8', | ||
| 136 | 'psgix.input.buffered' => 1, | ||
| 137 | 'psgi.input' => FileHandle->new(\ $body, '<')}; | ||
| 138 | } | ||
| 139 | |||
| 140 | if (defined $content_type) { | ||
| 141 | $env_hash = { | ||
| 142 | %{$env_hash}, | ||
| 143 | 'CONTENT_TYPE' => $content_type . '; charset=UTF-8'}; | ||
| 144 | } | ||
| 145 | |||
| 146 | my $registry = Eve::Registry->new(); | ||
| 147 | |||
| 148 | return Eve::HttpRequest::Psgi->new( | ||
| 149 | uri_constructor => sub { | ||
| 150 | return $registry->get_uri(@_); | ||
| 151 | }, | ||
| 152 | env_hash => $env_hash); | ||
| 153 | } | ||
| 154 | |||
| 155 | =head1 SEE ALSO | ||
| 156 | |||
| 157 | =over 4 | ||
| 158 | |||
| 159 | =item L<Eve::Test> | ||
| 160 | |||
| 161 | =item L<Test::Class> | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =head1 LICENSE AND COPYRIGHT | ||
| 166 | |||
| 167 | Copyright 2010-2013 Sergey Konoplev, Igor Zinovyev. | ||
| 168 | |||
| 169 | This program is free software; you can redistribute it and/or modify it | ||
| 170 | under the terms of either: the GNU General Public License as published | ||
| 171 | by the Free Software Foundation; or the Artistic License. | ||
| 172 | |||
| 173 | See http://dev.perl.org/licenses/ for more information. | ||
| 174 | |||
| 175 | =head1 AUTHOR | ||
| 176 | |||
| 177 | =over 4 | ||
| 178 | |||
| 179 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||
| 180 | |||
| 181 | =item L<Igor Zinovyev|mailto:zinigor@gmail.com> | ||
| 182 | |||
| 183 | =back | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | 1; | ||