Skip to content

Commit 00d96ab

Browse files
committed
Merge pull request #93 from digitalsadhu/BenjaminHorn/config.js
do not override config.js's remotes.options.json
2 parents d670fc0 + 71418fc commit 00d96ab

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

lib/headers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ module.exports = function (app, options) {
3030
]);
3131

3232
//extend rest body parser to also parse application/vnd.api+json
33-
remotes.options.json = {
33+
remotes.options.json = remotes.options.json || {};
34+
remotes.options.json = _.extend(remotes.options.json, {
3435
strict: false,
3536
type: function (req) {
3637
//if Content-Type is any of the following, then parse otherwise don't
3738
return !!is(req, ['json', 'application/json', 'application/vnd.api+json']);
3839
}
39-
};
40+
});
4041
};

test/config.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var request = require('supertest');
2+
var loopback = require('loopback');
3+
var expect = require('chai').expect;
4+
var JSONAPIComponent = require('../');
5+
6+
var app;
7+
var Image;
8+
9+
describe('Dont override config.js ', function () {
10+
beforeEach(function () {
11+
app = loopback();
12+
app.set('legacyExplorer', false);
13+
app.use(loopback.rest());
14+
15+
var remotes = app.remotes();
16+
remotes.options.json = {limit: '100B'};
17+
var ds = loopback.createDataSource('memory');
18+
Image = ds.createModel('image', {
19+
id: {type: Number, id: true},
20+
source: String
21+
});
22+
23+
app.model(Image);
24+
25+
JSONAPIComponent(app);
26+
});
27+
28+
it('should have limit property', function (done) {
29+
var remotes = app.remotes();
30+
expect(remotes.options.json).to.have.any.keys('limit');
31+
done();
32+
});
33+
34+
it('should accept payload < 100B', function (done) {
35+
request(app).post('/images')
36+
.send({
37+
data: {
38+
type: 'images',
39+
attributes: {
40+
source: 'a'
41+
}
42+
}
43+
})
44+
.set('Accept', 'application/vnd.api+json')
45+
.set('Content-Type', 'application/json')
46+
.expect(201)
47+
.end(done);
48+
});
49+
50+
it('should not accept payload > 100B', function (done) {
51+
request(app).post('/images')
52+
.send({
53+
data: {
54+
type: 'images',
55+
attributes: {
56+
source: 'A long text to make the payload greater then 100B'
57+
}
58+
}
59+
})
60+
.set('Accept', 'application/vnd.api+json')
61+
.set('Content-Type', 'application/json')
62+
.expect(400)
63+
.end(done);
64+
});
65+
66+
});

0 commit comments

Comments
 (0)