generated from apollographql/typescript-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c57c0ad
commit 8a5b3d0
Showing
13 changed files
with
517 additions
and
127 deletions.
There are no files selected for viewing
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
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
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,46 @@ | ||
import { ApolloServer, ApolloServerOptions, BaseContext } from '@apollo/server'; | ||
import { | ||
CreateServerForIntegrationTestsOptions, | ||
defineIntegrationTestSuite, | ||
} from '@apollo/server-integration-testsuite'; | ||
import { createServer } from 'http'; | ||
import { v4 } from '..'; | ||
import { createMockServer, urlForHttpServer } from './mockServer-v4'; | ||
|
||
describe('Azure Functions v4', () => { | ||
defineIntegrationTestSuite( | ||
async function ( | ||
serverOptions: ApolloServerOptions<BaseContext>, | ||
testOptions?: CreateServerForIntegrationTestsOptions, | ||
) { | ||
const httpServer = createServer(); | ||
const server = new ApolloServer({ | ||
...serverOptions, | ||
}); | ||
|
||
const handler = testOptions | ||
? v4.startServerAndCreateHandler(server, testOptions) | ||
: v4.startServerAndCreateHandler(server); | ||
|
||
await new Promise<void>((resolve) => { | ||
httpServer.listen({ port: 0 }, resolve); | ||
}); | ||
|
||
httpServer.addListener('request', createMockServer(handler)); | ||
|
||
return { | ||
server, | ||
url: urlForHttpServer(httpServer), | ||
async extraCleanup() { | ||
await new Promise<void>((resolve) => { | ||
httpServer.close(() => resolve()); | ||
}); | ||
}, | ||
}; | ||
}, | ||
{ | ||
serverIsStartedInBackground: true, | ||
noIncrementalDelivery: true, | ||
}, | ||
); | ||
}); |
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,72 @@ | ||
import { | ||
HttpHandler, | ||
InvocationContext, | ||
type HttpMethod, | ||
type HttpRequest, | ||
} from '@azure/functions-v4'; | ||
import type { IncomingMessage, Server, ServerResponse } from 'http'; | ||
import type { AddressInfo } from 'net'; | ||
import { Headers, HeadersInit } from 'undici'; | ||
|
||
export function urlForHttpServer(httpServer: Server): string { | ||
const { address, port } = httpServer.address() as AddressInfo; | ||
|
||
// Convert IPs which mean "any address" (IPv4 or IPv6) into localhost | ||
// corresponding loopback ip. Note that the url field we're setting is | ||
// primarily for consumption by our test suite. If this heuristic is wrong for | ||
// your use case, explicitly specify a frontend host (in the `host` option | ||
// when listening). | ||
const hostname = address === '' || address === '::' ? 'localhost' : address; | ||
|
||
return `http://${hostname}:${port}`; | ||
} | ||
|
||
export const createMockServer = (handler: HttpHandler) => { | ||
return (req: IncomingMessage, res: ServerResponse) => { | ||
let body = ''; | ||
req.on('data', (chunk) => (body += chunk)); | ||
|
||
req.on('end', async () => { | ||
const azReq: HttpRequest = { | ||
method: (req.method as HttpMethod) || null, | ||
url: new URL(req.url || '', 'http://localhost').toString(), | ||
headers: new Headers(req.headers as HeadersInit), | ||
body, | ||
query: new URLSearchParams(req.url), | ||
params: {}, | ||
user: null, | ||
arrayBuffer: async () => { | ||
return Buffer.from(body).buffer; | ||
}, | ||
text: async () => { | ||
return body; | ||
}, | ||
json: async () => { | ||
return JSON.parse(body); | ||
}, | ||
blob: async () => { | ||
throw new Error('Not implemented'); | ||
}, | ||
bodyUsed: false, | ||
formData: async () => { | ||
throw new Error('Not implemented'); | ||
}, | ||
}; | ||
|
||
const context = new InvocationContext({ | ||
invocationId: 'mock', | ||
functionName: 'mock', | ||
logHandler: console.log, | ||
}); | ||
|
||
const azRes = await handler(azReq, context); | ||
|
||
res.statusCode = azRes.status || 200; | ||
Object.entries(azRes.headers ?? {}).forEach(([key, value]) => { | ||
res.setHeader(key, value!.toString()); | ||
}); | ||
res.write(azRes.body); | ||
res.end(); | ||
}); | ||
}; | ||
}; |
Oops, something went wrong.