| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | use Test::More; |
|---|
| 4 | |
|---|
| 5 | eval q{ use HTTP::Session; }; |
|---|
| 6 | plan skip_all => "HTTP::Session is not installed" if $@; |
|---|
| 7 | eval q( { package foo; use Any::Moose;use Any::Moose 'X::Types' } ); |
|---|
| 8 | plan skip_all => "Mo[ou]seX::Types is not installed" if $@; |
|---|
| 9 | |
|---|
| 10 | plan tests => 8; |
|---|
| 11 | |
|---|
| 12 | use HTTP::Engine; |
|---|
| 13 | use HTTP::Engine::Middleware; |
|---|
| 14 | use HTTP::Engine::Response; |
|---|
| 15 | use HTTP::Request; |
|---|
| 16 | use HTTP::Request::Common; |
|---|
| 17 | |
|---|
| 18 | sub run_engine (&) { |
|---|
| 19 | my $code = shift; |
|---|
| 20 | |
|---|
| 21 | my $mw = HTTP::Engine::Middleware->new({method_class => 'HTTP::Engine::Request'}); |
|---|
| 22 | $mw->install( |
|---|
| 23 | 'HTTP::Engine::Middleware::HTTPSession' => { |
|---|
| 24 | state => { |
|---|
| 25 | class => 'URI', |
|---|
| 26 | args => { |
|---|
| 27 | session_id_name => 'foo_sid', |
|---|
| 28 | }, |
|---|
| 29 | }, |
|---|
| 30 | store => { |
|---|
| 31 | class => 'Test', |
|---|
| 32 | args => { }, |
|---|
| 33 | }, |
|---|
| 34 | } |
|---|
| 35 | ); |
|---|
| 36 | |
|---|
| 37 | my $request = HTTP::Request->new( GET => 'http://localhost/?getparam=1', ); |
|---|
| 38 | my $res = HTTP::Engine->new( |
|---|
| 39 | interface => { |
|---|
| 40 | module => 'Test', |
|---|
| 41 | request_handler => $mw->handler( $code ), |
|---|
| 42 | }, |
|---|
| 43 | )->run($request); |
|---|
| 44 | |
|---|
| 45 | return $res; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | MAIN: { |
|---|
| 49 | |
|---|
| 50 | my $res = run_engine { |
|---|
| 51 | my $req = shift; |
|---|
| 52 | $req->session; |
|---|
| 53 | HTTP::Engine::Response->new( body => '<a href="/tmp/">foo</a>' ); |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | my $out = $res->content; |
|---|
| 57 | is $res->code, '200', 'response code'; |
|---|
| 58 | like $out, qr{<a href="/tmp/\?foo_sid=.{32}">foo</a>}, 'response content'; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | $res = run_engine { |
|---|
| 62 | my $req = shift; |
|---|
| 63 | HTTP::Engine::Response->new( body => '<a href="/tmp/">foo</a>' ); |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | $out = $res->content; |
|---|
| 67 | is $res->code, '200', 'response code'; |
|---|
| 68 | like $out, qr{<a href="/tmp/">foo</a>}, 'response content'; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | COERCE: { |
|---|
| 72 | my $s = HTTP::Engine::Middleware::HTTPSession->new( |
|---|
| 73 | state => HTTP::Session::State::URI->new( |
|---|
| 74 | session_id_name => 'foo_sid', |
|---|
| 75 | ), |
|---|
| 76 | store => HTTP::Session::Store::Test->new(), |
|---|
| 77 | ); |
|---|
| 78 | is ref($s->state), 'CODE'; |
|---|
| 79 | is ref($s->state->()), 'HTTP::Session::State::URI'; |
|---|
| 80 | is ref($s->store), 'CODE'; |
|---|
| 81 | is ref($s->store->()), 'HTTP::Session::Store::Test'; |
|---|
| 82 | }; |
|---|