mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-09 04:22:07 +00:00
[node] add tests to getBodyParser helper (#10109)
This commit is contained in:
55
packages/node/test/unit/serverless-functions/helpers.test.ts
Normal file
55
packages/node/test/unit/serverless-functions/helpers.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { getBodyParser } from '../../../src/serverless-functions/helpers';
|
||||
|
||||
describe('serverless-functions/helpers', () => {
|
||||
describe('getBodyParser', () => {
|
||||
it('content type undefined should return the original string', () => {
|
||||
const rawBody = 'body content';
|
||||
const body = Buffer.from(rawBody);
|
||||
const result = getBodyParser(body, undefined)();
|
||||
expect(result).toBe(rawBody);
|
||||
});
|
||||
|
||||
it('content type "text/plain" should return the original string', () => {
|
||||
const rawBody = 'body content';
|
||||
const body = Buffer.from(rawBody);
|
||||
const result = getBodyParser(body, 'text/plain')();
|
||||
expect(result).toBe(rawBody);
|
||||
});
|
||||
|
||||
it('content type "application/octet-stream" should return the body buffer', () => {
|
||||
const rawBody = 'body content';
|
||||
const body = Buffer.from(rawBody);
|
||||
const result = getBodyParser(body, 'application/octet-stream')();
|
||||
expect(result).toBe(body);
|
||||
});
|
||||
|
||||
it('content type "application/x-www-form-urlencoded" should return the parsed query string', () => {
|
||||
const rawBody = 'foo=bar&baz=zim';
|
||||
const body = Buffer.from(rawBody);
|
||||
|
||||
const result = getBodyParser(body, 'application/x-www-form-urlencoded')();
|
||||
expect(result).toEqual({
|
||||
foo: 'bar',
|
||||
baz: 'zim',
|
||||
});
|
||||
});
|
||||
|
||||
it('content type "application/json" should return the parsed object', () => {
|
||||
const rawBody = '{"foo": "bar", "baz": "zim"}';
|
||||
const body = Buffer.from(rawBody);
|
||||
const result = getBodyParser(body, 'application/json')();
|
||||
expect(result).toEqual({
|
||||
foo: 'bar',
|
||||
baz: 'zim',
|
||||
});
|
||||
});
|
||||
|
||||
it('content type "application/json" should throw when parsing bad json', () => {
|
||||
const rawBody = 'not valid json';
|
||||
const body = Buffer.from(rawBody);
|
||||
expect(() => {
|
||||
getBodyParser(body, 'application/json')();
|
||||
}).toThrow('Invalid JSON');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user