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