|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | + |
| 5 | +import type http from 'http'; |
| 6 | +import { vol } from 'memfs'; |
| 7 | +import nock from 'nock'; |
| 8 | + |
| 9 | +import { prepareFile, runServer } from './server'; |
| 10 | + |
| 11 | +// Use mock files. |
| 12 | +jest.mock('fs', () => require('memfs').fs); |
| 13 | +jest.mock('fs/promises', () => require('memfs').fs.promises); |
| 14 | + |
| 15 | +const PORT = 3000; |
| 16 | + |
| 17 | +describe('Server', () => { |
| 18 | + describe('prepareFile', () => { |
| 19 | + beforeAll(() => { |
| 20 | + vol.fromJSON( |
| 21 | + { |
| 22 | + '/system/sensitive.txt': 'sensitive data', |
| 23 | + '/root/index.html': '<html>Hello World</html>', |
| 24 | + '/root/styles.css': 'body { color: red; }', |
| 25 | + }, |
| 26 | + '/', |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + afterAll(() => { |
| 31 | + vol.reset(); |
| 32 | + }); |
| 33 | + |
| 34 | + test('Should return the correct file.', async () => { |
| 35 | + const file = await prepareFile('/root', '/styles.css'); |
| 36 | + expect(file.found).toBe(true); |
| 37 | + expect(file.ext).toBe('css'); |
| 38 | + expect(file.content).toBe('body { color: red; }'); |
| 39 | + }); |
| 40 | + |
| 41 | + test('Should handle missing files.', async () => { |
| 42 | + const file = await prepareFile('/root', '/nonexistent.txt'); |
| 43 | + expect(file.found).toBe(false); |
| 44 | + expect(file.content).toBe(''); |
| 45 | + }); |
| 46 | + |
| 47 | + test('Should append index.html when path ends with /', async () => { |
| 48 | + const file = await prepareFile('/root', '/'); |
| 49 | + expect(file.found).toBe(true); |
| 50 | + expect(file.ext).toBe('html'); |
| 51 | + expect(file.content).toBe('<html>Hello World</html>'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('Should prevent path traversal attacks', async () => { |
| 55 | + const file = await prepareFile('/root', '/../system/sensitive.txt'); |
| 56 | + expect(file.found).toBe(false); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('runServer', () => { |
| 61 | + let server: http.Server; |
| 62 | + |
| 63 | + beforeAll(() => { |
| 64 | + // Allow local server. |
| 65 | + nock.enableNetConnect('127.0.0.1'); |
| 66 | + |
| 67 | + // Add one file. |
| 68 | + vol.fromJSON({ |
| 69 | + '/root/index.html': '<html>Hello World</html>', |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + afterAll(() => { |
| 74 | + vol.reset(); |
| 75 | + nock.cleanAll(); |
| 76 | + nock.disableNetConnect(); |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(() => { |
| 80 | + if (!server) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + server.close(); |
| 85 | + server.closeAllConnections(); |
| 86 | + server.closeIdleConnections(); |
| 87 | + }); |
| 88 | + |
| 89 | + test('Should start the server', async () => { |
| 90 | + server = runServer({ |
| 91 | + port: PORT, |
| 92 | + root: '/root', |
| 93 | + }); |
| 94 | + expect(server).toBeDefined(); |
| 95 | + expect(server.listening).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + test('Should handle routes', async () => { |
| 99 | + const getHandler = jest.fn((req, res) => { |
| 100 | + res.end('Hello World'); |
| 101 | + }); |
| 102 | + |
| 103 | + const routes = { |
| 104 | + '/route': { |
| 105 | + get: getHandler, |
| 106 | + }, |
| 107 | + }; |
| 108 | + |
| 109 | + server = runServer({ |
| 110 | + port: PORT, |
| 111 | + root: '/root', |
| 112 | + routes, |
| 113 | + }); |
| 114 | + |
| 115 | + const response = await fetch(`http://127.0.0.1:${PORT}/route`); |
| 116 | + expect(response.ok).toBe(true); |
| 117 | + expect(await response.text()).toBe('Hello World'); |
| 118 | + expect(getHandler).toHaveBeenCalled(); |
| 119 | + }); |
| 120 | + |
| 121 | + test("Should fallback to files when routes doesn't hit", async () => { |
| 122 | + const routes = { |
| 123 | + '/route': { |
| 124 | + get: jest.fn(), |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + server = runServer({ |
| 129 | + port: PORT, |
| 130 | + root: '/root', |
| 131 | + routes, |
| 132 | + }); |
| 133 | + |
| 134 | + const response = await fetch(`http://127.0.0.1:${PORT}/`); |
| 135 | + expect(response.ok).toBe(true); |
| 136 | + expect(await response.text()).toBe('<html>Hello World</html>'); |
| 137 | + }); |
| 138 | + |
| 139 | + test('Should use middleware', async () => { |
| 140 | + const middleware = jest.fn((response) => { |
| 141 | + return { |
| 142 | + statusCode: 201, |
| 143 | + headers: { |
| 144 | + 'Content-Type': 'text/plain', |
| 145 | + }, |
| 146 | + body: `Content was: ${response.body}`, |
| 147 | + }; |
| 148 | + }); |
| 149 | + |
| 150 | + server = runServer({ |
| 151 | + port: PORT, |
| 152 | + root: '/root', |
| 153 | + middleware, |
| 154 | + }); |
| 155 | + |
| 156 | + const response = await fetch(`http://127.0.0.1:${PORT}/`); |
| 157 | + expect(response.ok).toBe(true); |
| 158 | + expect(response.status).toBe(201); |
| 159 | + expect(response.headers.get('Content-Type')).toBe('text/plain'); |
| 160 | + expect(await response.text()).toBe('Content was: <html>Hello World</html>'); |
| 161 | + expect(middleware).toHaveBeenCalled(); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments