Skip to content

Commit a4bb20d

Browse files
authored
Merge branch 'master' into 1647-non-vulnerable-node
2 parents 9d687f9 + 4957c2b commit a4bb20d

File tree

5 files changed

+116
-8
lines changed

5 files changed

+116
-8
lines changed

docs/sdk/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ If we take a step back and look at the general requirement, at the end we simply
2323

2424
In this example (integration test) we are going to make some requests to the `/channels` API endpoint. For that we need to be able to make actual requests (`http` middleware) as well as to authenticate the requests using the API Client Credentials Flow (`auth` middleware).
2525

26-
The `queue` middleware is not really necessary in this simple example but it is usually useful to limit a bit the number of concurrent requests.
26+
The `queue` middleware is not really necessary in this simple example but it is usually useful to limit a bit the number of concurrent requests and should be place before the `http` middleware.
2727

2828
The `api-request-builder` package comes in handy to easily construct the request _URI_ but it is not really necessary as the _URI_ could be also typed manually.
2929

@@ -54,7 +54,7 @@ const queueMiddleware = createQueueMiddleware({
5454
concurrency: 5,
5555
})
5656
const client = createClient({
57-
middlewares: [authMiddleware, httpMiddleware, queueMiddleware],
57+
middlewares: [authMiddleware, queueMiddleware, httpMiddleware],
5858
})
5959

