Repository: stripe/stripe-node
Pull request #960 (2020-07-16 15:48:51)

Use strict equality check for 'protocol' field for consistency
Minor change, but the code below uses triple `=`, so might be worth cleaning this up

richardm-stripe: Looks good to me, thanks for the cleanup!

Patch:
diff --git a/lib/StripeResource.js b/lib/StripeResource.js
index b2d73fde..036812dc 100644
--- a/lib/StripeResource.js
+++ b/lib/StripeResource.js
@@ -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;
 
@@ -408,7 +423,7 @@ StripeResource.prototype = {
           : this._stripe.getApiField('timeout');
 
       const isInsecureConnection =
-        this._stripe.getApiField('protocol') == 'http';
+        this._stripe.getApiField('protocol') === 'http';
       let agent = this._stripe.getApiField('agent');
       if (agent == null) {
         agent = isInsecureConnection ? defaultHttpAgent : defaultHttpsAgent;


Test patch:
diff --git a/test/stripe.spec.js b/test/stripe.spec.js
index 08e27ffd..2de04746 100644
--- a/test/stripe.spec.js
+++ b/test/stripe.spec.js
@@ -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;
 
@@ -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(