File
Blob: src/workerd/api/node/tests/http-incoming-nodejs-test.js
| 1 | // Copyright (c) 2025 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 http from 'node:http'; |
| 5 | import { deepStrictEqual, strictEqual } from 'node:assert'; |
| 6 | |
| 7 | // Test is taken from test/parallel/test-http-incoming-matchKnownFields.js |
| 8 | export const testHttpIncomingMatchKnownFields = { |
| 9 | async test() { |
| 10 | function checkDest(field, result, value) { |
| 11 | const dest = {}; |
| 12 | |
| 13 | const incomingMessage = new http.IncomingMessage(field); |
| 14 | // Dest is changed by IncomingMessage._addHeaderLine |
| 15 | if (value) incomingMessage._addHeaderLine(field, 'test', dest); |
| 16 | incomingMessage._addHeaderLine(field, value, dest); |
| 17 | deepStrictEqual(dest, result); |
| 18 | } |
| 19 | |
| 20 | checkDest('', { '': undefined }); |
| 21 | checkDest('Content-Type', { 'content-type': undefined }); |
| 22 | checkDest('content-type', { 'content-type': 'test' }, 'value'); |
| 23 | checkDest('User-Agent', { 'user-agent': undefined }); |
| 24 | checkDest('user-agent', { 'user-agent': 'test' }, 'value'); |
| 25 | checkDest('Referer', { referer: undefined }); |
| 26 | checkDest('referer', { referer: 'test' }, 'value'); |
| 27 | checkDest('Host', { host: undefined }); |
| 28 | checkDest('host', { host: 'test' }, 'value'); |
| 29 | checkDest('Authorization', { authorization: undefined }, undefined); |
| 30 | checkDest('authorization', { authorization: 'test' }, 'value'); |
| 31 | checkDest('Proxy-Authorization', { 'proxy-authorization': undefined }); |
| 32 | checkDest( |
| 33 | 'proxy-authorization', |
| 34 | { 'proxy-authorization': 'test' }, |
| 35 | 'value' |
| 36 | ); |
| 37 | checkDest('If-Modified-Since', { 'if-modified-since': undefined }); |
| 38 | checkDest('if-modified-since', { 'if-modified-since': 'test' }, 'value'); |
| 39 | checkDest('If-Unmodified-Since', { 'if-unmodified-since': undefined }); |
| 40 | checkDest( |
| 41 | 'if-unmodified-since', |
| 42 | { 'if-unmodified-since': 'test' }, |
| 43 | 'value' |
| 44 | ); |
| 45 | checkDest('Form', { form: undefined }); |
| 46 | checkDest('form', { form: 'test, value' }, 'value'); |
| 47 | checkDest('Location', { location: undefined }); |
| 48 | checkDest('location', { location: 'test' }, 'value'); |
| 49 | checkDest('Max-Forwards', { 'max-forwards': undefined }); |
| 50 | checkDest('max-forwards', { 'max-forwards': 'test' }, 'value'); |
| 51 | checkDest('Retry-After', { 'retry-after': undefined }); |
| 52 | checkDest('retry-after', { 'retry-after': 'test' }, 'value'); |
| 53 | checkDest('Etag', { etag: undefined }); |
| 54 | checkDest('etag', { etag: 'test' }, 'value'); |
| 55 | checkDest('Last-Modified', { 'last-modified': undefined }); |
| 56 | checkDest('last-modified', { 'last-modified': 'test' }, 'value'); |
| 57 | checkDest('Server', { server: undefined }); |
| 58 | checkDest('server', { server: 'test' }, 'value'); |
| 59 | checkDest('Age', { age: undefined }); |
| 60 | checkDest('age', { age: 'test' }, 'value'); |
| 61 | checkDest('Expires', { expires: undefined }); |
| 62 | checkDest('expires', { expires: 'test' }, 'value'); |
| 63 | checkDest('Set-Cookie', { 'set-cookie': [undefined] }); |
| 64 | checkDest('set-cookie', { 'set-cookie': ['test', 'value'] }, 'value'); |
| 65 | checkDest('Transfer-Encoding', { 'transfer-encoding': undefined }); |
| 66 | checkDest( |
| 67 | 'transfer-encoding', |
| 68 | { 'transfer-encoding': 'test, value' }, |
| 69 | 'value' |
| 70 | ); |
| 71 | checkDest('Date', { date: undefined }); |
| 72 | checkDest('date', { date: 'test, value' }, 'value'); |
| 73 | checkDest('Connection', { connection: undefined }); |
| 74 | checkDest('connection', { connection: 'test, value' }, 'value'); |
| 75 | checkDest('Cache-Control', { 'cache-control': undefined }); |
| 76 | checkDest('cache-control', { 'cache-control': 'test, value' }, 'value'); |
| 77 | checkDest('Transfer-Encoding', { 'transfer-encoding': undefined }); |
| 78 | checkDest( |
| 79 | 'transfer-encoding', |
| 80 | { 'transfer-encoding': 'test, value' }, |
| 81 | 'value' |
| 82 | ); |
| 83 | checkDest('Vary', { vary: undefined }); |
| 84 | checkDest('vary', { vary: 'test, value' }, 'value'); |
| 85 | checkDest('Content-Encoding', { 'content-encoding': undefined }, undefined); |
| 86 | checkDest( |
| 87 | 'content-encoding', |
| 88 | { 'content-encoding': 'test, value' }, |
| 89 | 'value' |
| 90 | ); |
| 91 | checkDest('Cookie', { cookie: undefined }); |
| 92 | checkDest('cookie', { cookie: 'test; value' }, 'value'); |
| 93 | checkDest('Origin', { origin: undefined }); |
| 94 | checkDest('origin', { origin: 'test, value' }, 'value'); |
| 95 | checkDest('Upgrade', { upgrade: undefined }); |
| 96 | checkDest('upgrade', { upgrade: 'test, value' }, 'value'); |
| 97 | checkDest('Expect', { expect: undefined }); |
| 98 | checkDest('expect', { expect: 'test, value' }, 'value'); |
| 99 | checkDest('If-Match', { 'if-match': undefined }); |
| 100 | checkDest('if-match', { 'if-match': 'test, value' }, 'value'); |
| 101 | checkDest('If-None-Match', { 'if-none-match': undefined }); |
| 102 | checkDest('if-none-match', { 'if-none-match': 'test, value' }, 'value'); |
| 103 | checkDest('Accept', { accept: undefined }); |
| 104 | checkDest('accept', { accept: 'test, value' }, 'value'); |
| 105 | checkDest('Accept-Encoding', { 'accept-encoding': undefined }); |
| 106 | checkDest('accept-encoding', { 'accept-encoding': 'test, value' }, 'value'); |
| 107 | checkDest('Accept-Language', { 'accept-language': undefined }); |
| 108 | checkDest('accept-language', { 'accept-language': 'test, value' }, 'value'); |
| 109 | checkDest('X-Forwarded-For', { 'x-forwarded-for': undefined }); |
| 110 | checkDest('x-forwarded-for', { 'x-forwarded-for': 'test, value' }, 'value'); |
| 111 | checkDest('X-Forwarded-Host', { 'x-forwarded-host': undefined }); |
| 112 | checkDest( |
| 113 | 'x-forwarded-host', |
| 114 | { 'x-forwarded-host': 'test, value' }, |
| 115 | 'value' |
| 116 | ); |
| 117 | checkDest('X-Forwarded-Proto', { 'x-forwarded-proto': undefined }); |
| 118 | checkDest( |
| 119 | 'x-forwarded-proto', |
| 120 | { 'x-forwarded-proto': 'test, value' }, |
| 121 | 'value' |
| 122 | ); |
| 123 | checkDest('X-Foo', { 'x-foo': undefined }); |
| 124 | checkDest('x-foo', { 'x-foo': 'test, value' }, 'value'); |
| 125 | }, |
| 126 | }; |
| 127 | |
| 128 | // Test is taken from test/parallel/test-http-incoming-message-connection-setter.js |
| 129 | export const testHttpIncomingMessageConnectionSetter = { |
| 130 | async test() { |
| 131 | const incomingMessage = new http.IncomingMessage(); |
| 132 | |
| 133 | strictEqual(incomingMessage.connection, undefined); |
| 134 | strictEqual(incomingMessage.socket, undefined); |
| 135 | |
| 136 | incomingMessage.connection = 'fhqwhgads'; |
| 137 | |
| 138 | strictEqual(incomingMessage.connection, 'fhqwhgads'); |
| 139 | strictEqual(incomingMessage.socket, 'fhqwhgads'); |
| 140 | }, |
| 141 | }; |
| 142 | |
| 143 | // Test is taken from test/parallel/test-http-incoming-message-destroy.js |
| 144 | export const testHttpIncomingMessageOptions = { |
| 145 | async test() { |
| 146 | const incomingMessage = new http.IncomingMessage(); |
| 147 | |
| 148 | strictEqual(incomingMessage.destroy(), incomingMessage); |
| 149 | }, |
| 150 | }; |
| 151 | |
| 152 | // Node.js tests |
| 153 | // - [x] test/parallel/test-http-incoming-matchKnownFields.js |
| 154 | // - [x] test/parallel/test-http-incoming-message-connection-setter.js |
| 155 | // - [x] test/parallel/test-http-incoming-message-destroy.js |
| 156 | // - [x] test/parallel/test-http-incoming-message-options.js |
| 157 | // - [ ] test/parallel/test-http-incoming-pipelined-socket-destroy.js |
| 158 | // - [ ] test/parallel/test-http-incoming-message-options.js |
| 159 | // - [ ] test/parallel/test-http-incoming-pipelined-socket-destroy.js |