Skip to content
File

Blob: src/workerd/api/node/tests/http-client-nodejs-server.js

javascript135 lines
1// Copyright (c) 2017-2022 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 
5// This is a sidecar that runs alongside the http-client-nodejs-test.* tests.
6// It is executed using the appropriate Node.js version defined in build/deps/nodejs.MODULE.bazel.
7const http = require('node:http');
8const assert = require('node:assert/strict');
9const zlib = require('node:zlib');
10 
11function listenTo(server, port) {
12 server.listen(port, process.env.SIDECAR_HOSTNAME, () => {
13 const address = server.address();
14 console.info(`Listening on ${address.address}:${address.port}`);
15 });
16}
17 
18const pongServer = http.createServer((req, res) => {
19 req.resume();
20 req.on('end', () => {
21 res.writeHead(200);
22 res.end('pong');
23 });
24});
25 
26listenTo(pongServer, process.env.PONG_SERVER_PORT);
27 
28const asdServer = http.createServer((_req, res) => {
29 res.end('asd');
30});
31 
32listenTo(asdServer, process.env.ASD_SERVER_PORT);
33 
34{
35 const expectedHeaders = {
36 DELETE: ['content-length', 'host'],
37 GET: ['host'],
38 HEAD: ['host'],
39 OPTIONS: ['content-length', 'host'],
40 POST: ['content-length', 'host'],
41 PUT: ['content-length', 'host'],
42 TRACE: ['content-length', 'host'],
43 };
44 
45 const defaultHeadersExistServer = http.createServer((req, res) => {
46 res.end();
47 
48 assert(
49 Object.hasOwn(expectedHeaders, req.method),
50 `${req.method} was an unexpected method`
51 );
52 
53 const requestHeaders = Object.keys(req.headers);
54 for (const header of requestHeaders) {
55 assert.ok(
56 expectedHeaders[req.method].includes(header.toLowerCase()),
57 `${header} should not exist for method ${req.method}`
58 );
59 }
60 
61 assert.deepStrictEqual(
62 requestHeaders,
63 expectedHeaders[req.method],
64 `Unexpected headers for method ${req.method}`
65 );
66 });
67 
68 listenTo(defaultHeadersExistServer, process.env.DEFAULT_HEADERS_EXIST_PORT);
69}
70 
71const requestArgumentsServer = http.createServer((req, res) => {
72 assert.strictEqual(req.url, '/testpath');
73 res.end();
74});
75 
76listenTo(requestArgumentsServer, process.env.REQUEST_ARGUMENTS_PORT);
77 
78const helloWorldServer = http.createServer((req, res) => {
79 res.removeHeader('Date');
80 res.setHeader('Keep-Alive', 'timeout=1');
81 
82 switch (req.url.slice(1).split('?').at(0)) {
83 case 'join-duplicate-headers': {
84 res.writeHead(200, [
85 'authorization',
86 '3',
87 'authorization',
88 '4',
89 'cookie',
90 'foo',
91 'cookie',
92 'bar',
93 ]);
94 res.end();
95 break;
96 }
97 
98 case 'search-path': {
99 res.writeHead(200, { 'Content-Type': 'text/plain', url: req.url });
100 res.end('Hello, World!');
101 break;
102 }
103 
104 case 'echo': {
105 // Echo the request body back as the response
106 let body = '';
107 req.on('data', (chunk) => {
108 body += chunk.toString();
109 });
110 req.on('end', () => {
111 res.writeHead(200, { 'Content-Type': 'text/plain' });
112 res.end(body);
113 });
114 break;
115 }
116 
117 default: {
118 res.end();
119 }
120 }
121});
122 
123listenTo(helloWorldServer, process.env.HELLO_WORLD_SERVER_PORT);
124 
125const gzipServer = http.createServer((_req, res) => {
126 const body = zlib.gzipSync(Buffer.from('hello from gzip server'));
127 res.writeHead(200, {
128 'Content-Encoding': 'gzip',
129 'Content-Type': 'text/plain',
130 });
131 res.end(body);
132});
133 
134listenTo(gzipServer, process.env.GZIP_SERVER_PORT);