=begin testing-SETUP
-BEGIN {
- eval 'use HTTP::Headers; use Params::Coerce; use URI;';
- if ($@) {
- diag 'HTTP::Headers, Params::Coerce & URI required for this test';
- ok(1);
- exit 0;
- }
-}
+use Test::Requires {
+ 'HTTP::Headers' => '0',
+ 'Params::Coerce' => '0',
+ 'URI' => '0',
+};
=end testing-SETUP
use Params::Coerce ();
use URI ();
- subtype 'My.HTTP::Headers' => as class_type('HTTP::Headers');
+ subtype 'My::Types::HTTP::Headers' => as class_type('HTTP::Headers');
- coerce 'My.HTTP::Headers'
+ coerce 'My::Types::HTTP::Headers'
=> from 'ArrayRef'
=> via { HTTP::Headers->new( @{$_} ) }
=> from 'HashRef'
=> via { HTTP::Headers->new( %{$_} ) };
- subtype 'My.URI' => as class_type('HTTP::Headers');
+ subtype 'My::Types::URI' => as class_type('URI');
- coerce 'My.URI'
+ coerce 'My::Types::URI'
=> from 'Object'
=> via { $_->isa('URI')
? $_
=> as 'Str'
=> where { /^HTTP\/[0-9]\.[0-9]$/ };
- has 'base' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
- has 'uri' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
+ has 'base' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 );
+ has 'uri' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 );
has 'method' => ( is => 'rw', isa => 'Str' );
has 'protocol' => ( is => 'rw', isa => 'Protocol' );
has 'headers' => (
is => 'rw',
- isa => 'My.HTTP::Headers',
+ isa => 'My::Types::HTTP::Headers',
coerce => 1,
default => sub { HTTP::Headers->new }
);
First, we create the subtype to which we will coerce the other types:
- subtype 'My.HTTP::Headers' => as class_type('HTTP::Headers');
+ subtype 'My::Types::HTTP::Headers' => as class_type('HTTP::Headers');
We are creating a subtype rather than using C<HTTP::Headers> as a type
directly. The reason we do this is coercions are global, and a
instance, and just do the right thing. This is exactly what coercion
is for:
- coerce 'My.HTTP::Headers'
+ coerce 'My::Types::HTTP::Headers'
=> from 'ArrayRef'
=> via { HTTP::Headers->new( @{$_} ) }
=> from 'HashRef'
has 'headers' => (
is => 'rw',
- isa => 'My.HTTP::Headers',
+ isa => 'My::Types::HTTP::Headers',
coerce => 1,
default => sub { HTTP::Headers->new }
);
Once again, we need to declare a class type for our non-Moose L<URI>
class:
- subtype 'My.URI' => as class_type('HTTP::Headers');
+ subtype 'My::Types::URI' => as class_type('URI');
Then we define the coercion:
- coerce 'My.URI'
+ coerce 'My::Types::URI'
=> from 'Object'
=> via { $_->isa('URI')
? $_
Finally, we need to make sure our attributes enable coercion.
- has 'base' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
- has 'uri' => ( is => 'rw', isa => 'My.URI', coerce => 1 );
+ has 'base' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 );
+ has 'uri' => ( is => 'rw', isa => 'My::Types::URI', coerce => 1 );
Re-using the coercion lets us enforce a consistent API across multiple
attributes.
=head1 COPYRIGHT AND LICENSE
-Copyright 2006-2009 by Infinity Interactive, Inc.
+Copyright 2006-2010 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>
is( $header4->content_type, 'application/pdf',
'... got the right content type in the header' );
- dies_ok {
- $r->headers('Foo');
- }
- '... dies when it gets bad params';
+ isnt(
+ exception {
+ $r->headers('Foo');
+ },
+ undef,
+ '... dies when it gets bad params'
+ );
}
{
is( $r->protocol, undef, '... got nothing by default' );
- lives_ok {
- $r->protocol('HTTP/1.0');
- }
- '... set the protocol correctly';
+ is(
+ exception {
+ $r->protocol('HTTP/1.0');
+ },
+ undef,
+ '... set the protocol correctly'
+ );
+
is( $r->protocol, 'HTTP/1.0', '... got nothing by default' );
- dies_ok {
- $r->protocol('http/1.0');
- }
- '... the protocol died with bar params correctly';
+ isnt(
+ exception {
+ $r->protocol('http/1.0');
+ },
+ undef,
+ '... the protocol died with bar params correctly'
+ );
+}
+
+{
+ $r->base('http://localhost/');
+ isa_ok( $r->base, 'URI' );
+
+ $r->uri('http://localhost/');
+ isa_ok( $r->uri, 'URI' );
}
=end testing