File
Blob: src/workerd/api/tests/http-standard-test.js
| 1 | // Copyright (c) 2024 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, rejects, throws } from 'node:assert'; |
| 5 | |
| 6 | export default { |
| 7 | async fetch(req) { |
| 8 | if (req.url.endsWith('/')) { |
| 9 | return new Response(null, { |
| 10 | status: 302, |
| 11 | headers: { |
| 12 | location: ' /\t2 ', |
| 13 | }, |
| 14 | }); |
| 15 | } else if (req.url.endsWith('/2')) { |
| 16 | return new Response('ok'); |
| 17 | } else if (req.url.endsWith('/error')) { |
| 18 | const resp = Response.error(); |
| 19 | strictEqual(resp.type, 'error'); |
| 20 | strictEqual(resp.status, 0); |
| 21 | return resp; |
| 22 | } |
| 23 | }, |
| 24 | |
| 25 | foo() { |
| 26 | return Response.error(); |
| 27 | }, |
| 28 | }; |
| 29 | |
| 30 | export const test = { |
| 31 | async test(ctrl, env) { |
| 32 | const resp = await env.SERVICE.fetch( |
| 33 | ' http://p\tl\na\tc\ne\th\no\tl\nd\te\nr\t/ ' |
| 34 | ); |
| 35 | |
| 36 | console.log(resp.url); |
| 37 | |
| 38 | strictEqual(await resp.text(), 'ok'); |
| 39 | }, |
| 40 | }; |
| 41 | |
| 42 | export const typeTest = { |
| 43 | test() { |
| 44 | const resp = new Response('ok'); |
| 45 | strictEqual(resp.type, 'default'); |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | export const error = { |
| 50 | async test(_, env) { |
| 51 | await rejects(env.SERVICE.fetch('http://example/error'), { |
| 52 | message: |
| 53 | 'Return value from serve handler must not be an error ' + |
| 54 | 'response (like Response.error())', |
| 55 | }); |
| 56 | |
| 57 | const errorResp = Response.error(); |
| 58 | throws(() => new Response('', errorResp), { |
| 59 | message: |
| 60 | 'Responses may only be constructed with status codes in ' + |
| 61 | 'the range 200 to 599, inclusive.', |
| 62 | }); |
| 63 | |
| 64 | const errorRespRpc = await env.SERVICE.foo(null); |
| 65 | strictEqual(errorRespRpc.type, 'error'); |
| 66 | strictEqual(errorRespRpc.ok, false); |
| 67 | }, |
| 68 | }; |
| 69 | |
| 70 | // Test that DNS-JSON content type does not trigger a warning when using text() method |
| 71 | // Reference: https://github.com/cloudflare/workerd/issues/3326 |
| 72 | export const dnsJsonContentType = { |
| 73 | async test() { |
| 74 | // Create a minimal response with application/dns-json mimetype |
| 75 | const dnsJsonResponse = new Response('{"Status":0}', { |
| 76 | headers: { 'Content-Type': 'application/dns-json' }, |
| 77 | }); |
| 78 | |
| 79 | const text = await dnsJsonResponse.text(); |
| 80 | strictEqual(typeof text, 'string', 'Response text should be a string'); |
| 81 | }, |
| 82 | }; |