Skip to content
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

Remove request json parsing from the worker file #201

Merged
merged 4 commits into from
Jun 11, 2020
Merged
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
4 changes: 1 addition & 3 deletions src/mockServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
172 changes: 83 additions & 89 deletions test/rest-api/body.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})