Skip to content

Commit 05d2609

Browse files
author
Olof Moriya
committed
fix(sdk-middleware-http) Remove dependence on content in ok result
It's not uncommon to use return `Accepted()` or return `Ok()` in asp core and even though specifying the content type the return will be empty making json() throw. The content length header is stripped in the proxy so the simplest solution for handling a common server solution for empty 202 and 200 seems to be to catch the exception and continue with an empty body.
1 parent 693f630 commit 05d2609

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ export default function createHttpMiddleware({
146146
return
147147
}
148148

149-
res.json().then((result: Object) => {
149+
res.text().then((result: Object) => {
150150
const parsedResponse: Object = {
151151
...response,
152-
body: result,
152+
body: result.length > 0 ? JSON.parse(result) : {},
153153
statusCode: res.status,
154154
}
155155

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,4 +952,34 @@ describe('Http', () => {
952952

953953
httpMiddleware(next)(request, response)
954954
}))
955+
956+
test('execute a post request with empty 202 response', () =>
957+
new Promise((resolve, reject) => {
958+
const request = createTestRequest({
959+
uri: '/foo/bar',
960+
method: 'POST',
961+
})
962+
const response = { resolve, reject }
963+
const next = (req, res) => {
964+
expect(res).toEqual({
965+
...response,
966+
body: {},
967+
statusCode: 202,
968+
})
969+
resolve()
970+
}
971+
// Use default options
972+
const httpMiddleware = createHttpMiddleware({
973+
host: testHost,
974+
fetch,
975+
})
976+
nock(testHost)
977+
.defaultReplyHeaders({
978+
'Content-Type': 'application/json',
979+
})
980+
.post('/foo/bar')
981+
.reply(202, undefined)
982+
983+
httpMiddleware(next)(request, response)
984+
}))
955985
})

0 commit comments

Comments
 (0)