Skip to content

Commit

Permalink
Added a test for 404s
Browse files Browse the repository at this point in the history
  • Loading branch information
ErisDS committed Jan 25, 2022
1 parent 2b5f7ef commit eb73f62
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/express-test/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ Object.keys(agents).forEach((agentName) => {
text.should.eql('{"posts":[{"id":42,"title":"Hello World!"}]}');
});

it('wrong param', async function () {
const {statusCode, headers, body, text} = await agent.get('/posts/404/');

statusCode.should.eql(404);
headers.should.be.an.Object().with.properties('x-powered-by', 'content-type', 'content-length', 'etag');
body.should.eql({});
text.should.eql('Not Found');
});

it('PUT JSON', async function () {
const {statusCode, headers, body, text} = await agent.put('/posts/42/', {
body: {posts: [{title: 'So long and thanks for all the fish.'}]}
Expand Down Expand Up @@ -71,6 +80,15 @@ describe('Testing with supertest', function () {
text.should.eql('{"posts":[{"id":42,"title":"Hello World!"}]}');
});

it('wrong param', async function () {
const {statusCode, headers, body, text} = await agent.get('/posts/404/');

statusCode.should.eql(404);
headers.should.be.an.Object().with.properties('x-powered-by', 'content-type', 'content-length', 'etag');
body.should.eql({});
text.should.eql('Not Found');
});

it('PUT JSON', async function () {
const {statusCode, headers, body, text} = await agent
.put('/posts/42/')
Expand Down
13 changes: 10 additions & 3 deletions packages/express-test/test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ const path = require('path');
app.use(express.json());

app.get('/', (req, res) => {
res.send('Hello World!');
return res.send('Hello World!');
});

app.get('/posts/:id/', async (req, res) => {
if (req.params.id !== '42') {
return res.sendStatus(404);
}

const response = await fs.readFile(path.join(__dirname, 'post.json'), {encoding: 'utf8'});
const json = JSON.parse(response);
res.json(json);
return res.json(json);
});

app.put('/posts/:id', async (req, res) => {
if (req.params.id !== '42') {
return res.sendStatus(404);
}
const response = await fs.readFile(path.join(__dirname, 'post.json'), {encoding: 'utf8'});
const json = JSON.parse(response);
json.posts[0] = Object.assign(json.posts[0], req.body.posts[0]);

res.json(json);
return res.json(json);
});

module.exports = app;

0 comments on commit eb73f62

Please sign in to comment.