Skip to content

Commit

Permalink
test: cover testing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Jun 20, 2020
1 parent 56b4759 commit 689b20a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"doctoc": "1.4.0",
"dripip": "0.9.0",
"jest": "26.0.1",
"nock": "^12.0.3",
"node-pty": "0.9.0",
"prettier": "2.0.5",
"ts-jest": "26.0.0"
Expand Down
10 changes: 9 additions & 1 deletion src/lib/graphql-client/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import nock from 'nock'
import { GraphQLClient } from './'

let client: GraphQLClient

beforeEach(() => {
client = new GraphQLClient('foo.bar')
client = new GraphQLClient('http://foo.bar')
})

describe('headers', () => {
Expand All @@ -28,4 +29,11 @@ describe('headers', () => {
client.headers.add(...input)
expect(client.headers.get(header)).toEqual(output)
})

it('headers are sent with request', async () => {
const nockreq = nock('http://foo.bar').post('/').matchHeader('foo', 'bar').reply(200, { data: {} })
client.headers.set('foo', 'bar')
await client.send('')
nockreq.done()
})
})
16 changes: 12 additions & 4 deletions src/lib/graphql-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ export class GraphQLClient {
}

send(queryString: string, variables?: Variables) {
const headers: Record<string, string> = {}
for (const [key, value] of this.fetchHeaders.entries()) {
headers[key] = value
}
let headers = fetchHeadersToObject(this.fetchHeaders)
const url = this.url
const client = new GQLR.GraphQLClient(url, { headers })
return client.request(queryString, variables)
Expand Down Expand Up @@ -83,3 +80,14 @@ export class Headers {
return this.fetchHeaders.entries()
}
}

/**
* Convert fetch headers to plain object.
*/
function fetchHeadersToObject(headers: globalThis.Headers): Record<string, string> {
let obj: Record<string, string> = {}
for (const [name, value] of headers.entries()) {
obj[name] = value
}
return obj
}
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4000,7 +4000,7 @@ json-schema@0.2.3:
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
integrity sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=

json-stringify-safe@~5.0.1:
json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=
Expand Down Expand Up @@ -4461,6 +4461,16 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

nock@^12.0.3:
version "12.0.3"
resolved "https://registry.yarnpkg.com/nock/-/nock-12.0.3.tgz#83f25076dbc4c9aa82b5cdf54c9604c7a778d1c9"
integrity sha512-QNb/j8kbFnKCiyqi9C5DD0jH/FubFGj5rt9NQFONXwQm3IPB0CULECg/eS3AU1KgZb/6SwUa4/DTRKhVxkGABw==
dependencies:
debug "^4.1.0"
json-stringify-safe "^5.0.1"
lodash "^4.17.13"
propagate "^2.0.0"

node-fetch@2.6.0, node-fetch@^2.3.0, node-fetch@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
Expand Down Expand Up @@ -4989,6 +4999,11 @@ prompts@^2.0.1, prompts@^2.3.0:
kleur "^3.0.3"
sisteransi "^1.0.4"

propagate@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==

proxy-addr@~2.0.5:
version "2.0.6"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.6.tgz#fdc2336505447d3f2f2c638ed272caf614bbb2bf"
Expand Down

0 comments on commit 689b20a

Please sign in to comment.