Skip to content

Commit

Permalink
Add new fields to lastResponse: apiVersion, stripeAccount, idempotenc…
Browse files Browse the repository at this point in the history
…yKey (#952)

* Add stripeAccount, apiVersion and idempotencyKey to lastResponse
  • Loading branch information
polybuildr committed Jul 16, 2020
1 parent 10d6787 commit 1b84a26
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
17 changes: 16 additions & 1 deletion lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,25 @@ StripeResource.prototype = {
const headers = res.headers || {};
// NOTE: Stripe responds with lowercase header names/keys.

// For convenience, make Request-Id easily accessible on
// For convenience, make some headers easily accessible on
// lastResponse.
res.requestId = headers['request-id'];

const stripeAccount = headers['stripe-account'];
if (stripeAccount) {
res.stripeAccount = stripeAccount;
}

const apiVersion = headers['stripe-version'];
if (apiVersion) {
res.apiVersion = apiVersion;
}

const idempotencyKey = headers['idempotency-key'];
if (idempotencyKey) {
res.idempotencyKey = idempotencyKey;
}

const requestEndTime = Date.now();
const requestDurationMs = requestEndTime - req._requestStart;

Expand Down
63 changes: 50 additions & 13 deletions test/stripe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const testUtils = require('../testUtils');
const utils = require('../lib/utils');
const Stripe = require('../lib/stripe');
const stripe = require('../lib/stripe')(testUtils.getUserStripeKey(), 'latest');
const crypto = require('crypto');

const expect = require('chai').expect;

Expand Down Expand Up @@ -390,22 +391,58 @@ describe('Stripe Module', function() {
})
).to.eventually.equal('Called!'));

it('Will expose HTTP response object', () =>
expect(
new Promise((resolve, reject) => {
stripe.customers.create(CUSTOMER_DETAILS, (err, customer) => {
cleanup.deleteCustomer(customer.id);
describe('lastResponse', () => {
it('Will expose HTTP response object', () =>
expect(
new Promise((resolve, reject) => {
stripe.customers.create(CUSTOMER_DETAILS, (err, customer) => {
cleanup.deleteCustomer(customer.id);

const headers = customer.lastResponse.headers;
expect(headers).to.contain.keys('request-id');
const headers = customer.lastResponse.headers;
expect(headers).to.contain.keys('request-id');

expect(customer.lastResponse.requestId).to.match(/^req_/);
expect(customer.lastResponse.statusCode).to.equal(200);
resolve('Called!');
});
})
).to.eventually.equal('Called!'));

resolve('Called!');
});
})
).to.eventually.equal('Called!'));
it('Will have request id, status code and version', () =>
expect(
new Promise((resolve, reject) => {
stripe.customers.create(CUSTOMER_DETAILS, (_err, customer) => {
cleanup.deleteCustomer(customer.id);

expect(customer.lastResponse.requestId).to.match(/^req_/);
expect(customer.lastResponse.statusCode).to.equal(200);
expect(customer.lastResponse.apiVersion).to.be.a('string').that
.is.not.empty;

resolve('Called!');
});
})
).to.eventually.equal('Called!'));

it('Will have the idempotency key', () =>
expect(
new Promise((resolve, reject) => {
const key = crypto.randomBytes(16).toString('hex');

stripe.customers.create(
CUSTOMER_DETAILS,
{
idempotencyKey: key,
},
(err, customer) => {
cleanup.deleteCustomer(customer.id);

expect(customer.lastResponse.idempotencyKey).to.equal(key);

resolve('Called!');
}
);
})
).to.eventually.equal('Called!'));
});

it('Given an error the callback will receive it', () =>
expect(
Expand Down

0 comments on commit 1b84a26

Please sign in to comment.