Skip to content

Commit add509c

Browse files
committed
Merge pull request #68 from contentful/int-test-fixes
Integration test fixes
2 parents 4489ca8 + e14d75a commit add509c

File tree

5 files changed

+121
-6
lines changed

5 files changed

+121
-6
lines changed

lib/create-cda-api.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ export default function createCdaApi (http, resolveLinksGlobalSetting) {
9797
* Gets an Entry
9898
* @memberof CDAClient
9999
* @param {string} id
100+
* @param {Object=} query - Object with search parameters. In this method it's only useful for `locale`.
100101
* @return {Promise<Entities.Entry>} Promise for an Entry
101102
* @example
102103
* client.getEntry('entryId')
103104
* .then(entry => console.log(entry))
104105
*/
105-
function getEntry (id) {
106-
return http.get('entries/' + id)
106+
function getEntry (id, query = {}) {
107+
return http.get('entries/' + id, createRequestConfig({query: query}))
107108
.then(response => wrapEntry(response.data), errorHandler)
108109
}
109110

@@ -127,13 +128,14 @@ export default function createCdaApi (http, resolveLinksGlobalSetting) {
127128
* Gets an Asset
128129
* @memberof CDAClient
129130
* @param {string} id
131+
* @param {Object=} query - Object with search parameters. In this method it's only useful for `locale`.
130132
* @return {Promise<Entities.Asset>} Promise for an Asset
131133
* @example
132134
* client.getAsset('assetId')
133135
* .then(asset => console.log(asset))
134136
*/
135-
function getAsset (id) {
136-
return http.get('assets/' + id)
137+
function getAsset (id, query = {}) {
138+
return http.get('assets/' + id, createRequestConfig({query: query}))
137139
.then(response => wrapAsset(response.data), errorHandler)
138140
}
139141

lib/create-http-client.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import version from '../version'
2+
import qs from 'qs'
23

34
/**
45
* Create pre configured axios instance
@@ -29,6 +30,7 @@ export default function createHttpClient (axios, {space, accessToken, insecure,
2930
return axios.create({
3031
baseURL: `${insecure ? 'http' : 'https'}://${hostname}:${port}/spaces/${space}/`,
3132
headers: headers,
32-
agent: agent
33+
agent: agent,
34+
paramsSerializer: qs.stringify
3335
})
3436
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"babel-runtime": "^6.3.19",
5151
"follow-redirects": "0.0.7",
5252
"json-stringify-safe": "^5.0.1",
53-
"lodash": "^4.2.0"
53+
"lodash": "^4.2.0",
54+
"qs": "^6.1.0"
5455
},
5556
"devDependencies": {
5657
"axios": "^0.9.1",

test/integration/tests.js

+96
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ test('Gets entry', t => {
5151
})
5252
})
5353

54+
test('Gets an entry with a specific locale', t => {
55+
t.plan(1)
56+
return client.getEntry('nyancat', {
57+
locale: 'tlh'
58+
})
59+
.then(entry => {
60+
t.equal(entry.sys.locale, 'tlh')
61+
})
62+
})
63+
5464
test('Gets entries', t => {
5565
t.plan(1)
5666
return client.getEntries()
@@ -59,6 +69,39 @@ test('Gets entries', t => {
5969
})
6070
})
6171

72+
test('Gets entries with a specific locale', t => {
73+
t.plan(2)
74+
return client.getEntries({
75+
locale: 'tlh'
76+
})
77+
.then(response => {
78+
t.equal(response.items[0].sys.locale, 'tlh')
79+
t.ok(response.items, 'items')
80+
})
81+
})
82+
83+
test('Gets entries with a limit parameter', t => {
84+
t.plan(2)
85+
return client.getEntries({
86+
limit: 2
87+
})
88+
.then(response => {
89+
t.ok(response.items, 'items')
90+
t.equal(response.items.length, 2)
91+
})
92+
})
93+
94+
test('Gets entries with a skip parameter', t => {
95+
t.plan(2)
96+
return client.getEntries({
97+
skip: 2
98+
})
99+
.then(response => {
100+
t.ok(response.items, 'items')
101+
t.equal(response.skip, 2)
102+
})
103+
})
104+
62105
test('Gets entries with linked includes', t => {
63106
t.plan(5)
64107
return client.getEntries({include: 2})
@@ -167,6 +210,17 @@ test('Gets entries with inverse exists query', t => {
167210
})
168211
})
169212

213+
test('Gets entries with field link query', t => {
214+
t.plan(1)
215+
return client.getEntries({
216+
content_type: 'cat',
217+
'fields.bestFriend.sys.id': 'happycat'
218+
})
219+
.then(response => {
220+
t.equal(response.items[0].sys.id, 'nyancat', 'returned entry has link to specified linked entry')
221+
})
222+
})
223+
170224
test('Gets entries with gte range query', t => {
171225
t.plan(1)
172226
return client.getEntries({
@@ -283,6 +337,16 @@ test('Gets asset', t => {
283337
})
284338
})
285339

340+
test('Gets an asset with a specific locale', t => {
341+
t.plan(1)
342+
return client.getEntry('jake', {
343+
locale: 'tlh'
344+
})
345+
.then(asset => {
346+
t.equal(asset.sys.locale, 'tlh')
347+
})
348+
})
349+
286350
test('Gets assets', t => {
287351
t.plan(1)
288352
return client.getAssets()
@@ -303,3 +367,35 @@ test('Sync space', t => {
303367
t.equal(response.entries[4].fields.image['en-US'].sys.type, 'Asset', 'links are resolved')
304368
})
305369
})
370+
371+
test('Sync space with token', t => {
372+
t.plan(5)
373+
return client.sync({nextSyncToken: 'w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1wrzCrBzCqMOpZAwOOcOvCcOAwqHDv0XCiMKaOcOxZA8BJUzDr8K-wo1lNx7DnHE'})
374+
.then(response => {
375+
t.ok(response.entries, 'entries')
376+
t.ok(response.assets, 'assets')
377+
t.ok(response.deletedEntries, 'deleted entries')
378+
t.ok(response.deletedAssets, 'deleted assets')
379+
t.ok(response.nextSyncToken, 'next sync token')
380+
})
381+
})
382+
383+
test('Sync spaces assets', t => {
384+
t.plan(3)
385+
return client.sync({initial: true, type: 'Asset'})
386+
.then(response => {
387+
t.ok(response.assets, 'assets')
388+
t.ok(response.deletedAssets, 'deleted assets')
389+
t.ok(response.nextSyncToken, 'next sync token')
390+
})
391+
})
392+
393+
test('Sync space entries by content type', t => {
394+
t.plan(3)
395+
return client.sync({initial: true, type: 'Entry', content_type: 'dog'})
396+
.then(response => {
397+
t.ok(response.entries, 'entries')
398+
t.ok(response.deletedEntries, 'deleted entries')
399+
t.ok(response.nextSyncToken, 'next sync token')
400+
})
401+
})
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import test from 'tape'
2+
3+
import createRequestConfig from '../../lib/create-request-config'
4+
5+
test('Create request config', t => {
6+
const config = createRequestConfig({
7+
resolveLinks: true,
8+
query: {}
9+
})
10+
11+
t.ok(config.params, 'params property exist')
12+
t.notOk(config.params.resolveLinks, 'resolveLinks property is removed from query')
13+
t.end()
14+
})

0 commit comments

Comments
 (0)