root/lang/perl/HTTP-Engine-Middleware/trunk/t/200_middlewares/httpsession.t @ 30243

Revision 30243, 2.1 kB (checked in by yappo, 4 years ago)

merged from any-moose branch ( svn merge -r30234:HEAD ../branches/any-moose )

Line 
1use strict;
2use warnings;
3use Test::More;
4
5eval q{ use HTTP::Session; };
6plan skip_all => "HTTP::Session is not installed" if $@;
7eval q( { package foo; use Any::Moose;use Any::Moose 'X::Types' } );
8plan skip_all => "Mo[ou]seX::Types is not installed" if $@;
9
10plan tests => 8;
11
12use HTTP::Engine;
13use HTTP::Engine::Middleware;
14use HTTP::Engine::Response;
15use HTTP::Request;
16use HTTP::Request::Common;
17
18sub 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
48MAIN: {
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
71COERCE: {
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};
Note: See TracBrowser for help on using the browser.