Skip to content
File

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

javascript112 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');
9 
10function reportPort(server) {
11 const address = server.address();
12 console.info(`Listening on ${address.address}:${address.port}`);
13}
14 
15const pongServer = http.createServer((req, res) => {
16 req.resume();
17 req.on('end', () => {
18 res.writeHead(200);
19 res.end('pong');
20 });
21});
22pongServer.listen(
23 process.env.PONG_SERVER_PORT,
24 process.env.SIDECAR_HOSTNAME,
25 () => reportPort(pongServer)
26);
27 
28const asdServer = http.createServer((_req, res) => {
29 res.end('asd');
30});
31asdServer.listen(
32 process.env.ASD_SERVER_PORT,
33 process.env.SIDECAR_HOSTNAME,
34 () => reportPort(asdServer)
35);
36 
37const timeoutServer = http.createServer((_req, res) => {
38 setTimeout(() => {
39 res.end('pong');
40 }, 1000);
41});
42timeoutServer.listen(
43 process.env.TIMEOUT_SERVER_PORT,
44 process.env.SIDECAR_HOSTNAME,
45 () => reportPort(timeoutServer)
46);
47 
48const helloWorldServer = http.createServer((req, res) => {
49 res.removeHeader('Date');
50 res.setHeader('Keep-Alive', 'timeout=1');
51 
52 switch (req.url.slice(1)) {
53 case 'multiple-writes':
54 res.write('hello');
55 res.end('world');
56 break;
57 case 'end-with-data':
58 res.end('hello world');
59 break;
60 case 'content-length0':
61 res.writeHead(200, { 'Content-Length': '0' });
62 res.end();
63 break;
64 default:
65 res.end();
66 }
67});
68helloWorldServer.listen(
69 process.env.HELLO_WORLD_SERVER_PORT,
70 process.env.SIDECAR_HOSTNAME,
71 () => reportPort(helloWorldServer)
72);
73 
74let headerValidationServerCount = 0;
75const headerValidationServer = http.createServer((req, res) => {
76 if (headerValidationServerCount++ !== 0) {
77 switch (req.url.slice(1)) {
78 case 'test-1':
79 assert.deepStrictEqual(req.rawHeaders, [
80 'test',
81 'value',
82 'Host',
83 `${process.env.SIDECAR_HOSTNAME}:${headerValidationServer.address().port}`,
84 'foo',
85 'bar',
86 'foo',
87 'baz',
88 'connection',
89 'close',
90 ]);
91 break;
92 case 'test-2':
93 assert.deepStrictEqual(req.rawHeaders, [
94 'Content-Length',
95 '0',
96 'Host',
97 `${process.env.SIDECAR_HOSTNAME}:${headerValidationServer.address().port}`,
98 ]);
99 break;
100 default:
101 break;
102 }
103 }
104 
105 res.end('ok');
106});
107headerValidationServer.listen(
108 process.env.HEADER_VALIDATION_SERVER_PORT,
109 process.env.SIDECAR_HOSTNAME,
110 () => reportPort(headerValidationServer)
111);