Skip to content

Commit 8d40f1c

Browse files
committed
Merge pull request #5 from jelhan/fix-content-type
set correct content type
2 parents 5a8fe1e + 21af373 commit 8d40f1c

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/responses/ok.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ module.exports = function sendOK(data) {
6060
// Serialize to jsonapi
6161
jsonApiRes = new JSONAPISerializer(modelName, data, opts);
6262

63-
// Set status code and send response
63+
// Set mime type
64+
res.set('Content-Type', 'application/vnd.api+json');
65+
66+
// Set status code
6467
res.status(200);
68+
69+
// Send response
6570
return res.json(jsonApiRes);
6671
};

tests/responses/ok-test.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ test('Book model and fixture is loaded correctly', function (t) {
8080
});
8181

8282
test('Returns correct attributes', function (t) {
83-
t.plan(7);
83+
t.plan(9);
8484

8585
sails.request({
8686
url : '/user',
@@ -90,6 +90,8 @@ test('Returns correct attributes', function (t) {
9090
t.fail(err);
9191
}
9292
try {
93+
t.equal(res.statusCode, 200, 'HTTP status code is 200');
94+
t.equal(res.headers['Content-Type'], 'application/vnd.api+json', 'Sends jsonapi mime type');
9395
t.ok(body.data, 'Body contains a "data" property');
9496
t.equal(body.data.length, 2, 'There are two user objects');
9597
t.equal(body.data[0].id, '1', 'First model id is 1');
@@ -105,7 +107,7 @@ test('Returns correct attributes', function (t) {
105107
});
106108

107109
test('Returns correct nested resources', function (t) {
108-
t.plan(3);
110+
t.plan(5);
109111

110112
sails.config.jsonapi.compoundDoc = false;
111113

@@ -117,6 +119,8 @@ test('Returns correct nested resources', function (t) {
117119
t.fail(err);
118120
}
119121
try {
122+
t.equal(res.statusCode, 200, 'HTTP status code is 200');
123+
t.equal(res.headers['Content-Type'], 'application/vnd.api+json', 'Sends jsonapi mime type');
120124
t.ok(body.data[0].attributes.books, '"attributes" contains a "books" property');
121125
t.equal(typeof body.data[0].attributes.books, 'object', '"books" is an object');
122126
t.deepEqual(body.data[0].attributes.books[0].title, 'A Game of Thrones', '"title" of first book is "A Game of Thrones"');
@@ -128,7 +132,7 @@ test('Returns correct nested resources', function (t) {
128132
});
129133

130134
test('Returns relationships for compound document', function (t) {
131-
t.plan(4);
135+
t.plan(6);
132136

133137
sails.config.jsonapi.compoundDoc = true;
134138

@@ -140,6 +144,8 @@ test('Returns relationships for compound document', function (t) {
140144
t.fail(err);
141145
}
142146
try {
147+
t.equal(res.statusCode, 200, 'HTTP status code is 200');
148+
t.equal(res.headers['Content-Type'], 'application/vnd.api+json', 'Sends jsonapi mime type');
143149
t.ok(body.data[0].relationships, 'Body contains a "relationships" property');
144150
t.ok(body.data[0].relationships.books, 'Relationships contains a "books" property');
145151
t.ok(body.data[0].relationships.books.data, 'Relationships contains a "books" property');
@@ -152,7 +158,7 @@ test('Returns relationships for compound document', function (t) {
152158
});
153159

154160
test('Returns included data', function (t) {
155-
t.plan(8);
161+
t.plan(10);
156162

157163
sails.config.jsonapi.compoundDoc = true;
158164
sails.config.jsonapi.included = true;
@@ -165,6 +171,8 @@ test('Returns included data', function (t) {
165171
t.fail(err);
166172
}
167173
try {
174+
t.equal(res.statusCode, 200, 'HTTP status code is 200');
175+
t.equal(res.headers['Content-Type'], 'application/vnd.api+json', 'Sends jsonapi mime type');
168176
t.ok(body.included, 'Body contains an "included" property');
169177
t.equal(body.included.length, 3, 'Three books are included');
170178
t.ok(body.included[0].type, '"included" contains a "type" property');
@@ -181,7 +189,7 @@ test('Returns included data', function (t) {
181189
});
182190

183191
test('Does not return included data if "included = false"', function (t) {
184-
t.plan(2);
192+
t.plan(4);
185193

186194
sails.config.jsonapi.compoundDoc = true;
187195
sails.config.jsonapi.included = false;
@@ -194,6 +202,8 @@ test('Does not return included data if "included = false"', function (t) {
194202
t.fail(err);
195203
}
196204
try {
205+
t.equal(res.statusCode, 200, 'HTTP status code is 200');
206+
t.equal(res.headers['Content-Type'], 'application/vnd.api+json', 'Sends jsonapi mime type');
197207
t.notOk(body.included, 'Body does not contain an "included" property');
198208
t.ok(body.data[0].relationships, 'But it still has a "relationships" property (compound doc)');
199209
} catch (err) {

0 commit comments

Comments
 (0)