Skip to content

Commit

Permalink
test: add
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed May 7, 2021
1 parent 5d937fe commit 9116e75
Showing 1 changed file with 120 additions and 28 deletions.
148 changes: 120 additions & 28 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2392,45 +2392,137 @@ describe.each([
});

describe('headers option', () => {
beforeEach((done) => {
const compiler = getCompiler(webpackConfig);
describe('works with object', () => {
beforeEach((done) => {
const compiler = getCompiler(webpackConfig);

instance = middleware(compiler, {
headers: { 'X-nonsense-1': 'yes', 'X-nonsense-2': 'no' },
instance = middleware(compiler, {
headers: { 'X-nonsense-1': 'yes', 'X-nonsense-2': 'no' },
});

app = framework();
app.use(instance);

listen = listenShorthand(done);
});

app = framework();
app.use(instance);
afterEach(close);

listen = listenShorthand(done);
it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
request(app)
.get('/bundle.js')
.expect('X-nonsense-1', 'yes')
.expect('X-nonsense-2', 'no')
.expect(200, done);
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
app.use('/file.jpg', (req, res) => {
// Express API
if (res.send) {
res.send('welcome');
}
// Connect API
else {
res.end('welcome');
}
});

const res = await request(app).get('/file.jpg');
expect(res.statusCode).toEqual(200);
expect(res.headers['X-nonsense-1']).toBeUndefined();
expect(res.headers['X-nonsense-2']).toBeUndefined();
});
});
describe('works with function', () => {
beforeEach((done) => {
const compiler = getCompiler(webpackConfig);

afterEach(close);
instance = middleware(compiler, {
headers: () => {
return { 'X-nonsense-1': 'yes', 'X-nonsense-2': 'no' };
},
});

it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
request(app)
.get('/bundle.js')
.expect('X-nonsense-1', 'yes')
.expect('X-nonsense-2', 'no')
.expect(200, done);
app = framework();
app.use(instance);

listen = listenShorthand(done);
});

afterEach(close);

it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
request(app)
.get('/bundle.js')
.expect('X-nonsense-1', 'yes')
.expect('X-nonsense-2', 'no')
.expect(200, done);
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
app.use('/file.jpg', (req, res) => {
// Express API
if (res.send) {
res.send('welcome');
}
// Connect API
else {
res.end('welcome');
}
});

const res = await request(app).get('/file.jpg');
expect(res.statusCode).toEqual(200);
expect(res.headers['X-nonsense-1']).toBeUndefined();
expect(res.headers['X-nonsense-2']).toBeUndefined();
});
});
describe('works with headers function with params', () => {
beforeEach((done) => {
const compiler = getCompiler(webpackConfig);

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
app.use('/file.jpg', (req, res) => {
// Express API
if (res.send) {
res.send('welcome');
}
// Connect API
else {
res.end('welcome');
}
instance = middleware(compiler, {
// eslint-disable-next-line no-unused-vars
headers: (req, res, context) => {
res.setHeader('X-nonsense-1', 'yes');
res.setHeader('X-nonsense-2', 'no');
},
});

app = framework();
app.use(instance);

listen = listenShorthand(done);
});

const res = await request(app).get('/file.jpg');
expect(res.statusCode).toEqual(200);
expect(res.headers['X-nonsense-1']).toBeUndefined();
expect(res.headers['X-nonsense-2']).toBeUndefined();
afterEach(close);

it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
request(app)
.get('/bundle.js')
.expect('X-nonsense-1', 'yes')
.expect('X-nonsense-2', 'no')
.expect(200, done);
});

it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
app.use('/file.jpg', (req, res) => {
// Express API
if (res.send) {
res.send('welcome');
}
// Connect API
else {
res.end('welcome');
}
});

const res = await request(app).get('/file.jpg');
expect(res.statusCode).toEqual(200);
expect(res.headers['X-nonsense-1']).toBeUndefined();
expect(res.headers['X-nonsense-2']).toBeUndefined();
});
});
});

Expand Down

0 comments on commit 9116e75

Please sign in to comment.