Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions packages/clients/src/scw/fetch/__tests__/response-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ const unmarshalJSON = (obj: unknown) => {
return obj
}

const makeValidFetchResponse = (
contentType = 'application/json',
const makeValidJSONResponse = (
value: JSON | null = SIMPLE_REQ_BODY,
status = 200,
) =>
new Response(value !== null ? convertObjToBuffer(value) : value, {
headers: { 'Content-Type': contentType },
headers: { 'Content-Type': 'application/json' },
status,
})

const makeValidTextResponse = (value: string | null, status = 200) =>
new Response(value, {
headers: { 'Content-Type': 'plain/text' },
status,
})

Expand Down Expand Up @@ -82,7 +87,7 @@ describe(`responseParser`, () => {
})

it(`triggers an error for unsuccessful unmarshalling`, async () => {
const validResponse = makeValidFetchResponse()
const validResponse = makeValidJSONResponse()

await expect(
responseParser(() => {
Expand All @@ -91,7 +96,7 @@ describe(`responseParser`, () => {
).rejects.toThrow(
new ScalewayError(
validResponse.status,
`could not parse application/json response: couldn't unwrap response value`,
`could not parse 'application/json' response: couldn't unwrap response value`,
),
)

Expand All @@ -103,42 +108,25 @@ describe(`responseParser`, () => {
).rejects.toThrow(
new ScalewayError(
validResponse.status,
`could not parse application/json response`,
),
)
})

it(`triggers an error for invalid content type`, async () => {
const cType = 'plain/text'
const invalidResponse = makeValidFetchResponse(cType)

return expect(parseJson(invalidResponse)).rejects.toThrow(
new ScalewayError(
invalidResponse.status,
`invalid content type ${cType}`,
`could not parse 'application/json' response`,
),
)
})

it(`triggers an error for undefined content type`, async () => {
const invalidResponse = new Response(convertObjToBuffer(SIMPLE_REQ_BODY), {
headers: {},
status: 200,
})
it(`returns the response as-if for unknown content type`, async () => {
const textResponse = makeValidTextResponse('text-body')

return expect(parseJson(invalidResponse)).rejects.toThrow(
new ScalewayError(invalidResponse.status, `invalid content type`),
)
return expect(parseAsIs(textResponse)).resolves.toBe('text-body')
})

it(`returns a simple object for a valid 'Response' object`, async () =>
expect(parseJson(makeValidFetchResponse())).resolves.toMatchObject(
expect(parseJson(makeValidJSONResponse())).resolves.toMatchObject(
SIMPLE_REQ_BODY,
))

it(`returns undefined for a 204 status code, even if content-type is json`, async () =>
expect(
parseAsIs(makeValidFetchResponse('application/json', null, 204)),
parseAsIs(makeValidJSONResponse(null, 204)),
).resolves.toBeUndefined())
})

Expand Down
29 changes: 13 additions & 16 deletions packages/clients/src/scw/fetch/response-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,22 @@ export const responseParser =
if (response.ok) {
if (response.status === 204) return unmarshaller(undefined)
const contentType = response.headers.get('Content-Type')
switch (contentType) {
case 'application/json':
try {
try {
switch (contentType) {
case 'application/json':
return unmarshaller(
fixLegacyTotalCount(await response.json(), response.headers),
)
} catch (err) {
throw new ScalewayError(
response.status,
`could not parse ${contentType} response${
err instanceof Error ? `: ${err.message}` : ''
}`,
)
}
default:
throw new ScalewayError(
response.status,
`invalid content type ${contentType ?? ''}`.trim(),
)
default:
return unmarshaller(await response.text())
}
} catch (err) {
throw new ScalewayError(
response.status,
`could not parse '${contentType ?? ''}' response${
err instanceof Error ? `: ${err.message}` : ''
}`,
)
}
}

Expand Down