Skip to content

Commit

Permalink
chore(test/rest): more tests to detect accidental api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Feb 1, 2016
1 parent 3166341 commit bff9fd8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build/tasks/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ app.all('*', function(req, res) {
path: req.path,
query: req.query,
body: req.body,
method: req.method
method: req.method,
contentType: req.header('content-type'),
Authorization: req.header('Authorization')
});
});

Expand Down
75 changes: 75 additions & 0 deletions test/rest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ let baseUrls = {
config.registerEndpoint('api', baseUrls.api);
config.registerEndpoint('github', baseUrls.github);

let criteria = {user: 'john', comment: 'last'};
let body = {message: 'some'};
let options = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer aToken'
}
};

describe('Rest', function() {
describe('.find()', function() {
it('Should find results for multiple endpoints.', function(done) {
Expand All @@ -27,12 +36,78 @@ describe('Rest', function() {
expect(x[0].login).toBe('RWOverdijk');
}),
injectTest.apiEndpoint.find('posts')
.then(y => {
expect(y.method).toBe('GET');
}),
injectTest.apiEndpoint.find('posts')
.then(y => {
expect(y.path).toBe('/posts');
}),
injectTest.apiEndpoint.find('posts', 'id')
.then(y => {
expect(y.path).toBe('/posts/id');
expect(JSON.stringify(y.query)).toBe('{}');
}),
injectTest.apiEndpoint.find('posts', criteria)
.then(y => {
expect(y.path).toBe('/posts');
expect(JSON.stringify(y.query)).toBe(JSON.stringify(criteria));
}),
injectTest.apiEndpoint.find('posts', undefined, options)
.then(y => {
expect(y.path).toBe('/posts');
expect(y.contentType).toBe(options.headers['Content-Type']);
expect(y.Authorization).toBe(options.headers['Authorization']);
})
]).then(x => {
done();
});
});
});

describe('.update()', function() {
it('Should update with body (as json), criteria and options.', function(done) {
let injectTest = container.get(InjectTest);

injectTest.apiEndpoint.update('posts', criteria, body, options)
.then(y => {
expect(y.method).toBe('PUT');
expect(y.path).toBe('/posts');
expect(JSON.stringify(y.query)).toBe(JSON.stringify(criteria));
expect(y.contentType).toBe('application/json');
expect(y.Authorization).toBe(options.headers['Authorization']);
done();
});
});
});

describe('.destroy()', function() {
it('Should destroy with id and options.', function(done) {
let injectTest = container.get(InjectTest);

injectTest.apiEndpoint.destroy('posts', 'id', options)
.then(y => {
expect(y.method).toBe('DELETE');
expect(y.path).toBe('/posts/id');
expect(JSON.stringify(y.query)).toBe('{}');
expect(y.Authorization).toBe(options.headers['Authorization']);
done();
});
});
});

describe('.create()', function() {
it('Should create body (as json) and options.', function(done) {
let injectTest = container.get(InjectTest);

injectTest.apiEndpoint.create('posts', body, options)
.then(y => {
expect(y.method).toBe('POST');
expect(y.path).toBe('/posts');
expect(y.contentType).toBe('application/json');
expect(y.Authorization).toBe(options.headers['Authorization']);
done();
});
});
});
});

0 comments on commit bff9fd8

Please sign in to comment.