Skip to content

Commit

Permalink
Merge pull request #31 from joaoneto/test/improve-coverage
Browse files Browse the repository at this point in the history
test: improve tests coverage
  • Loading branch information
joaoneto committed Mar 8, 2024
2 parents c19aa0e + 7c3a80a commit f3faa98
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/small-eggs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'guarapi': patch
---

improve tests coverage
change bodyparser plugin content-length handler
rename plugin to correct name bodyParser
6 changes: 2 additions & 4 deletions packages/guarapi/src/plugins/body-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ const MAX_PAYLOAD_SIZE_BYTES = 2097152;
const bodyParserPlugin: Plugin = (_app, config) => {
const { maxPayloadSize = MAX_PAYLOAD_SIZE_BYTES } = config || {};
return {
name: 'formUrlencodedParser',
name: 'bodyParser',
pre: async (req, res, next) => {
const contentType = req.headers['content-type'];
const contentLength = req.headers['content-length']
? parseInt(req.headers['content-length'], 10)
: 0;
const contentLength = parseInt(req.headers['content-length']!, 10);

if (contentLength > maxPayloadSize) {
next(new Error(`Max payload ${maxPayloadSize}`));
Expand Down
88 changes: 85 additions & 3 deletions packages/guarapi/test/plugins/body-parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import request from 'supertest';
import guarapi, {
GuarapiConfig,
MiddlewareError,
bodyParserPlugin,
createServer,
middlewarePlugin,
} from '../../src';

describe('Guarapi - plugins/body-parser', () => {
const buildApp = () => {
const app = guarapi({ maxPayloadSize: 1000 });
const buildApp = (config?: GuarapiConfig) => {
const app = guarapi(config);
const server = createServer({}, app);

app.plugin(bodyParserPlugin);
Expand Down Expand Up @@ -111,6 +112,58 @@ describe('Guarapi - plugins/body-parser', () => {
});
});

it('should parse x-www-form-urlencoded with valid array data', async () => {
const { app, server } = buildApp();
const body = jest.fn();
const maliciousPayload = 'item=1&item&&item=2';

app.use((req, res) => {
body(req.body);
res.end();
});

await request(server)
.post('/')
.set('Content-type', 'application/x-www-form-urlencoded')
.send(maliciousPayload)
.expect(200);

expect(body).toBeCalledWith({ item: ['1', '', '2'] });
});

it('should parse x-www-form-urlencoded with valid deep object data', async () => {
const { app, server } = buildApp();
const body = jest.fn();
const maliciousPayload =
'deep.obj.a=1&deep.obj[b]=2&deep.obj.c.1.2=10&deep.obj.c.1.2=20&deep.obj[]=1000';

app.use((req, res) => {
body(req.body);
res.end();
});

await request(server)
.post('/')
.set('Content-type', 'application/x-www-form-urlencoded')
.send(maliciousPayload)
.expect(200);

expect(body).toBeCalledWith({
deep: {
obj: {
a: '1',
b: '2',
c: {
1: {
2: ['10', '20'],
},
},
3: '1000',
},
},
});
});

it('should handle x-www-form-urlencoded with invalid UTF-8 characters', async () => {
const { app, server } = buildApp();
const body = jest.fn();
Expand Down Expand Up @@ -157,7 +210,7 @@ describe('Guarapi - plugins/body-parser', () => {
});

it('should handle x-www-form-urlencoded with payload overflow', async () => {
const { app, server } = buildApp();
const { app, server } = buildApp({ maxPayloadSize: 1000 });
const bodyHandler = jest.fn();
const errorHandler = jest.fn();

Expand All @@ -184,4 +237,33 @@ describe('Guarapi - plugins/body-parser', () => {
expect(errorHandler).toBeCalled();
expect(response.text).toBe('Payload too large');
});

it('should handle x-www-form-urlencoded with empty payload', async () => {
const { app, server } = buildApp();
const bodyHandler = jest.fn();
const errorHandler = jest.fn();

const noPayload = '';
const expectedEmptyParsedPayload = {};

app.use((req, res) => {
bodyHandler(req.body);
res.end();
});

app.use<MiddlewareError>((error, req, res, _next) => {
errorHandler();
res.status(400);
res.end();
});

await request(server)
.post('/')
.set('Content-type', 'application/x-www-form-urlencoded')
.send(noPayload)
.expect(200);

expect(bodyHandler).toBeCalledWith(expectedEmptyParsedPayload);
expect(errorHandler).not.toBeCalled();
});
});

0 comments on commit f3faa98

Please sign in to comment.