Skip to content

Commit

Permalink
0.15.4 - set body to empty string (#479)
Browse files Browse the repository at this point in the history
* set body to empty string

* 0.15.4

* 0.15.4-dev.1

* 0.15.4
  • Loading branch information
mapsam authored Mar 12, 2024
1 parent d8ce98b commit af46a7d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 0.15.4

- **Fix:** Set `body = ''` for POST, PUT, PATCH, and DELETE requests that do not have a body [#479](https://github.com/mapbox/mapbox-sdk-js/pull/479)

## 0.15.3

- **Fix:** Maximum Optimization V1 request size limits should not be enforced client-side
Expand Down
7 changes: 7 additions & 0 deletions lib/node/node-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ function createRequestStreams(request) {
gotOptions.body = JSON.stringify(request.body);
}

if (
['POST', 'PUT', 'PATCH', 'DELETE'].includes(request.method) &&
!request.body
) {
gotOptions.body = '';
}

var gotStream = got.stream(url, gotOptions);

gotStream.setEncoding(request.encoding);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mapbox/mapbox-sdk",
"version": "0.15.3",
"version": "0.15.4",
"description": "JS SDK for accessing Mapbox APIs",
"main": "index.js",
"files": [
Expand Down
43 changes: 43 additions & 0 deletions test/node-interface.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,46 @@ describe('test node progress events', () => {
});
});
});

describe('test empty request bodies', () => {
let server;
let createLocalClient;
const { mockToken } = tu;

beforeAll(() => {
return tu.mockServer().then(s => {
server = s;
createLocalClient = server.localClient(nodeClient);
});
});

afterAll(done => {
server.close(done);
});

afterEach(() => {
server.reset();
});

beforeEach(() => {
server.setResponse((req, res) => {
res.append('Content-Type', 'application/json; charset=utf-8');
res.json({ mockStyle: true });
});
});

test('request for POST, PATCH, PUT, or DELETE without an explicit body should include empty string as body', () => {
// https://github.com/sindresorhus/got/issues/2303
const accessToken = mockToken();
const client = createLocalClient({ accessToken });
const request = client.createRequest({
method: 'DELETE',
path: '/tilesets/v1/:tilesetId',
params: { tilesetId: 'foo.bar' }
});

return request.send().then(({ statusCode }) => {
expect(statusCode).toBe(200);
});
});
});

0 comments on commit af46a7d

Please sign in to comment.