Skip to content

Commit

Permalink
fix: Issue when serving the GrappQL option object using a function in…
Browse files Browse the repository at this point in the history
… serveHTTP
  • Loading branch information
nicolasdao committed Aug 14, 2017
1 parent ca0313d commit c5794fa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exports.serveHTTP = (arg1, arg2, arg3) => {
appConfig = arg3
}
else {
if (arg1.length != undefined)
if (typeOfArg1 == 'object' && arg1.length >= 0)
throw new Error('The first argument of the \'serveHTTP\' method cannot be an array. It must either be a route, a graphQL options object, or a function similar to (req, res, params) => ... that returns a graphQL option object or a promise returning a graphql object.')
if (typeOfArg1 == 'object' && !arg1.schema)
throw new Error('If the first argument of the \'serveHTTP\' method is a graphQL object, then it must contain a valid property called \'schema\'.')
Expand Down
50 changes: 50 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,55 @@ describe('index', () =>
})
.catch(() => assert.equal(1,2, `This request should have succeeded.`))

return Promise.all([result_01, result_02])
})))

/*eslint-disable */
describe('index', () =>
describe('#serveHTTP: 05', () =>
it(`Should succeed if the first argument if a function returning either a graphQl option object or a Promise that returns a graphQL option object.`, () => {
/*eslint-enable */
const req_01 = httpMocks.createRequest({
method: 'GET',
headers: {
origin: 'http://localhost:8080',
referer: 'http://localhost:8080'
},
_parsedUrl: {
pathname: '/users/graphiql'
}
})
const res_01 = httpMocks.createResponse()
const req_02 = httpMocks.createRequest({
method: 'GET',
headers: {
origin: 'http://localhost:8080',
referer: 'http://localhost:8080'
}
})
const res_02 = httpMocks.createResponse()

const appconfig = {
headers: {
'Access-Control-Allow-Methods': 'GET, HEAD, OPTIONS, POST',
'Access-Control-Allow-Headers': 'Authorization, Content-Type, Origin',
'Access-Control-Allow-Origin': 'http://boris.com, http://localhost:8080',
'Access-Control-Max-Age': '1296000'
}
}

const fn_01 = serveHTTP(() => { schema: {} }, appconfig)
const fn_02 = serveHTTP(() => Promise.resolve({ schema: {} }), appconfig)

const result_01 = fn_01(req_01, res_01).then(() => {
assert.equal(1,1)
})
.catch(() => assert.equal(1,2, `This request should have succeeded.`))

const result_02 = fn_02(req_02, res_02).then(() => {
assert.equal(1,1)
})
.catch(() => assert.equal(1,2, `This request should have succeeded.`))

return Promise.all([result_01, result_02])
})))

0 comments on commit c5794fa

Please sign in to comment.