WebService-MyGengo

 view release on metacpan or  search on metacpan

t/lib/WebService/MyGengo/Test/Mock/LWP.pm  view on Meta::CPAN

package WebService::MyGengo::Test::Mock::LWP;

use Moose;
use MooseX::NonMoose;

extends 'LWP::UserAgent';

use WebService::MyGengo::Job;
use WebService::MyGengo::Comment;
use WebService::MyGengo::Revision;

use URI::Escape;
use JSON;
use Data::Dumper;

=head1 NAME

WebService::MyGengo::Test::Mock::LWP - A mock LWP for testing the WebService::MyGengo API

=head1 DESCRIPTION

Sending too many requests to the myGengo sandbox results in account throttling.

Luckily, we don't really have to interact with the real Sandbox to use
automated testing!

This mock library accepts an L<HTTP::Request> object from L<WebService::MyGengo::Client>
and returns success or failure responses based on certain flags in the request.

=head1 SYNOPSIS

Also see L<WebService::MyGengo::Test::Util::Client> in the t/lib directory.

    # t/001-blah.t
    use WebService::MyGengo::Client;
    use WebService::MyGengo::Test::Mock::LWP;

    # Your normal testing configuration
    my $config = {
        public_key      => 'real-pubkey"
        , private_key   => 'real-privkey'
        , use_sandbox   => 1
        };

    my $client = WebService::MyGengo::Client->new( $config );
    my $ua = WebService::MyGengo::Test::Mock::LWP->new();
    $client->_set_user_agent( $ua ); # For testing purposes only

    # A mocked WebService::MyGengo::Account object
    my $acct = $client->get_account();

    # From here on out, any subsequent calls will return a successful response
    $client->public_key( 'OK' );

    # From here on out, any subsequent calls will return API errors
    $client->public_key( 'APIFAIL' );

    # From here on out, any subsequent calls will return Internal server errors
    $client->public_key( 'SERVFAIL' );

    # To revert to normal functioning, build a fresh client
    $client = WebService::MyGengo::Client->new( $config );

=head1 public_key FLAG

Some methods do not accept any parameters, so their responses must be
managed using the value of the public_key as a flag (as in the synopsis.)

Because the public key is passed in cleartext in API requests, we can
capture and evaluate it here to determine what kind of response
to return.

The default is "OK".

=over

=item OK = A valid API response.

=item APIFAIL = Failure at the API level. Returns a valid API response with the
'opstat' parameter set to 'error' and an API error code in the response body.

=item SERVFAIL = Failure at the server level. Returns an HTTP 500 response for
now.

=back

B<Don't forget to check the pubkey value before troubleshooting other failing
tests!>

=head1 ATTRIBUTES

=head2 DEBUG (Bool)

Set to true to enable (verbose and oogly) debugging to STDERR

=cut
has DEBUG => ( is => 'rw', isa => 'Bool', default => 0 );

=head2 next_job_id (Int)

Used to increment the IDs of new Jobs.



( run in 1.542 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )