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

GET and DELETE requests data: body->queryParams #691

Merged
merged 4 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/StripeMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ function stripeMethod(spec) {
}

module.exports = stripeMethod;

module.exports = stripeMethod;
18 changes: 15 additions & 3 deletions lib/makeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
spec.validator(data, {headers});
}

const dataInQuery = spec.method === 'GET' || spec.method === 'DELETE';
const bodyData = dataInQuery ? {} : data;
const queryData = dataInQuery ? data : {};

return {
requestMethod,
requestPath,
data,
bodyData,
queryData,
auth: options.auth,
headers,
host,
Expand Down Expand Up @@ -77,11 +82,18 @@ function makeRequest(self, requestArgs, spec, overrideData) {
}
}

const emptyQuery = Object.keys(opts.queryData).length === 0;
const path = [
opts.requestPath,
emptyQuery ? '' : '?',
utils.stringifyRequestData(opts.queryData),
].join('');

self._request(
opts.requestMethod,
opts.host,
opts.requestPath,
opts.data,
path,
opts.bodyData,
opts.auth,
{headers: opts.headers},
requestCallback
Expand Down
41 changes: 32 additions & 9 deletions test/StripeResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,27 @@ describe('StripeResource', () => {
});

describe('_request', () => {
it('encodes the body in GET requests', (done) => {
it('encodes data for GET requests as query params', (done) => {
const data = {
customer: 'cus_123',
subscription_items: [
{plan: 'foo', quantity: 2},
{id: 'si_123', deleted: true},
],
};
const options = {
host: stripe.getConstant('DEFAULT_HOST'),
path: '/v1/invoices/upcoming',
data: {
customer: 'cus_123',
subscription_items: [
{plan: 'foo', quantity: 2},
{id: 'si_123', deleted: true},
],
},
data,
};

const scope = nock(`https://${options.host}`)
.get(options.path, options.data)
.get(
`${
options.path
}?customer=cus_123&subscription_items[0][plan]=foo&subscription_items[0][quantity]=2&subscription_items[1][id]=si_123&subscription_items[1][deleted]=true`,
''
)
.reply(200, '{}');

realStripe.invoices.retrieveUpcoming(options.data, (err, response) => {
Expand All @@ -71,6 +77,23 @@ describe('StripeResource', () => {
});
});

it('encodes data for DELETE requests as query params', (done) => {
const data = {
foo: 'bar',
};
const host = stripe.getConstant('DEFAULT_HOST');
const path = '/v1/invoiceitems/invoiceItemId1?foo=bar';

const scope = nock(`https://${host}`)
.delete(/.*/)
.reply(200, '{}');

realStripe.invoiceItems.del('invoiceItemId1', data, (err, response) => {
done(err);
scope.done();
});
});

it('encodes the body in POST requests', (done) => {
const options = {
host: stripe.getConstant('DEFAULT_HOST'),
Expand Down
1 change: 0 additions & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--bail
richardm-stripe marked this conversation as resolved.
Show resolved Hide resolved
--recursive
6 changes: 2 additions & 4 deletions test/resources/BitcoinReceivers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ describe('BitcoinReceivers Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/bitcoin/receivers/receiverId/transactions',
url: '/v1/bitcoin/receivers/receiverId/transactions?limit=1',
headers: {},
data: {
limit: 1,
},
data: {},
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/resources/CreditNotes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ describe('CreditNotes Resource', () => {
stripe.creditNotes.list({count: 25});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/credit_notes',
url: '/v1/credit_notes?count=25',
headers: {},
data: {count: 25},
data: {},
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/resources/Events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ describe('Events Resource', () => {
stripe.events.list({count: 25});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/events',
url: '/v1/events?count=25',
headers: {},
data: {count: 25},
data: {},
});
});
});
Expand Down
21 changes: 8 additions & 13 deletions test/resources/Invoices.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe('Invoices Resource', () => {
stripe.invoices.list({count: 25});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices',
url: '/v1/invoices?count=25',
headers: {},
data: {count: 25},
data: {},
});
});
});
Expand Down Expand Up @@ -97,12 +97,10 @@ describe('Invoices Resource', () => {

expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/upcoming',
url:
'/v1/invoices/upcoming?customer=cus_abc&subscription_items[0][plan]=potato&subscription_items[1][plan]=rutabaga',
headers: {},
data: {
customer: 'cus_abc',
subscription_items: [{plan: 'potato'}, {plan: 'rutabaga'}],
},
data: {},
});
});
});
Expand All @@ -117,13 +115,10 @@ describe('Invoices Resource', () => {

expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/upcoming/lines',
url:
'/v1/invoices/upcoming/lines?customer=cus_abc&subscription_items[0][plan]=potato&subscription_items[1][plan]=rutabaga&limit=5',
headers: {},
data: {
customer: 'cus_abc',
subscription_items: [{plan: 'potato'}, {plan: 'rutabaga'}],
limit: 5,
},
data: {},
});
});
});
Expand Down
12 changes: 4 additions & 8 deletions test/resources/OrderReturns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ describe('OrderReturn Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/order_returns',
data: {
limit: 3,
},
url: '/v1/order_returns?limit=3',
data: {},
headers: {},
});
});
Expand All @@ -37,11 +35,9 @@ describe('OrderReturn Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/order_returns',
data: {
order: 'orderIdFoo123',
},
url: '/v1/order_returns?order=orderIdFoo123',
headers: {},
data: {},
});
});
});
Expand Down
12 changes: 4 additions & 8 deletions test/resources/Orders.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ describe('Order Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/orders',
data: {
limit: 3,
},
url: '/v1/orders?limit=3',
data: {},
headers: {},
});
});
Expand All @@ -79,10 +77,8 @@ describe('Order Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/orders',
data: {
status: 'active',
},
url: '/v1/orders?status=active',
data: {},
headers: {},
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/resources/PaymentMethods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe('PaymentMethods Resource', () => {
stripe.paymentMethods.list(data);
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/payment_methods',
url: '/v1/payment_methods?customer=cus_123&type=card',
headers: {},
data,
data: {},
});
});
});
Expand Down
12 changes: 4 additions & 8 deletions test/resources/Products.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ describe('Product Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/products',
data: {
limit: 3,
},
url: '/v1/products?limit=3',
data: {},
headers: {},
});
});
Expand All @@ -57,10 +55,8 @@ describe('Product Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/products',
data: {
shippable: true,
},
url: '/v1/products?shippable=true',
data: {},
headers: {},
});
});
Expand Down
6 changes: 2 additions & 4 deletions test/resources/Radar/ValueListItems.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ describe('Radar', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/radar/value_list_items',
url: '/v1/radar/value_list_items?value_list=rsl_123',
headers: {},
data: {
value_list: 'rsl_123',
},
data: {},
});
});
});
Expand Down
12 changes: 4 additions & 8 deletions test/resources/SKUs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ describe('SKU Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/skus',
data: {
limit: 3,
},
url: '/v1/skus?limit=3',
data: {},
headers: {},
});
});
Expand All @@ -61,10 +59,8 @@ describe('SKU Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/skus',
data: {
product: 'prodId123',
},
url: '/v1/skus?product=prodId123',
data: {},
headers: {},
});
});
Expand Down
7 changes: 2 additions & 5 deletions test/resources/SubscriptionItems.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ describe('SubscriptionItems Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/subscription_items',
url: '/v1/subscription_items?limit=3&subscription=test_sub',
headers: {},
data: {
limit: 3,
subscription: 'test_sub',
},
data: {},
});
});
});
Expand Down
8 changes: 2 additions & 6 deletions test/resources/Subscriptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,9 @@ describe('subscriptions Resource', () => {
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/subscriptions',
url: '/v1/subscriptions?limit=3&customer=test_cus&plan=gold',
headers: {},
data: {
limit: 3,
customer: 'test_cus',
plan: 'gold',
},
data: {},
});
});
});
Expand Down