changes-and-comments
[gitmo/Moose.git] / t / 005_basic.t
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     use Moose::Util::TypeConstraints;
26         
27         use HTTP::Headers  ();
28         use Params::Coerce ();
29         use URI            ();
30
31         subtype Header
32             => as Object
33             => where { $_->isa('HTTP::Headers') };
34
35         coerce Header
36             => from ArrayRef
37                 => via { HTTP::Headers->new( @{ $_ } ) }
38             => from HashRef
39                 => via { HTTP::Headers->new( %{ $_ } ) };
40
41         subtype Uri
42             => as Object
43             => where { $_->isa('URI') };
44
45         coerce Uri
46             => from Object
47                 => via { $_->isa('URI') ? $_ : Params::Coerce::coerce( 'URI', $_ ) }
48             => from Str
49                 => via { URI->new( $_, 'http' ) };
50
51         subtype Protocol
52             => as Str
53             => where { /^HTTP\/[0-9]\.[0-9]$/ };
54
55
56         has 'base'     => (is => 'rw', isa => 'Uri', coerce  => 1);
57         has 'url'      => (is => 'rw', isa => 'Uri', coerce  => 1);     
58         has 'method'   => (is => 'rw', isa => 'Str');   
59         has 'protocol' => (is => 'rw', isa => 'Protocol');              
60         has 'headers'  => (
61             is      => 'rw',
62             isa     => 'Header',
63             coerce  => 1,
64             default => sub { HTTP::Headers->new } 
65     );
66 }
67
68 my $r = Request->new;
69 isa_ok($r, 'Request');
70
71 {
72     my $header = $r->headers;
73     isa_ok($header, 'HTTP::Headers');
74
75     is($r->headers->content_type, '', '... got no content type in the header');
76
77     $r->headers( { content_type => 'text/plain' } );
78
79     my $header2 = $r->headers;
80     isa_ok($header2, 'HTTP::Headers');
81     isnt($header, $header2, '... created a new HTTP::Header object');
82
83     is($header2->content_type, 'text/plain', '... got the right content type in the header');
84
85     $r->headers( [ content_type => 'text/html' ] );
86
87     my $header3 = $r->headers;
88     isa_ok($header3, 'HTTP::Headers');
89     isnt($header2, $header3, '... created a new HTTP::Header object');
90
91     is($header3->content_type, 'text/html', '... got the right content type in the header');
92     
93     $r->headers( HTTP::Headers->new(content_type => 'application/pdf') );
94     
95     my $header4 = $r->headers;    
96     isa_ok($header4, 'HTTP::Headers');
97     isnt($header3, $header4, '... created a new HTTP::Header object');
98
99     is($header4->content_type, 'application/pdf', '... got the right content type in the header');    
100     
101     dies_ok {
102         $r->headers('Foo')
103     } '... dies when it gets bad params';
104 }
105
106 {
107     is($r->protocol, undef, '... got nothing by default');
108
109     lives_ok {
110         $r->protocol('HTTP/1.0');
111     } '... set the protocol correctly';
112     is($r->protocol, 'HTTP/1.0', '... got nothing by default');
113             
114     dies_ok {
115         $r->protocol('http/1.0');
116     } '... the protocol died with bar params correctly';            
117 }
118