Skip to content

Commit 6b488cf

Browse files
committed
tests: add
1 parent 5d937fe commit 6b488cf

File tree

1 file changed

+117
-28
lines changed

1 file changed

+117
-28
lines changed

test/middleware.test.js

Lines changed: 117 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,45 +2392,134 @@ describe.each([
23922392
});
23932393

23942394
describe('headers option', () => {
2395-
beforeEach((done) => {
2396-
const compiler = getCompiler(webpackConfig);
2395+
describe('works with object', () => {
2396+
beforeEach((done) => {
2397+
const compiler = getCompiler(webpackConfig);
23972398

2398-
instance = middleware(compiler, {
2399-
headers: { 'X-nonsense-1': 'yes', 'X-nonsense-2': 'no' },
2399+
instance = middleware(compiler, {
2400+
headers: { 'X-nonsense-1': 'yes', 'X-nonsense-2': 'no' },
2401+
});
2402+
2403+
app = framework();
2404+
app.use(instance);
2405+
2406+
listen = listenShorthand(done);
24002407
});
24012408

2402-
app = framework();
2403-
app.use(instance);
2409+
afterEach(close);
24042410

2405-
listen = listenShorthand(done);
2411+
it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
2412+
request(app)
2413+
.get('/bundle.js')
2414+
.expect('X-nonsense-1', 'yes')
2415+
.expect('X-nonsense-2', 'no')
2416+
.expect(200, done);
2417+
});
2418+
2419+
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
2420+
app.use('/file.jpg', (req, res) => {
2421+
// Express API
2422+
if (res.send) {
2423+
res.send('welcome');
2424+
}
2425+
// Connect API
2426+
else {
2427+
res.end('welcome');
2428+
}
2429+
});
2430+
2431+
const res = await request(app).get('/file.jpg');
2432+
expect(res.statusCode).toEqual(200);
2433+
expect(res.headers['X-nonsense-1']).toBeUndefined();
2434+
expect(res.headers['X-nonsense-2']).toBeUndefined();
2435+
});
24062436
});
2437+
describe('works with function', () => {
2438+
beforeEach((done) => {
2439+
const compiler = getCompiler(webpackConfig);
24072440

2408-
afterEach(close);
2441+
instance = middleware(compiler, {
2442+
headers: () => ({ 'X-nonsense-1': 'yes', 'X-nonsense-2': 'no' }),
2443+
});
24092444

2410-
it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
2411-
request(app)
2412-
.get('/bundle.js')
2413-
.expect('X-nonsense-1', 'yes')
2414-
.expect('X-nonsense-2', 'no')
2415-
.expect(200, done);
2445+
app = framework();
2446+
app.use(instance);
2447+
2448+
listen = listenShorthand(done);
2449+
});
2450+
2451+
afterEach(close);
2452+
2453+
it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
2454+
request(app)
2455+
.get('/bundle.js')
2456+
.expect('X-nonsense-1', 'yes')
2457+
.expect('X-nonsense-2', 'no')
2458+
.expect(200, done);
2459+
});
2460+
2461+
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
2462+
app.use('/file.jpg', (req, res) => {
2463+
// Express API
2464+
if (res.send) {
2465+
res.send('welcome');
2466+
}
2467+
// Connect API
2468+
else {
2469+
res.end('welcome');
2470+
}
2471+
});
2472+
2473+
const res = await request(app).get('/file.jpg');
2474+
expect(res.statusCode).toEqual(200);
2475+
expect(res.headers['X-nonsense-1']).toBeUndefined();
2476+
expect(res.headers['X-nonsense-2']).toBeUndefined();
2477+
});
24162478
});
2479+
describe('works with headers function with params', () => {
2480+
beforeEach((done) => {
2481+
const compiler = getCompiler(webpackConfig);
24172482

2418-
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
2419-
app.use('/file.jpg', (req, res) => {
2420-
// Express API
2421-
if (res.send) {
2422-
res.send('welcome');
2423-
}
2424-
// Connect API
2425-
else {
2426-
res.end('welcome');
2427-
}
2483+
instance = middleware(compiler, {
2484+
headers: (req, res, context) => {
2485+
res.setHeader('X-nonsense-1', 'yes');
2486+
res.setHeader('X-nonsense-2', 'no');
2487+
},
2488+
});
2489+
2490+
app = framework();
2491+
app.use(instance);
2492+
2493+
listen = listenShorthand(done);
24282494
});
24292495

2430-
const res = await request(app).get('/file.jpg');
2431-
expect(res.statusCode).toEqual(200);
2432-
expect(res.headers['X-nonsense-1']).toBeUndefined();
2433-
expect(res.headers['X-nonsense-2']).toBeUndefined();
2496+
afterEach(close);
2497+
2498+
it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
2499+
request(app)
2500+
.get('/bundle.js')
2501+
.expect('X-nonsense-1', 'yes')
2502+
.expect('X-nonsense-2', 'no')
2503+
.expect(200, done);
2504+
});
2505+
2506+
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
2507+
app.use('/file.jpg', (req, res) => {
2508+
// Express API
2509+
if (res.send) {
2510+
res.send('welcome');
2511+
}
2512+
// Connect API
2513+
else {
2514+
res.end('welcome');
2515+
}
2516+
});
2517+
2518+
const res = await request(app).get('/file.jpg');
2519+
expect(res.statusCode).toEqual(200);
2520+
expect(res.headers['X-nonsense-1']).toBeUndefined();
2521+
expect(res.headers['X-nonsense-2']).toBeUndefined();
2522+
});
24342523
});
24352524
});
24362525

0 commit comments

Comments
 (0)