-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix not properly parsing content type, usez JSR for deps and add e2e …
…tests
- Loading branch information
1 parent
8d9874d
commit 60ff17a
Showing
5 changed files
with
121 additions
and
108 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { expect } from 'https://deno.land/std@0.210.0/expect/mod.ts' | ||
export { assertEquals } from 'https://deno.land/std@0.210.0/assert/assert_equals.ts' | ||
export { assertMatch } from 'https://deno.land/std@0.210.0/assert/assert_match.ts' | ||
export { assertEquals } from 'jsr:@std/[email protected]/equals' | ||
export { assertMatch } from 'jsr:@std/[email protected]/match' | ||
export { parseMediaType } from 'jsr:@std/[email protected]/parse-media-type' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts' | ||
import { makeFetch } from './mod.ts' | ||
import { GraphQLHTTP } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { buildSchema } from 'npm:[email protected]' | ||
|
||
describe('e2e', () => { | ||
it('should work with GraphQL', async () => { | ||
const schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
} | ||
`) | ||
|
||
const handler = GraphQLHTTP({ | ||
schema, | ||
rootValue: { hello: () => 'Hello World!' }, | ||
}) | ||
|
||
const fetch = makeFetch(handler) | ||
|
||
const res = await fetch('/', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ query: '{hello}' }), | ||
}) | ||
|
||
res.expectBody({ data: { hello: 'Hello World!' } }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { assertEquals, assertMatch } from './deps.ts' | ||
import { assertEquals, assertMatch, parseMediaType } from './deps.ts' | ||
import { Handler, HandlerOrListener } from './types.ts' | ||
|
||
// credit - 'https://deno.land/x/[email protected]/mod.ts' | ||
|
@@ -39,9 +39,11 @@ const fetchEndpoint = async ( | |
const res = await fetch(`http://localhost:${port}${url}`, params) | ||
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 if (ct === null) data = await res.text() | ||
if (ct === null) return { data: await res.text(), res } | ||
const [mediaType] = parseMediaType(ct) | ||
|
||
if (mediaType === 'application/json') data = await res.json() | ||
else if (mediaType.includes('text')) data = await res.text() | ||
else data = await res.arrayBuffer() | ||
return { res, data } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { describe, it } from 'https://deno.land/std@0.210.0/testing/bdd.ts' | ||
import { expect } from './deps.ts' | ||
import { describe, it } from 'https://deno.land/std@0.224.0/testing/bdd.ts' | ||
import { expect } from 'jsr:@std/[email protected]/expect' | ||
import { makeFetch } from './mod.ts' | ||
import { Handler } from './types.ts' | ||
import { AssertionError } from 'https://deno.land/std@0.210.0/assert/assertion_error.ts' | ||
import { AssertionError } from 'jsr:@std/[email protected]/assertion-error' | ||
|
||
// this simulates the listener | ||
class PseudoListener { | ||
|
@@ -23,7 +23,7 @@ class PseudoListener { | |
this.rid = this.#listener.rid | ||
} | ||
|
||
fetchRandomPort = () => { | ||
fetchRandomPort() { | ||
return Math.round(Math.random() * (9000 - 2000)) + 2000 | ||
} | ||
|
||
|
@@ -53,6 +53,18 @@ describe('makeFetch', () => { | |
|
||
res.expect('Hello World') | ||
}) | ||
it('should not crash with text/plain', async () => { | ||
const handler: Handler = () => | ||
new Response('Hello World', { | ||
headers: { | ||
'Content-Type': 'text/plain;charset=UTF-8', | ||
}, | ||
}) | ||
const fetch = makeFetch(handler) | ||
const res = await fetch('/') | ||
|
||
res.expect('Hello World') | ||
}) | ||
it('should parse JSON if response is JSON', async () => { | ||
const handler: Handler = () => | ||
new Response(JSON.stringify({ hello: 'world' }), { | ||
|