File
Blob: src/workerd/api/tests/response-json.js
| 1 | // Copyright (c) 2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import { strictEqual, ok, deepStrictEqual, throws } from 'node:assert'; |
| 5 | |
| 6 | export const basics = { |
| 7 | async test() { |
| 8 | const response = Response.json({ a: 1 }); |
| 9 | ok(response instanceof Response); |
| 10 | strictEqual(response.headers.get('content-type'), 'application/json'); |
| 11 | strictEqual(await response.text(), '{"a":1}'); |
| 12 | }, |
| 13 | }; |
| 14 | |
| 15 | export const headers = { |
| 16 | async test() { |
| 17 | const response = Response.json( |
| 18 | { a: 1 }, |
| 19 | { |
| 20 | headers: [['content-type', 'abc']], |
| 21 | } |
| 22 | ); |
| 23 | ok(response instanceof Response); |
| 24 | strictEqual(response.headers.get('content-type'), 'abc'); |
| 25 | deepStrictEqual(await response.json(), { a: 1 }); |
| 26 | }, |
| 27 | }; |
| 28 | |
| 29 | export const headers2 = { |
| 30 | async test() { |
| 31 | const headers = new Headers(); |
| 32 | headers.set('content-type', 'abc'); |
| 33 | const response = Response.json({ a: 1 }, { headers }); |
| 34 | ok(response instanceof Response); |
| 35 | strictEqual(response.headers.get('content-type'), 'abc'); |
| 36 | deepStrictEqual(await response.json(), { a: 1 }); |
| 37 | }, |
| 38 | }; |
| 39 | |
| 40 | export const headers3 = { |
| 41 | async test() { |
| 42 | const other = new Response(); |
| 43 | other.headers.set('content-type', 'abc'); |
| 44 | const response = Response.json({ a: 1 }, other); |
| 45 | ok(response instanceof Response); |
| 46 | strictEqual(response.headers.get('content-type'), 'abc'); |
| 47 | deepStrictEqual(await response.json(), { a: 1 }); |
| 48 | }, |
| 49 | }; |
| 50 | |
| 51 | export const headers4 = { |
| 52 | async test() { |
| 53 | const response = Response.json({ a: 1 }, {}); |
| 54 | ok(response instanceof Response); |
| 55 | strictEqual(response.headers.get('content-type'), 'application/json'); |
| 56 | deepStrictEqual(await response.json(), { a: 1 }); |
| 57 | }, |
| 58 | }; |
| 59 | |
| 60 | export const notJsonSerializable = { |
| 61 | async test() { |
| 62 | // Some values are not JSON serializable... |
| 63 | throws(() => Response.json({ a: 1n })); |
| 64 | }, |
| 65 | }; |