|
1 | | -describe('dsHttpAdapter#find', function () { |
2 | | - it('should find a user in http', function (done) { |
3 | | - var id; |
4 | | - dsHttpAdapter.create(User, { name: 'John' }) |
5 | | - .then(function (user) { |
6 | | - id = user.id; |
7 | | - assert.equal(user.name, 'John'); |
8 | | - assert.isString(user.id); |
9 | | - return dsHttpAdapter.find(User, user.id); |
10 | | - }) |
11 | | - .then(function (user) { |
12 | | - assert.equal(user.name, 'John'); |
13 | | - assert.isString(user.id); |
14 | | - assert.deepEqual(user, { id: id, name: 'John' }); |
15 | | - return dsHttpAdapter.destroy(User, id); |
16 | | - }) |
17 | | - .then(function (destroyedUser) { |
18 | | - assert.isFalse(!!destroyedUser); |
| 1 | +describe('DSHttpAdapter.find(resourceConfig, id, options)', function () { |
| 2 | + |
| 3 | + it('should make a GET request', function (done) { |
| 4 | + var _this = this; |
| 5 | + |
| 6 | + dsHttpAdapter.find({ |
| 7 | + baseUrl: 'api', |
| 8 | + endpoint: 'posts', |
| 9 | + getEndpoint: function () { |
| 10 | + return 'posts'; |
| 11 | + } |
| 12 | + }, 1).then(function (data) { |
| 13 | + assert.deepEqual(data, p1, 'post should have been found'); |
| 14 | + |
| 15 | + dsHttpAdapter.find({ |
| 16 | + baseUrl: 'api', |
| 17 | + endpoint: 'posts', |
| 18 | + getEndpoint: function () { |
| 19 | + return 'posts'; |
| 20 | + } |
| 21 | + }, 1, { baseUrl: 'api2' }).then(function (data) { |
| 22 | + assert.deepEqual(data, p1, 'post should have been found'); |
| 23 | + assert.equal(queryTransform.callCount, 0, 'queryTransform should not have been called'); |
19 | 24 | done(); |
20 | | - }) |
21 | | - .catch(done); |
| 25 | + }).catch(function (err) { |
| 26 | + console.error(err.stack); |
| 27 | + done('should not have rejected'); |
| 28 | + }); |
| 29 | + |
| 30 | + setTimeout(function () { |
| 31 | + assert.equal(2, _this.requests.length); |
| 32 | + assert.equal(_this.requests[1].url, 'api2/posts/1'); |
| 33 | + assert.equal(_this.requests[1].method, 'get'); |
| 34 | + _this.requests[1].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(p1)); |
| 35 | + }, 10); |
| 36 | + }).catch(function (err) { |
| 37 | + console.error(err.stack); |
| 38 | + done('should not have rejected'); |
| 39 | + }); |
| 40 | + |
| 41 | + setTimeout(function () { |
| 42 | + assert.equal(1, _this.requests.length); |
| 43 | + assert.equal(_this.requests[0].url, 'api/posts/1'); |
| 44 | + assert.equal(_this.requests[0].method, 'get'); |
| 45 | + _this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(p1)); |
| 46 | + }, 10); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should use default configs', function (done) { |
| 50 | + var _this = this; |
| 51 | + |
| 52 | + dsHttpAdapter.defaults.httpConfig.params = { test: 'test' }; |
| 53 | + dsHttpAdapter.defaults.httpConfig.headers = { Authorization: 'test' }; |
| 54 | + |
| 55 | + dsHttpAdapter.find({ |
| 56 | + baseUrl: 'api', |
| 57 | + endpoint: 'posts', |
| 58 | + getEndpoint: function () { |
| 59 | + return 'posts'; |
| 60 | + } |
| 61 | + }, 1).then(function (data) { |
| 62 | + assert.deepEqual(data, p1, 'post should have been found'); |
| 63 | + |
| 64 | + delete dsHttpAdapter.defaults.httpConfig.params; |
| 65 | + delete dsHttpAdapter.defaults.httpConfig.headers; |
| 66 | + done(); |
| 67 | + }).catch(function (err) { |
| 68 | + console.error(err.stack); |
| 69 | + done('should not have rejected'); |
| 70 | + }); |
| 71 | + |
| 72 | + setTimeout(function () { |
| 73 | + assert.equal(1, _this.requests.length); |
| 74 | + assert.equal(_this.requests[0].url, 'api/posts/1?test=test'); |
| 75 | + assert.equal(_this.requests[0].method, 'get'); |
| 76 | + assert.deepEqual({ |
| 77 | + Authorization: 'test', |
| 78 | + Accept: 'application/json, text/plain, */*' |
| 79 | + }, _this.requests[0].requestHeaders); |
| 80 | + _this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify(p1)); |
| 81 | + }, 10); |
22 | 82 | }); |
23 | 83 | }); |
0 commit comments