Skip to content

Commit 316139b

Browse files
committed
Improve test coverage of headers
To bypass issues with supertest/superagent, I have added a test helper and written a single test to prove headers are working as expected
1 parent 05dccb9 commit 316139b

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "JSONAPI support for loopback",
55
"main": "lib/index.js",
66
"scripts": {
7-
"test": "npm run lint && mocha --reporter=spec ./test/**/*",
7+
"test": "npm run lint && mocha --reporter=spec ./test/**/*.test.js",
88
"lint": "eslint ."
99
},
1010
"repository": {

test/create.test.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var request = require('supertest');
22
var loopback = require('loopback');
33
var expect = require('chai').expect;
4+
var query = require('./util/query');
45
var JSONAPIComponent = require('../');
56
var app;
67
var Post;
@@ -38,25 +39,35 @@ describe('loopback json api component create method', function () {
3839
.end(done);
3940
});
4041
it('POST /models should have the JSON API Content-Type header set on response', function (done) {
41-
//TODO: superagent/supertest breaks when trying to use JSON API Content-Type header
42+
var data = {
43+
data: {
44+
type: 'posts',
45+
attributes: {
46+
title: 'my post',
47+
content: 'my post content'
48+
}
49+
}
50+
};
51+
52+
var options = {
53+
headers: {
54+
'Accept': 'application/vnd.api+json',
55+
'Content-Type': 'application/vnd.api+json'
56+
}
57+
};
58+
59+
//use http module via util to make this post to check headers are working since
60+
//superagent/supertest breaks when trying to use JSON API Content-Type header
4261
//waiting on a fix
4362
//see https://github.com/visionmedia/superagent/issues/753
44-
//using Content-Type: application/json in the mean time.
45-
//Have tested correct header using curl and all is well
46-
// request(app).post('/posts')
47-
// .send({
48-
// data: {
49-
// type: 'posts',
50-
// attributes: {
51-
// title: 'my post',
52-
// content: 'my post content'
53-
// }
54-
// }
55-
// })
56-
// .set('Content-Type', 'application/vnd.api+json')
57-
// .expect('Content-Type', 'application/vnd.api+json; charset=utf-8')
58-
// .end(done);
59-
done();
63+
query(app).post('/posts', data, options, function (err, res) {
64+
if (err) console.log(err);
65+
expect(res.headers['content-type']).to.match(/application\/vnd\.api\+json/);
66+
expect(res.statusCode).to.equal(201);
67+
expect(res.body).to.have.all.keys('data');
68+
expect(res.body.data).to.have.all.keys('type', 'id', 'links', 'attributes');
69+
done();
70+
});
6071
});
6172

6273
it('POST /models should have the Location header set on response', function (done) {

test/util/query.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var http = require('http');
2+
3+
module.exports = function (app) {
4+
return {
5+
post: function (path, data, options, cb) {
6+
var s = app.listen(function () {
7+
var req = http.request({
8+
port: s.address().port,
9+
path: path,
10+
headers: options.headers || {},
11+
method: 'POST'
12+
}, function (res) {
13+
res.setEncoding('utf8');
14+
res.rawData = '';
15+
res.on('data', function (chunk) {
16+
res.rawData += chunk;
17+
});
18+
res.on('end', function () {
19+
res.body = JSON.parse(res.rawData);
20+
cb(null, res);
21+
});
22+
});
23+
req.on('error', function (err) {
24+
cb(err);
25+
});
26+
var postData = JSON.stringify(data || {});
27+
req.write(postData);
28+
req.end();
29+
});
30+
}
31+
};
32+
};

0 commit comments

Comments
 (0)