Skip to content

Commit

Permalink
fallback to arraybuffer if content is neither text nor json
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Apr 5, 2023
1 parent 9b30474 commit 5510cb0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
10 changes: 6 additions & 4 deletions egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"entry": "./mod.ts",
"description": "🐕 Superdeno-like superagent testing library based on Fetch API. Ported from node-supertest-fetch.",
"homepage": "https://github.com/deno-libs/superfetch",
"version": "0.1.6",
"releaseType": null,
"version": "1.0.1",
"releaseType": "patch",
"unstable": false,
"unlisted": false,
"files": [
Expand All @@ -14,7 +14,9 @@
"LICENSE"
],
"ignore": [],
"checkFormat": false,
"checkFormat": true,
"checkTests": "deno test --allow-net",
"check": true
"check": true,
"checkAll": true,
"repository": "https://github.com/deno-libs/superfetch"
}
16 changes: 10 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ const makeFetchPromise = (handlerOrListener: HandlerOrListener) => {
`http://localhost:${port}${url}`,
params,
)
const data = res.headers.get('Content-Type') === 'application/json'
? await res.json()
: await res.text()
let data: unknown
const ct = res.headers.get('Content-Type')
if (ct === 'application/json') data = await res.json()
else if (ct?.includes('text')) data = await res.text()
else data = await res.arrayBuffer()

resolve({ res, data })
Deno.close(conn.rid + 1)
Expand Down Expand Up @@ -45,9 +47,11 @@ const makeFetchPromise = (handlerOrListener: HandlerOrListener) => {
`http://localhost:${port}${url}`,
params,
)
const data = res.headers.get('Content-Type') === 'application/json'
? await res.json()
: await res.text()
let data: unknown
const ct = res.headers.get('Content-Type')
if (ct === 'application/json') data = await res.json()
else if (ct?.includes('text')) data = await res.text()
else data = await res.arrayBuffer()

resolve({ res, data })
Deno.close(conn.rid + 1)
Expand Down
18 changes: 18 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ describe('makeFetch', () => {

res.expect('Hello World')
})
it('should parse JSON if response is JSON', async () => {
const handler: Handler = () =>
new Response(JSON.stringify({ hello: 'world' }), {
headers: { 'Content-Type': 'application/json' },
})
const fetch = makeFetch(handler)
const res = await fetch('/')

res.expect({ hello: 'world' })
})
it('should fallback to arraybuffer', async () => {
const file = await Deno.readFile('README.md')
const handler: Handler = () => new Response(file)
const fetch = makeFetch(handler)
const res = await fetch('/')

res.expect(file.buffer)
})
})
describe('expectStatus', () => {
it('should pass with a correct status', async () => {
Expand Down

0 comments on commit 5510cb0

Please sign in to comment.