Skip to content

Commit 5510cb0

Browse files
author
v1rtl
committed
fallback to arraybuffer if content is neither text nor json
1 parent 9b30474 commit 5510cb0

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

egg.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"entry": "./mod.ts",
55
"description": "🐕 Superdeno-like superagent testing library based on Fetch API. Ported from node-supertest-fetch.",
66
"homepage": "https://github.com/deno-libs/superfetch",
7-
"version": "0.1.6",
8-
"releaseType": null,
7+
"version": "1.0.1",
8+
"releaseType": "patch",
99
"unstable": false,
1010
"unlisted": false,
1111
"files": [
@@ -14,7 +14,9 @@
1414
"LICENSE"
1515
],
1616
"ignore": [],
17-
"checkFormat": false,
17+
"checkFormat": true,
1818
"checkTests": "deno test --allow-net",
19-
"check": true
19+
"check": true,
20+
"checkAll": true,
21+
"repository": "https://github.com/deno-libs/superfetch"
2022
}

mod.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ const makeFetchPromise = (handlerOrListener: HandlerOrListener) => {
1313
`http://localhost:${port}${url}`,
1414
params,
1515
)
16-
const data = res.headers.get('Content-Type') === 'application/json'
17-
? await res.json()
18-
: await res.text()
16+
let data: unknown
17+
const ct = res.headers.get('Content-Type')
18+
if (ct === 'application/json') data = await res.json()
19+
else if (ct?.includes('text')) data = await res.text()
20+
else data = await res.arrayBuffer()
1921

2022
resolve({ res, data })
2123
Deno.close(conn.rid + 1)
@@ -45,9 +47,11 @@ const makeFetchPromise = (handlerOrListener: HandlerOrListener) => {
4547
`http://localhost:${port}${url}`,
4648
params,
4749
)
48-
const data = res.headers.get('Content-Type') === 'application/json'
49-
? await res.json()
50-
: await res.text()
50+
let data: unknown
51+
const ct = res.headers.get('Content-Type')
52+
if (ct === 'application/json') data = await res.json()
53+
else if (ct?.includes('text')) data = await res.text()
54+
else data = await res.arrayBuffer()
5155

5256
resolve({ res, data })
5357
Deno.close(conn.rid + 1)

mod_test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ describe('makeFetch', () => {
1616

1717
res.expect('Hello World')
1818
})
19+
it('should parse JSON if response is JSON', async () => {
20+
const handler: Handler = () =>
21+
new Response(JSON.stringify({ hello: 'world' }), {
22+
headers: { 'Content-Type': 'application/json' },
23+
})
24+
const fetch = makeFetch(handler)
25+
const res = await fetch('/')
26+
27+
res.expect({ hello: 'world' })
28+
})
29+
it('should fallback to arraybuffer', async () => {
30+
const file = await Deno.readFile('README.md')
31+
const handler: Handler = () => new Response(file)
32+
const fetch = makeFetch(handler)
33+
const res = await fetch('/')
34+
35+
res.expect(file.buffer)
36+
})
1937
})
2038
describe('expectStatus', () => {
2139
it('should pass with a correct status', async () => {

0 commit comments

Comments
 (0)