Skip to content

Commit

Permalink
Fix some typos, cleanup, better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jlomas-stripe committed Aug 10, 2017
1 parent a978803 commit 6c57841
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ function onRequest(request) {
// Do something.
}

// Add the event handler function:
stripe.on('request', onRequest);

stripe.removeListener('request', onRequest);
// OR: stripe.off('request', onRequest);
// Remove the event handler function:
stripe.off('request', onRequest);
```

#### `request` object
Expand All @@ -166,7 +167,7 @@ stripe.removeListener('request', onRequest);
path: '/v1/charges',
status: 402,
request_id: 'req_Ghc9r26ts73DRf',
elapsed: 445
elapsed: 445 // Elapsed time in milliseconds
}
```

Expand Down
7 changes: 4 additions & 3 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,16 @@ StripeResource.prototype = {
});
res.on('end', function() {
var headers = res.headers || {};
// NOTE: Stripe responds with lowercase header names/keys.

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

var responseEvent = utils.removeEmpty({
api_version: req._requestEvent.api_version,
account: headers['Stripe-Account'],
idempotency_key: headers['Idempotency-Key'],
api_version: headers['stripe-version'],
account: headers['stripe-account'],
idempotency_key: headers['idempotency-key'],
method: req._requestEvent.method,
path: req._requestEvent.path,
status: res.statusCode,
Expand Down
6 changes: 3 additions & 3 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Stripe.USER_AGENT_SERIALIZED = null;

var APP_INFO_PROPERTIES = ['name', 'version', 'url'];

var exec = require('child_process').exec;
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var exec = require('child_process').exec;
var objectAssign = require('object-assign');
var util = require('util');

var resources = {
// Support Accounts for consistency, Account for backwards compat
Expand Down Expand Up @@ -88,7 +88,7 @@ function Stripe(key, version) {
});

this.on = this._emitter.on.bind(this._emitter);
this.off = this.removeListener = this._emitter.removeListener.bind(this._emitter);
this.off = this._emitter.removeListener.bind(this._emitter);

this._api = {
auth: null,
Expand Down
56 changes: 48 additions & 8 deletions test/flows.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,38 @@ describe('Flows', function() {
});

describe('Request/Response Events', function() {
var connectedAccountId;

before(function(done) {
// Pick a random connected account to use in the `Stripe-Account` header
stripe.accounts.list({
limit: 1,
}).then(function(accounts) {
if (accounts.data.length < 1) {
return done(
new Error('Test requires at least one Connected Account in the Test Account')
);
}

connectedAccountId = accounts.data[0].id;

done();
});
});

it('should emit a `request` event to listeners on request', function(done) {
var idempotency_key = Math.random().toString(36).slice(2);
var apiVersion = '2017-06-05';
var idempotencyKey = Math.random().toString(36).slice(2);

function onRequest(request) {
stripe.removeListener('request', onRequest);
stripe.off('request', onRequest);

expect(request).to.eql({
api_version: 'latest',
idempotency_key: idempotency_key,
idempotency_key: idempotencyKey,
method: 'POST',
path: '/v1/charges',
account: connectedAccountId,
});

done();
Expand All @@ -435,15 +456,30 @@ describe('Flows', function() {
currency: 'usd',
card: 'tok_chargeDeclined',
}, {
idempotency_key: idempotency_key,
}).then(null, function() {});
api_version: apiVersion,
idempotency_key: idempotencyKey,
stripe_account: connectedAccountId,
}).then(null, function() {
// I expect there to be an error here.
});
});

it('should emit a `response` event to listeners on response', function(done) {
var apiVersion = '2017-06-05';
var idempotencyKey = Math.random().toString(36).slice(2);

function onResponse(response) {
stripe.removeListener('response', onResponse);
// On the off chance we're picking up a response from a differentrequest
// then just ignore this and wait for the right one:
if (response.idempotency_key !== idempotencyKey) {
return;
}

stripe.off('response', onResponse);

expect(response.api_version).to.equal('latest');
expect(response.api_version).to.equal(apiVersion);
expect(response.idempotency_key).to.equal(idempotencyKey);
expect(response.account).to.equal(connectedAccountId);
expect(response.method).to.equal('POST');
expect(response.path).to.equal('/v1/charges');
expect(response.request_id).to.match(/req_[\w\d]/);
Expand All @@ -459,6 +495,10 @@ describe('Flows', function() {
amount: 1234,
currency: 'usd',
card: 'tok_chargeDeclined',
}, {
api_version: apiVersion,
idempotency_key: idempotencyKey,
stripe_account: connectedAccountId,
}).then(null, function() {
// I expect there to be an error here.
});
Expand All @@ -470,7 +510,7 @@ describe('Flows', function() {
}

stripe.on('response', onResponse);
stripe.removeListener('response', onResponse);
stripe.off('response', onResponse);

stripe.charges.create({
amount: 1234,
Expand Down
2 changes: 1 addition & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('utils', function() {
});

describe('removeEmpty', function() {
it('removes empty properties and leave non-empty ones', function() {
it('removes empty properties and leaves non-empty ones', function() {
expect(utils.removeEmpty({
cat: 3,
dog: false,
Expand Down

0 comments on commit 6c57841

Please sign in to comment.