Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Test::LWP::Recorder to mock requests #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

ironcamel
Copy link
Contributor

This mocks our tests so that a stripe key or network connection will not
be necessary to run the tests. This also gives a major speedup. We can
still run real tests hitting the actual stripe webservice as we do now, if
a stripe api key is provided.

@tildedave
Copy link

Omg yay

@danschmidt5189
Copy link
Contributor

This is fantastic. I'm reviewing, should be OK but I just want to familiarize myself with the changes.

@@ -62,12 +65,20 @@ sub skip_unless_has_secret {
plan skip_all => 'PERL_STRIPE_TEST_API_KEY is required' unless api_key();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need skip_unless_has_secret anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to keep it because t/07-options.t still needs it. We can't use a mock in the idempotency_key test https://github.com/Crowdtilt/WebService-Stripe/blob/lwp-recorder/t/07-options.t#L17

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would filter_header => 'Idempotency-Key' solve that? I'm not sure if that affects how LWP Recorder would match a request with a response.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it wouldn't solve it. The problem is that LWP Recorder has no way to differentiate between 2 identical requests. It would only store the response for one of them. That would make mocking this test useless.

This mocks our tests so that a stripe key or network connection will not
be necessary to run the tests. This also gives a major speedup. We can
still run real tests hitting the actual stripe webservice as we do now, if
a stripe api key is provided.
@ironcamel
Copy link
Contributor Author

The tests run in under 1s now:

➜  WebService-Stripe git:(lwp-recorder) prove -l -I ../Test-LWP-Recorder/lib
t/01-customers.t .... ok   
t/02-charges.t ...... ok   
t/03-banks.t ........ ok   
t/04-transfers.t .... ok   
t/05-headers.t ...... ok   
t/06-balance.t ...... ok   
t/07-options.t ...... skipped: PERL_STRIPE_TEST_API_KEY is required
t/08-file-upload.t .. ok   
t/09-refunds.t ...... ok   
t/10-accounts.t ..... ok   
All tests successful.
Files=10, Tests=26,  1 wallclock secs ( 0.03 usr  0.01 sys +  1.15 cusr  0.06 csys =  1.25 CPU)
Result: PASS

state $client = WebService::Stripe->new(
api_key => api_key(),
version => '2014-12-17',
%params
%params,
);
return $client;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't like no_-prefixed variables. Affirmative variables (e.g. mock_lwp => 1) tend to be clearer / less confusing. Suggested rewrite:

memoize 'stripe';
sub stripe {
    my %params = @_;
    $params{'api_key'}         //= api_key;
    $params{'version'}         //= '2014-12-17';
    $params{'mock_lwp_record'} //= $ENV{'PERL_STRIPE_MOCK_RECORD'};
    $params{'mock_lwp'}        //= $ENV{'PERL_STRIPE_MOCK'}
        // !$params{'api_key'};

    my %client;
    $client{'api_key'} = $params{'api_key'};
    $client{'version'} = $params{'version'};
    $client{'ua'}      = Test::LWP::Recorder->new({
        record    => $params{'mock_lwp_record'},
        cache_dir => 't/LWPCache/' . basename($0),
    }) if $params{'mock_lwp'};

    state $stripe = WebService::Stripe->new(%client);
    return $stripe;
}

@danschmidt5189
Copy link
Contributor

👍 Had a Q about using filter_header to handle the Idempotency-Key though.

@ironcamel
Copy link
Contributor Author

We can't merge this until this patch gets accepted riemann42/Test-LWP-Recorder#3 , or the author lets me take over that module, or we aggressively fork it.

@ironcamel
Copy link
Contributor Author

Another option is to use this module instead https://metacpan.org/pod/Test::VCR::LWP

@danschmidt5189
Copy link
Contributor

That module seems much harder to use suite-wide than the current one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants