Skip to content

fix: don't require trailing line break on terminal boundary #53

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
40 changes: 38 additions & 2 deletions src/__test__/PatchResolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

function getMultiPartResponse(data, boundary) {
function getMultiPartResponse(data, boundary, includeTrailingLineBreak = true) {
const json = JSON.stringify(data);
const maybeLineBreak = includeTrailingLineBreak ? '\r\n' : '';

return ['Content-Type: application/json', '', json, `--${boundary}\r\n`].join('\r\n');
return ['Content-Type: application/json', '', json, `--${boundary}${maybeLineBreak}`].join(
'\r\n'
);
}

describe('PathResolver', function () {
Expand Down Expand Up @@ -188,6 +191,39 @@ describe('PathResolver', function () {
resolver.handleChunk(chunk2b);
expect(onResponse.mock.calls[0][0]).toEqual([chunk2Data]);
});

it('should work when final chunk ends with terminating boundary and no line break', function () {
const onResponse = jest.fn();
const resolver = new PatchResolver({
onResponse,
boundary,
});

resolver.handleChunk(`\r\n--${boundary}\r\n`);

expect(onResponse).not.toHaveBeenCalled();

resolver.handleChunk(chunk1);
expect(onResponse.mock.calls[0][0]).toEqual([chunk1Data]);

onResponse.mockClear();
resolver.handleChunk(chunk2);
expect(onResponse.mock.calls[0][0]).toEqual([chunk2Data]);

onResponse.mockClear();
resolver.handleChunk(chunk3);
expect(onResponse.mock.calls[0][0]).toEqual([chunk3Data]);

onResponse.mockClear();
const chunk4FinalBoundary = getMultiPartResponse(
chunk4Data,
`${boundary}--`,
false
);
resolver.handleChunk(chunk4FinalBoundary);
expect(onResponse.mock.calls[0][0]).toEqual([chunk4Data]);
});

it('should work when final chunk ends with terminating boundary', function () {
const onResponse = jest.fn();
const resolver = new PatchResolver({
Expand Down
2 changes: 1 addition & 1 deletion src/parseMultipartHttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function getDelimiter(boundary) {
}

function getClosingDelimiter(boundary) {
return `\r\n--${boundary}--\r\n`;
return `\r\n--${boundary}--`;
}

function splitWithRest(string, delim) {
Expand Down