|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + ClientHttp2Session, |
| 11 | + Http2Server, |
| 12 | + Http2ServerRequest, |
| 13 | + Http2ServerResponse, |
| 14 | + connect, |
| 15 | + createServer, |
| 16 | +} from 'node:http2'; |
| 17 | +import { AddressInfo } from 'node:net'; |
| 18 | +import { createWebRequestFromNodeRequest } from '../src/request'; |
| 19 | + |
| 20 | +describe('createWebRequestFromNodeRequest (HTTP/2)', () => { |
| 21 | + let server: Http2Server; |
| 22 | + let port: number; |
| 23 | + let client: ClientHttp2Session; |
| 24 | + |
| 25 | + function extractNodeRequest(makeRequest: () => void): Promise<Http2ServerRequest> { |
| 26 | + const nodeRequest = getNodeRequest(); |
| 27 | + makeRequest(); |
| 28 | + |
| 29 | + return nodeRequest; |
| 30 | + } |
| 31 | + |
| 32 | + async function getNodeRequest(): Promise<Http2ServerRequest> { |
| 33 | + const { req, res } = await new Promise<{ |
| 34 | + req: Http2ServerRequest; |
| 35 | + res: Http2ServerResponse<Http2ServerRequest>; |
| 36 | + }>((resolve) => { |
| 37 | + server.once('request', (req, res) => resolve({ req, res })); |
| 38 | + }); |
| 39 | + |
| 40 | + res.end(); |
| 41 | + |
| 42 | + return req; |
| 43 | + } |
| 44 | + |
| 45 | + beforeAll((done) => { |
| 46 | + server = createServer(); |
| 47 | + server.listen(0, () => { |
| 48 | + port = (server.address() as AddressInfo).port; |
| 49 | + done(); |
| 50 | + client = connect(`http://localhost:${port}`); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + afterAll((done) => { |
| 55 | + client.close(); |
| 56 | + server.close(done); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('GET Handling', () => { |
| 60 | + it('should correctly handle a basic GET request', async () => { |
| 61 | + const nodeRequest = await extractNodeRequest(() => { |
| 62 | + client |
| 63 | + .request({ |
| 64 | + ':path': '/basic-get', |
| 65 | + ':method': 'GET', |
| 66 | + }) |
| 67 | + .end(); |
| 68 | + }); |
| 69 | + |
| 70 | + const webRequest = createWebRequestFromNodeRequest(nodeRequest); |
| 71 | + expect(webRequest.method).toBe('GET'); |
| 72 | + expect(webRequest.url).toBe(`http://localhost:${port}/basic-get`); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should correctly handle GET request with query parameters', async () => { |
| 76 | + const nodeRequest = await extractNodeRequest(() => { |
| 77 | + client |
| 78 | + .request({ |
| 79 | + ':scheme': 'http', |
| 80 | + ':path': '/search?query=hello&page=2', |
| 81 | + ':method': 'POST', |
| 82 | + }) |
| 83 | + .end(); |
| 84 | + }); |
| 85 | + |
| 86 | + const webRequest = createWebRequestFromNodeRequest(nodeRequest); |
| 87 | + expect(webRequest.method).toBe('POST'); |
| 88 | + expect(webRequest.url).toBe(`http://localhost:${port}/search?query=hello&page=2`); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should correctly handle GET request with custom headers', async () => { |
| 92 | + const nodeRequest = await extractNodeRequest(() => { |
| 93 | + client |
| 94 | + .request({ |
| 95 | + ':path': '/with-headers', |
| 96 | + ':method': 'GET', |
| 97 | + 'X-Custom-Header1': 'value1', |
| 98 | + 'X-Custom-Header2': 'value2', |
| 99 | + }) |
| 100 | + .end(); |
| 101 | + }); |
| 102 | + |
| 103 | + const webRequest = createWebRequestFromNodeRequest(nodeRequest); |
| 104 | + expect(webRequest.method).toBe('GET'); |
| 105 | + expect(webRequest.url).toBe(`http://localhost:${port}/with-headers`); |
| 106 | + expect(webRequest.headers.get('x-custom-header1')).toBe('value1'); |
| 107 | + expect(webRequest.headers.get('x-custom-header2')).toBe('value2'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('POST Handling', () => { |
| 112 | + it('should handle POST request with JSON body and correct response', async () => { |
| 113 | + const postData = JSON.stringify({ message: 'Hello from POST' }); |
| 114 | + const nodeRequest = await extractNodeRequest(() => { |
| 115 | + const clientRequest = client.request({ |
| 116 | + ':path': '/post-json', |
| 117 | + ':method': 'POST', |
| 118 | + 'Content-Type': 'application/json', |
| 119 | + 'Content-Length': Buffer.byteLength(postData), |
| 120 | + }); |
| 121 | + clientRequest.write(postData); |
| 122 | + clientRequest.end(); |
| 123 | + }); |
| 124 | + |
| 125 | + const webRequest = createWebRequestFromNodeRequest(nodeRequest); |
| 126 | + expect(webRequest.method).toBe('POST'); |
| 127 | + expect(webRequest.url).toBe(`http://localhost:${port}/post-json`); |
| 128 | + expect(webRequest.headers.get('content-type')).toBe('application/json'); |
| 129 | + expect(await webRequest.json()).toEqual({ message: 'Hello from POST' }); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should handle POST request with empty text body', async () => { |
| 133 | + const postData = ''; |
| 134 | + const nodeRequest = await extractNodeRequest(() => { |
| 135 | + const clientRequest = client.request({ |
| 136 | + ':path': '/post-text', |
| 137 | + ':method': 'POST', |
| 138 | + 'Content-Type': 'text/plain', |
| 139 | + 'Content-Length': Buffer.byteLength(postData), |
| 140 | + }); |
| 141 | + clientRequest.write(postData); |
| 142 | + clientRequest.end(); |
| 143 | + }); |
| 144 | + |
| 145 | + const webRequest = createWebRequestFromNodeRequest(nodeRequest); |
| 146 | + expect(webRequest.method).toBe('POST'); |
| 147 | + expect(webRequest.url).toBe(`http://localhost:${port}/post-text`); |
| 148 | + expect(webRequest.headers.get('content-type')).toBe('text/plain'); |
| 149 | + expect(await webRequest.text()).toBe(''); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments