Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions axiosUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ const httpRequest = async (options) => {
maxRedirects: options.maxRedirects === 0 ? 0 : options.maxRedirects || 5,
validateStatus: options.simple === false ? () => true : defaultValidateStatus
}
if (options.form && options.body === undefined) {
const params = new URLSearchParams()
for (const [key, value] of Object.entries(options.form)) {
if (value !== undefined && value !== null) {
params.append(key, String(value))
}
}
config.data = params
config.headers = {
...config.headers,
'Content-Type': 'application/x-www-form-urlencoded'
}
}
const response = await axiosInstance(/** @type {import('axios').AxiosRequestConfig} */ (config))
if (options.resolveWithFullResponse) {
return { headers: response.headers, body: response.data }
Expand Down
23 changes: 23 additions & 0 deletions test/pushSubscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ describe('pushSubscriptions_test', function () {
'updated_at': '2015-04-29T18:11:09.400558047-07:00'
})
})

it('should POST URL-encoded fields, not an empty body', async () => {
let capturedBody
nock('https://www.strava.com')
.post('/api/v3/push_subscriptions', function (body) {
capturedBody = body
return true
})
.matchHeader('content-type', /application\/x-www-form-urlencoded/)
.once()
.reply(200, { id: 1 })

await strava.pushSubscriptions.create({
'callback_url': 'http://you.com/callback/',
'verify_token': 'node-strava-v3'
})

assert.ok(capturedBody, 'POST body must not be empty')
assert.strictEqual(capturedBody.callback_url, 'http://you.com/callback/')
assert.strictEqual(capturedBody.verify_token, 'node-strava-v3')
assert.strictEqual(capturedBody.client_id, 'test-client-id')
assert.strictEqual(capturedBody.client_secret, 'test-client-secret')
})
})

describe('#delete({id:...})', function () {
Expand Down