| 1 | package Test::Mock::LWP; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Exporter'; | ||
| 5 | use Test::MockObject; | ||
| 6 | our @EXPORT = qw($Mock_ua $Mock_req $Mock_request $Mock_resp $Mock_response); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Test::Mock::LWP - Easy mocking of LWP packages | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | Make LWP packages to make testing easier. | ||
| 17 | |||
| 18 | use Test::Mock::LWP; | ||
| 19 | |||
| 20 | # Setup fake response content and code | ||
| 21 | $Mock_response->mock( content => sub { 'foo' } ); | ||
| 22 | $Mock_resp->mock( code => sub { 201 } ); | ||
| 23 | |||
| 24 | # Validate args passed to request constructor | ||
| 25 | is_deeply $Mock_request->new_args, \@expected_args; | ||
| 26 | |||
| 27 | # Validate request headers | ||
| 28 | is_deeply [ $Mock_req->next_call ], | ||
| 29 | [ 'header', [ 'Accept', 'text/plain' ] ]; | ||
| 30 | |||
| 31 | # Special User Agent Behaviour | ||
| 32 | $Mock_ua->mock( request => sub { die 'foo' } ); | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This package arises from duplicating the same code to mock LWP et al in | ||
| 37 | several different modules I've written. This version is very minimalist, but | ||
| 38 | works for my needs so far. I'm very open to new suggestions and improvements. | ||
| 39 | |||
| 40 | =head1 EXPORTS | ||
| 41 | |||
| 42 | The following variables are exported by default: | ||
| 43 | |||
| 44 | =over 4 | ||
| 45 | |||
| 46 | =item $Mock_ua | ||
| 47 | |||
| 48 | The mock LWP::UserAgent object - a Test::MockObject object | ||
| 49 | |||
| 50 | =item $Mock_req, $Mock_request | ||
| 51 | |||
| 52 | The mock HTTP::Request object - a Test::MockObject object | ||
| 53 | |||
| 54 | =item $Mock_resp, $Mock_response | ||
| 55 | |||
| 56 | The mock HTTP::Response object - a Test::MockObject object | ||
| 57 | |||
| 58 | =back | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | our $VERSION = '0.08'; | ||
| 63 | |||
| 64 | BEGIN { | ||
| 65 | # Don't load the mock classes if the real ones are already loaded | ||
| 66 | my $mo = Test::MockObject->new; | ||
| 67 | my @mock_classes = ( | ||
| 68 | [ 'HTTP::Response' => '$Mock_response $Mock_resp' ], | ||
| 69 | [ 'HTTP::Request' => '$Mock_request $Mock_req' ], | ||
| 70 | [ 'LWP::UserAgent' => '$Mock_ua' ], | ||
| 71 | ); | ||
| 72 | for my $c (@mock_classes) { | ||
| 73 | my ($real, $imports) = @$c; | ||
| 74 | if (!$mo->check_class_loaded($real)) { | ||
| 75 | my $mock_class = "Test::Mock::$real"; | ||
| 76 | eval "require $mock_class"; | ||
| 77 | if ($@) { | ||
| 78 | warn "error during require $mock_class: $@" if $@; | ||
| 79 | next; | ||
| 80 | } | ||
| 81 | my $import = "$mock_class qw($imports)"; | ||
| 82 | eval "import $import"; | ||
| 83 | warn "error during import $import: $@" if $@; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | =head1 AUTHOR | ||
| 89 | |||
| 90 | Luke Closs, C<< <test-mock-lwp at 5thplane.com> >> | ||
| 91 | |||
| 92 | =head1 BUGS | ||
| 93 | |||
| 94 | Please report any bugs or feature requests through the web interface at | ||
| 95 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Mock-LWP>. | ||
| 96 | I will be notified, and then you'll automatically be notified of progress on | ||
| 97 | your bug as I make changes. | ||
| 98 | |||
| 99 | =head1 SUPPORT | ||
| 100 | |||
| 101 | You can find documentation for this module with the perldoc command. | ||
| 102 | |||
| 103 | perldoc Test::Mock::LWP | ||
| 104 | |||
| 105 | You can also look for information at: | ||
| 106 | |||
| 107 | =over 4 | ||
| 108 | |||
| 109 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 110 | |||
| 111 | L<http://annocpan.org/dist/Test-Mock-LWP> | ||
| 112 | |||
| 113 | =item * CPAN Ratings | ||
| 114 | |||
| 115 | L<http://cpanratings.perl.org/d/Test-Mock-LWP> | ||
| 116 | |||
| 117 | =item * RT: CPAN's request tracker | ||
| 118 | |||
| 119 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Mock-LWP> | ||
| 120 | |||
| 121 | =item * Search CPAN | ||
| 122 | |||
| 123 | L<http://search.cpan.org/dist/Test-Mock-LWP> | ||
| 124 | |||
| 125 | =back | ||
| 126 | |||
| 127 | =head1 ACKNOWLEDGEMENTS | ||
| 128 | |||
| 129 | =head1 COPYRIGHT & LICENSE | ||
| 130 | |||
| 131 | Copyright 2006 Luke Closs, all rights reserved. | ||
| 132 | |||
| 133 | This program is free software; you can redistribute it and/or modify it | ||
| 134 | under the same terms as Perl itself. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | 1; |