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