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

set body to empty string #479

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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-dev.1",
"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);
});
});
});
Loading