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
9 changes: 8 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ export function getServer(
limit: requestLimit,
verify: rawBodySaver,
};
// Parse structured syntax suffix types such as application/vnd.api+json as
// JSON too (RFC 6839). application/cloudevents+json is matched by the
// CloudEvents parser first.
const jsonBodySavingOptions = {
...defaultBodySavingOptions,
type: ['application/json', 'application/*+json'],
};
const cloudEventsBodySavingOptions = {
type: 'application/cloudevents+json',
limit: requestLimit,
Expand All @@ -89,7 +96,7 @@ export function getServer(

// Apply middleware
app.use(bodyParser.json(cloudEventsBodySavingOptions));
app.use(bodyParser.json(defaultBodySavingOptions));
app.use(bodyParser.json(jsonBodySavingOptions));
app.use(bodyParser.text(defaultBodySavingOptions));
app.use(bodyParser.urlencoded(urlEncodedOptions));
// The parser will process ALL content types so MUST come last.
Expand Down
14 changes: 14 additions & 0 deletions test/integration/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,18 @@ describe('HTTP Function', () => {
assert.strictEqual(callCount, test.expectedCallCount);
});
});

['application/vnd.api+json', 'application/merge-patch+json'].forEach(
contentType => {
it(`parses a ${contentType} body as JSON`, async () => {
const response = await supertest(getTestServer('testHttpFunction'))
.post('/')
.set('Content-Type', contentType)
.send(JSON.stringify({text: 'hello'}));

assert.deepStrictEqual(response.body, {result: 'hello'});
assert.strictEqual(response.status, 200);
});
},
);
});
Loading