From c265804eaa6b1c2743dbf637fdb6b2b58127a1eb Mon Sep 17 00:00:00 2001 From: mouhannad-sh Date: Sun, 7 Jun 2020 01:05:23 +0800 Subject: [PATCH 1/2] remove request body JSON parsing from worker file --- src/mockServiceWorker.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/mockServiceWorker.js b/src/mockServiceWorker.js index 01b0afaa0..b8e6ed819 100644 --- a/src/mockServiceWorker.js +++ b/src/mockServiceWorker.js @@ -107,9 +107,7 @@ self.addEventListener('fetch', async function (event) { } const reqHeaders = serializeHeaders(request.headers) - const body = request.headers.get('content-type')?.includes('json') - ? await request.json() - : await request.text() + const body = await request.text() const rawClientMessage = await sendToClient(client, { type: 'REQUEST', From ab611f369ea4deb30f849f15df5d604827ebf72c Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Thu, 11 Jun 2020 22:07:00 +0200 Subject: [PATCH 2/2] Adds integration test for a GET request and "Content-Type: application/json" --- test/rest-api/body.test.ts | 172 ++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 89 deletions(-) diff --git a/test/rest-api/body.test.ts b/test/rest-api/body.test.ts index c2c2724de..3c1a8d928 100644 --- a/test/rest-api/body.test.ts +++ b/test/rest-api/body.test.ts @@ -1,98 +1,92 @@ import * as path from 'path' -import { TestAPI, runBrowserWith } from '../support/runBrowserWith' +import { runBrowserWith } from '../support/runBrowserWith' -describe('REST: Request body', () => { - let test: TestAPI +function createRuntime() { + return runBrowserWith(path.resolve(__dirname, 'body.mocks.ts')) +} - beforeAll(async () => { - test = await runBrowserWith(path.resolve(__dirname, 'body.mocks.ts')) +test('handles a GET request without a body', async () => { + const runtime = await createRuntime() + + const res = await runtime.request({ + url: `${runtime.origin}/login`, }) + const body = await res.json() + expect(body).toEqual({ body: undefined }) + + await runtime.cleanup() +}) - afterAll(() => { - return test.cleanup() +test('handles a GET request without a body and "Content-Type: application/json" header', async () => { + const runtime = await createRuntime() + + const res = await runtime.request({ + url: `${runtime.origin}/login`, + fetchOptions: { + headers: { + 'Content-Type': 'application/json', + }, + }, }) + const body = await res.json() + expect(body).toEqual({ body: undefined }) + + await runtime.cleanup() +}) + +test('handles a POST request with an explicit empty body', async () => { + const runtime = await createRuntime() - describe('when I reference "req.body" inside a request handler', () => { - describe('and I performed a GET request without a body', () => { - it('should not return any request body', async () => { - const res = await test.request({ - url: `${test.origin}/login`, - }) - const body = await res.json() - - expect(body).toEqual({ body: undefined }) - }) - }) - - describe('and I performed a POST request with intentionally empty body', () => { - it('should return the request body as-is', async () => { - const res = await test.request({ - url: `${test.origin}/login`, - fetchOptions: { - method: 'POST', - body: '', - }, - }) - const body = await res.json() - - expect(body).toEqual({ body: '' }) - }) - }) - - describe('and I performed a POST request with a text body', () => { - it('should return a text request body as-is', async () => { - const res = await test.request({ - url: `${test.origin}/login`, - fetchOptions: { - method: 'POST', - body: 'text-body', - }, - }) - const body = await res.json() - - expect(body).toEqual({ body: 'text-body' }) - }) - }) - - describe('and I performed a POST request with a JSON body without any "Content-Type" header', () => { - it('should return a text request body as-is', async () => { - const res = await test.request({ - url: `${test.origin}/login`, - fetchOptions: { - method: 'POST', - body: JSON.stringify({ - json: 'body', - }), - }, - }) - const body = await res.text() - - expect(body).toEqual(`{"body":"{\\"json\\":\\"body\\"}"}`) - }) - }) - - describe('and I performed a POST request with a JSON body with a "Content-Type" header', () => { - it('should return a text request body as-is', async () => { - const res = await test.request({ - url: `${test.origin}/login`, - fetchOptions: { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - json: 'body', - }), - }, - }) - const body = await res.json() - - expect(body).toEqual({ - body: { - json: 'body', - }, - }) - }) - }) + const res = await runtime.request({ + url: `${runtime.origin}/login`, + fetchOptions: { + method: 'POST', + body: '', + }, }) + const body = await res.json() + expect(body).toEqual({ body: '' }) + + await runtime.cleanup() +}) + +test('handles a POST request with a textual body', async () => { + const runtime = await createRuntime() + + const res = await runtime.request({ + url: `${runtime.origin}/login`, + fetchOptions: { + method: 'POST', + body: 'text-body', + }, + }) + const body = await res.json() + expect(body).toEqual({ body: 'text-body' }) + + await runtime.cleanup() +}) + +test('handles a POST request with a JSON body and "Content-Type: application/json" header', async () => { + const runtime = await createRuntime() + + const res = await runtime.request({ + url: `${runtime.origin}/login`, + fetchOptions: { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + json: 'body', + }), + }, + }) + const body = await res.json() + expect(body).toEqual({ + body: { + json: 'body', + }, + }) + + await runtime.cleanup() })