s/metaclass/__PACKAGE__->meta/
[gitmo/Moose.git] / t / 000_recipes / 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 );
5cf3dbcf 64
5a3217de 65 __PACKAGE__->meta->make_immutable(debug => 0);
471c4f09 66}
00867c44 67
471c4f09 68my $r = Request->new;
69isa_ok($r, 'Request');
00867c44 70
471c4f09 71{
72 my $header = $r->headers;
73 isa_ok($header, 'HTTP::Headers');
00867c44 74
471c4f09 75 is($r->headers->content_type, '', '... got no content type in the header');
00867c44 76
471c4f09 77 $r->headers( { content_type => 'text/plain' } );
4b598ea3 78
471c4f09 79 my $header2 = $r->headers;
80 isa_ok($header2, 'HTTP::Headers');
81 isnt($header, $header2, '... created a new HTTP::Header object');
4b598ea3 82
471c4f09 83 is($header2->content_type, 'text/plain', '... got the right content type in the header');
4b598ea3 84
471c4f09 85 $r->headers( [ content_type => 'text/html' ] );
4b598ea3 86
471c4f09 87 my $header3 = $r->headers;
88 isa_ok($header3, 'HTTP::Headers');
89 isnt($header2, $header3, '... created a new HTTP::Header object');
4b598ea3 90
471c4f09 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');
4b598ea3 98
471c4f09 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';
00867c44 104}
4b598ea3 105
00867c44 106{
471c4f09 107 is($r->protocol, undef, '... got nothing by default');
4b598ea3 108
471c4f09 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';
00867c44 117}
4b598ea3 118