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