6060
describe('Channels', () => {

integration-tests/cli/helpers/personal-data-erasure.data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const customLineItem = [
6363
slug: 'mySlug',
6464
taxCategory: {
6565
typeId: 'tax-category',
66-
id: 'fd890829-7554-4c22-a806-d6c6dcdbb0c5',
66+
id: 'd205cc42-e399-424a-b6fc-0ef44772d6bc',
6767
},
6868
},
6969
],

integration-tests/cli/personal-data-erasure.it.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,19 @@ describe('personal data erasure', () => {
105105
it('should get data on the CTP', async () => {
106106
const data = await personalDataErasure.getCustomerData(customerId)
107107

108-
expect(data).toHaveLength(10)
108+
expect(data).toHaveLength(11)
109+
expect(data).toContainEqual(expect.objectContaining({type: 'CartCreated'}))
110+
expect(data).toContainEqual(expect.objectContaining({type: 'PaymentCreated'}))
111+
expect(data).toContainEqual(expect.objectContaining({type: 'CustomerCreated'}))
112+
expect(data).toContainEqual(expect.objectContaining({type: 'ReviewCreated'}))
113+
expect(data).toContainEqual(expect.objectContaining({type: 'OrderCreated'}))
114+
expect(data).toContainEqual(expect.objectContaining({type: 'CartCreated'}))
115+
expect(data).toContainEqual(expect.objectContaining({type: 'Order'}))
116+
expect(data).toContainEqual(expect.objectContaining({type: 'Cart'}))
117+
expect(data).toContainEqual(expect.objectContaining({email: '[email protected]'}))
118+
expect(data).toContainEqual(expect.objectContaining({amountPlanned: { type: 'centPrecision', currencyCode: 'EUR', centAmount: 100, fractionDigits: 2}}))
119+
expect(data).toContainEqual(expect.objectContaining({name: {de: 'deutscherListenName', en: 'englishListName'}}))
120+
expect(data).toContainEqual(expect.objectContaining({text: 'Review text'}))
109121
})
110122
})
111123

packages/sdk-middleware-http/src/http.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,18 @@ export default function createHttpMiddleware({
6868
} = {},
6969
fetch: fetcher,
7070
abortController: _abortController,
71-
getAbortController
71+
getAbortController,
7272
}: HttpMiddlewareOptions): Middleware {
7373
if (!fetcher && typeof fetch === 'undefined')
7474
throw new Error(
7575
'`fetch` is not available. Please pass in `fetch` as an option or have it globally available.'
7676
)
77-
if (timeout && !getAbortController && !_abortController && typeof AbortController === 'undefined')
77+
if (
78+
timeout &&
79+
!getAbortController &&
80+
!_abortController &&
81+
typeof AbortController === 'undefined'
82+
)
7883
throw new Error(
7984
'`AbortController` is not available. Please pass in `getAbortController` as an option or have AbortController globally available when using timeout.'
8085
)
@@ -95,7 +100,10 @@ export default function createHttpMiddleware({
95100
let abortController: any
96101
if (timeout || getAbortController || _abortController)
97102
// eslint-disable-next-line
98-
abortController = (getAbortController ? getAbortController(): null) || _abortController || new AbortController()
103+
abortController =
104+
(getAbortController ? getAbortController() : null) ||
105+
_abortController ||
106+
new AbortController()
99107

100108
const url = host.replace(/\/$/, '') + request.uri
101109
const body =
@@ -148,9 +156,31 @@ export default function createHttpMiddleware({
148156
}
149157

150158
res.text().then((result: Object) => {
159+
// Try to parse the response as JSON
160+
let parsed
161+
try {
162+
parsed = result.length > 0 ? JSON.parse(result) : {}
163+
} catch (err) {
164+
if (enableRetry && retryCount < maxRetries) {
165+
setTimeout(
166+
executeFetch,
167+
calcDelayDuration(
168+
retryCount,
169+
retryDelay,
170+
maxRetries,
171+
backoff,
172+
maxDelay
173+
)
174+
)
175+
retryCount += 1
176+
return
177+
}
178+
parsed = result
179+
}
180+
151181
const parsedResponse: Object = {
152182
...response,
153-
body: result.length > 0 ? JSON.parse(result) : {},
183+
body: parsed,
154184
statusCode: res.status,
155185
}
156186

packages/sdk-middleware-http/test/http.spec.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,72 @@ describe('Http', () => {
7070
httpMiddleware(next)(request, response)
7171
}))
7272

73+
test("execute a get request which doesn't return a json response", () =>
74+
new Promise((resolve, reject) => {
75+
const request = createTestRequest({
76+
uri: '/foo/bar',
77+
})
78+
const response = { resolve, reject }
79+
const next = (req, res) => {
80+
expect(res).toEqual({
81+
...response,
82+
body: 'not json response',
83+
statusCode: 200,
84+
})
85+
resolve()
86+
}
87+
// Use default options
88+
const httpMiddleware = createHttpMiddleware({
89+
host: testHost,
90+
fetch,
91+
})
92+
nock(testHost)
93+
.defaultReplyHeaders({
94+
'Content-Type': 'application/json',
95+
})
96+
.get('/foo/bar')
97+
.reply(200, 'not json response')
98+
99+
httpMiddleware(next)(request, response)
100+
}))
101+
102+
test("execute a get request which doesn't return a json response with retry", () =>
103+
new Promise((resolve, reject) => {
104+
const request = createTestRequest({
105+
uri: '/foo/bar',
106+
})
107+
const response = { resolve, reject }
108+
const next = (req, res) => {
109+
expect(res).toEqual({
110+
...response,
111+
body: { foo: 'bar' },
112+
statusCode: 200,
113+
})
114+
resolve()
115+
}
116+
// Use default options
117+
const httpMiddleware = createHttpMiddleware({
118+
host: testHost,
119+
fetch,
120+
enableRetry: true,
121+
})
122+
nock(testHost)
123+
.defaultReplyHeaders({
124+
'Content-Type': 'application/json',
125+
})
126+
.get('/foo/bar')
127+
.reply(200, 'not json response')
128+
129+
nock(testHost)
130+
.defaultReplyHeaders({
131+
'Content-Type': 'application/json',
132+
})
133+
.get('/foo/bar')
134+
.reply(200, { foo: 'bar' })
135+
136+
httpMiddleware(next)(request, response)
137+
}))
138+
73139
test('execute a get request with timeout (success)', () =>
74140
new Promise((resolve, reject) => {
75141
const request = createTestRequest({

0 commit comments

Comments
 (0)