Commit | Line | Data |
b841b2a3 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
7 | |
8 | BEGIN { |
9 | eval "use HTTP::Headers; use Params::Coerce; use URI;"; |
10 | plan skip_all => "HTTP::Headers & Params::Coerce & URI required for this test" if $@; |
11 | plan no_plan => 1; |
12 | } |
13 | |
14 | use Test::Exception; |
15 | |
16 | BEGIN { |
17 | use_ok('Moose'); |
18 | } |
19 | |
20 | { |
21 | package Request; |
22 | use strict; |
23 | use warnings; |
24 | use Moose; |
25 | |
26 | use HTTP::Headers (); |
27 | use Params::Coerce (); |
28 | use URI (); |
29 | |
30 | subtype Header |
31 | => as Object |
32 | => where { $_->isa('HTTP::Headers') }; |
33 | |
34 | coerce Header |
35 | => from ArrayRef |
36 | => via { HTTP::Headers->new( @{ $_ } ) } |
37 | => from HashRef |
38 | => via { HTTP::Headers->new( %{ $_ } ) }; |
39 | |
40 | subtype Uri |
41 | => as Object |
42 | => where { $_->isa('URI') }; |
43 | |
44 | coerce Uri |
45 | => from Object |
46 | => via { $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ) } |
47 | => from Str |
48 | => via { URI->new( $_, 'http' ) }; |
49 | |
50 | subtype Protocol |
51 | => as Str |
52 | => where { /^HTTP\/[0-9]\.[0-9]$/ }; |
53 | |
54 | |
55 | has 'base' => (is => 'rw', isa => 'Uri', coerce => 1); |
56 | has 'url' => (is => 'rw', isa => 'Uri', coerce => 1); |
57 | has 'method' => (is => 'rw', isa => 'Str'); |
58 | has 'protocol' => (is => 'rw', isa => 'Protocol'); |
59 | has 'headers' => ( |
60 | is => 'rw', |
61 | isa => 'Header', |
62 | coerce => 1, |
63 | default => sub { HTTP::Headers->new } |
64 | ); |
65 | } |
66 | |
67 | my $r = Request->new; |
68 | isa_ok($r, 'Request'); |
69 | |
70 | { |
71 | my $header = $r->headers; |
72 | isa_ok($header, 'HTTP::Headers'); |
73 | |
74 | is($r->headers->content_type, '', '... got no content type in the header'); |
75 | |
76 | $r->headers( { content_type => 'text/plain' } ); |
77 | |
78 | my $header2 = $r->headers; |
79 | isa_ok($header2, 'HTTP::Headers'); |
80 | isnt($header, $header2, '... created a new HTTP::Header object'); |
81 | |
82 | is($header2->content_type, 'text/plain', '... got the right content type in the header'); |
83 | |
84 | $r->headers( [ content_type => 'text/html' ] ); |
85 | |
86 | my $header3 = $r->headers; |
87 | isa_ok($header3, 'HTTP::Headers'); |
88 | isnt($header2, $header3, '... created a new HTTP::Header object'); |
89 | |
90 | is($header3->content_type, 'text/html', '... got the right content type in the header'); |
91 | |
92 | $r->headers( HTTP::Headers->new(content_type => 'application/pdf') ); |
93 | |
94 | my $header4 = $r->headers; |
95 | isa_ok($header4, 'HTTP::Headers'); |
96 | isnt($header3, $header4, '... created a new HTTP::Header object'); |
97 | |
98 | is($header4->content_type, 'application/pdf', '... got the right content type in the header'); |
99 | |
100 | dies_ok { |
101 | $r->headers('Foo') |
102 | } '... dies when it gets bad params'; |
103 | } |
104 | |
4e036ee4 |
105 | { |
106 | is($r->protocol, undef, '... got nothing by default'); |
b841b2a3 |
107 | |
4e036ee4 |
108 | lives_ok { |
109 | $r->protocol('HTTP/1.0'); |
110 | } '... set the protocol correctly'; |
111 | is($r->protocol, 'HTTP/1.0', '... got nothing by default'); |
112 | |
113 | dies_ok { |
114 | $r->protocol('http/1.0'); |
115 | } '... the protocol died with bar params correctly'; |
116 | } |
b841b2a3 |
117 | |