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