|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const request = require('supertest'); |
| 5 | +const testServer = require('../helpers/test-server'); |
| 6 | +const config = require('../fixtures/contentbase-config/webpack.config'); |
| 7 | +const port = require('../ports-map')['contentBase-option']; |
| 8 | + |
| 9 | +const contentBasePublic = path.resolve( |
| 10 | + __dirname, |
| 11 | + '../fixtures/contentbase-config/public' |
| 12 | +); |
| 13 | +const contentBaseOther = path.resolve( |
| 14 | + __dirname, |
| 15 | + '../fixtures/contentbase-config/other' |
| 16 | +); |
| 17 | + |
| 18 | +const contentBasePublicPath = '/serve-content-base-at-this-url'; |
| 19 | + |
| 20 | +describe('contentBasePublicPath option', () => { |
| 21 | + let server; |
| 22 | + let req; |
| 23 | + |
| 24 | + describe('to directory', () => { |
| 25 | + beforeAll((done) => { |
| 26 | + server = testServer.start( |
| 27 | + config, |
| 28 | + { |
| 29 | + contentBase: contentBasePublic, |
| 30 | + contentBasePublicPath, |
| 31 | + watchContentBase: true, |
| 32 | + port, |
| 33 | + }, |
| 34 | + done |
| 35 | + ); |
| 36 | + req = request(server.app); |
| 37 | + }); |
| 38 | + |
| 39 | + afterAll((done) => { |
| 40 | + testServer.close(() => { |
| 41 | + done(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('Request to index', (done) => { |
| 46 | + req.get(`${contentBasePublicPath}/`).expect(200, /Heyo/, done); |
| 47 | + }); |
| 48 | + |
| 49 | + it('Request to other file', (done) => { |
| 50 | + req |
| 51 | + .get(`${contentBasePublicPath}/other.html`) |
| 52 | + .expect(200, /Other html/, done); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('test listing files in folders without index.html using the option serveIndex:false', () => { |
| 57 | + beforeAll((done) => { |
| 58 | + server = testServer.start( |
| 59 | + config, |
| 60 | + { |
| 61 | + contentBase: contentBasePublic, |
| 62 | + contentBasePublicPath, |
| 63 | + watchContentBase: true, |
| 64 | + serveIndex: false, |
| 65 | + port, |
| 66 | + }, |
| 67 | + done |
| 68 | + ); |
| 69 | + req = request(server.app); |
| 70 | + }); |
| 71 | + |
| 72 | + afterAll((done) => { |
| 73 | + testServer.close(() => { |
| 74 | + done(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("shouldn't list the files inside the assets folder (404)", (done) => { |
| 79 | + req.get(`${contentBasePublicPath}/assets/`).expect(404, done); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should show Heyo. because bar has index.html inside it (200)', (done) => { |
| 83 | + req.get(`${contentBasePublicPath}/bar/`).expect(200, /Heyo/, done); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('test listing files in folders without index.html using the option serveIndex:true', () => { |
| 88 | + beforeAll((done) => { |
| 89 | + server = testServer.start( |
| 90 | + config, |
| 91 | + { |
| 92 | + contentBase: contentBasePublic, |
| 93 | + contentBasePublicPath, |
| 94 | + watchContentBase: true, |
| 95 | + serveIndex: true, |
| 96 | + port, |
| 97 | + }, |
| 98 | + done |
| 99 | + ); |
| 100 | + req = request(server.app); |
| 101 | + }); |
| 102 | + |
| 103 | + afterAll((done) => { |
| 104 | + testServer.close(() => { |
| 105 | + done(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should list the files inside the assets folder (200)', (done) => { |
| 110 | + req.get(`${contentBasePublicPath}/assets/`).expect(200, done); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should show Heyo. because bar has index.html inside it (200)', (done) => { |
| 114 | + req.get(`${contentBasePublicPath}/bar/`).expect(200, /Heyo/, done); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('test listing files in folders without index.html using the option serveIndex default (true)', () => { |
| 119 | + beforeAll((done) => { |
| 120 | + server = testServer.start( |
| 121 | + config, |
| 122 | + { |
| 123 | + contentBase: contentBasePublic, |
| 124 | + contentBasePublicPath, |
| 125 | + watchContentBase: true, |
| 126 | + port, |
| 127 | + }, |
| 128 | + done |
| 129 | + ); |
| 130 | + req = request(server.app); |
| 131 | + }); |
| 132 | + |
| 133 | + afterAll((done) => { |
| 134 | + testServer.close(() => { |
| 135 | + done(); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should list the files inside the assets folder (200)', (done) => { |
| 140 | + req.get(`${contentBasePublicPath}/assets/`).expect(200, done); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should show Heyo. because bar has index.html inside it (200)', (done) => { |
| 144 | + req.get(`${contentBasePublicPath}/bar/`).expect(200, /Heyo/, done); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('to directories', () => { |
| 149 | + beforeAll((done) => { |
| 150 | + server = testServer.start( |
| 151 | + config, |
| 152 | + { |
| 153 | + contentBase: [contentBasePublic, contentBaseOther], |
| 154 | + contentBasePublicPath, |
| 155 | + port, |
| 156 | + }, |
| 157 | + done |
| 158 | + ); |
| 159 | + req = request(server.app); |
| 160 | + }); |
| 161 | + |
| 162 | + afterAll((done) => { |
| 163 | + testServer.close(() => { |
| 164 | + done(); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it('Request to first directory', (done) => { |
| 169 | + req.get(`${contentBasePublicPath}/`).expect(200, /Heyo/, done); |
| 170 | + }); |
| 171 | + |
| 172 | + it('Request to second directory', (done) => { |
| 173 | + req.get(`${contentBasePublicPath}/foo.html`).expect(200, /Foo!/, done); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + describe('default to PWD', () => { |
| 178 | + beforeAll((done) => { |
| 179 | + jest.spyOn(process, 'cwd').mockImplementation(() => contentBasePublic); |
| 180 | + |
| 181 | + server = testServer.start(config, { contentBasePublicPath }, done); |
| 182 | + req = request(server.app); |
| 183 | + }); |
| 184 | + |
| 185 | + afterAll((done) => { |
| 186 | + testServer.close(() => { |
| 187 | + done(); |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + it('Request to page', (done) => { |
| 192 | + req.get(`${contentBasePublicPath}/other.html`).expect(200, done); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + describe('disable', () => { |
| 197 | + beforeAll((done) => { |
| 198 | + // This is a somewhat weird test, but it is important that we mock |
| 199 | + // the PWD here, and test if /other.html in our "fake" PWD really is not requested. |
| 200 | + jest.spyOn(process, 'cwd').mockImplementation(() => contentBasePublic); |
| 201 | + |
| 202 | + server = testServer.start( |
| 203 | + config, |
| 204 | + { |
| 205 | + contentBase: false, |
| 206 | + contentBasePublicPath, |
| 207 | + port, |
| 208 | + }, |
| 209 | + done |
| 210 | + ); |
| 211 | + req = request(server.app); |
| 212 | + }); |
| 213 | + |
| 214 | + afterAll((done) => { |
| 215 | + testServer.close(() => { |
| 216 | + done(); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + it('Request to page', (done) => { |
| 221 | + req.get(`${contentBasePublicPath}/other.html`).expect(404, done); |
| 222 | + }); |
| 223 | + }); |
| 224 | + |
| 225 | + describe('Content type', () => { |
| 226 | + beforeAll((done) => { |
| 227 | + server = testServer.start( |
| 228 | + config, |
| 229 | + { |
| 230 | + contentBase: [contentBasePublic], |
| 231 | + contentBasePublicPath, |
| 232 | + port, |
| 233 | + }, |
| 234 | + done |
| 235 | + ); |
| 236 | + req = request(server.app); |
| 237 | + }); |
| 238 | + |
| 239 | + afterAll((done) => { |
| 240 | + testServer.close(() => { |
| 241 | + done(); |
| 242 | + }); |
| 243 | + }); |
| 244 | + |
| 245 | + it('Request foo.wasm', (done) => { |
| 246 | + req |
| 247 | + .get(`${contentBasePublicPath}/foo.wasm`) |
| 248 | + .expect('Content-Type', 'application/wasm', done); |
| 249 | + }); |
| 250 | + }); |
| 251 | + |
| 252 | + describe('to ignore other methods than GET and HEAD', () => { |
| 253 | + beforeAll((done) => { |
| 254 | + server = testServer.start( |
| 255 | + config, |
| 256 | + { |
| 257 | + contentBase: contentBasePublic, |
| 258 | + contentBasePublicPath, |
| 259 | + watchContentBase: true, |
| 260 | + port, |
| 261 | + }, |
| 262 | + done |
| 263 | + ); |
| 264 | + req = request(server.app); |
| 265 | + }); |
| 266 | + |
| 267 | + afterAll((done) => { |
| 268 | + testServer.close(done); |
| 269 | + }); |
| 270 | + |
| 271 | + it('GET request', (done) => { |
| 272 | + req.get(`${contentBasePublicPath}/`).expect(200, done); |
| 273 | + }); |
| 274 | + |
| 275 | + it('HEAD request', (done) => { |
| 276 | + req.head(`${contentBasePublicPath}/`).expect(200, done); |
| 277 | + }); |
| 278 | + |
| 279 | + it('POST request', (done) => { |
| 280 | + req.post(`${contentBasePublicPath}/`).expect(405, done); |
| 281 | + }); |
| 282 | + |
| 283 | + it('PUT request', (done) => { |
| 284 | + req.put(`${contentBasePublicPath}/`).expect(405, done); |
| 285 | + }); |
| 286 | + |
| 287 | + it('DELETE request', (done) => { |
| 288 | + req.delete(`${contentBasePublicPath}/`).expect(405, done); |
| 289 | + }); |
| 290 | + |
| 291 | + it('PATCH request', (done) => { |
| 292 | + req.patch(`${contentBasePublicPath}/`).expect(405, done); |
| 293 | + }); |
| 294 | + }); |
| 295 | +}); |
0 commit comments