Skip to content

Commit d76fb46

Browse files
committed
feat(integration): add fastify to the integration test suite
1 parent ea92da3 commit d76fb46

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

__tests__/integrations/node.Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ WORKDIR /src/packages/node/examples/hapi
1616
ADD packages/node/examples/hapi/package*.json ./
1717
RUN npm ci
1818

19+
WORKDIR /src/packages/node/examples/fastify
20+
ADD packages/node/examples/fastify/package*.json ./
21+
RUN npm ci
22+
1923
# Install top level dependencies
2024
WORKDIR /src
2125
ADD __tests__ ./__tests__

docker-compose.yml

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ services:
2424
environment:
2525
- EXAMPLE_SERVER=node ./packages/node/examples/hapi/index.js
2626

27+
integration_metrics_node_fastify:
28+
build:
29+
context: .
30+
dockerfile: ./__tests__/integrations/node.Dockerfile
31+
command: npm run test:integration-metrics
32+
environment:
33+
- EXAMPLE_SERVER=node ./packages/node/examples/fastify/index.js
34+
2735
integration_metrics_dotnet_v6.0:
2836
build:
2937
context: .
+17-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
import Fastify from 'fastify';
22
import readmeio from 'readmeio';
33

4+
if (!process.env.README_API_KEY) {
5+
// eslint-disable-next-line no-console
6+
console.error('Missing `README_API_KEY` environment variable');
7+
process.exit(1);
8+
}
9+
410
const fastify = Fastify({
511
logger: true,
612
});
13+
const port = process.env.PORT || 4000;
714

8-
fastify.decorateRequest('readmeStartTime', '');
15+
fastify.decorateRequest('readmeStartTime', null);
16+
fastify.decorateReply('payload', null);
917
fastify.addHook('onRequest', async request => {
1018
request.readmeStartTime = new Date();
1119
});
1220

1321
fastify.addHook('onSend', async (request, reply, payload) => {
22+
// eslint-disable-next-line no-param-reassign
23+
reply.payload = payload;
24+
});
25+
26+
fastify.addHook('onResponse', async (request, reply) => {
1427
const payloadData = {
1528
// User's API Key
1629
apiKey: 'owlbert-api-key',
@@ -21,16 +34,16 @@ fastify.addHook('onSend', async (request, reply, payload) => {
2134

2235
startedDateTime: new Date(request.readmeStartTime),
2336
responseEndDateTime: new Date(),
24-
responseBody: payload,
37+
responseBody: reply.payload,
2538
};
2639

27-
readmeio.log(process.env.README_API_KEY, request, reply, payloadData, { fireAndForget: true });
40+
readmeio.log(process.env.README_API_KEY, request.raw, reply, payloadData, { fireAndForget: true });
2841
});
2942

3043
fastify.get('/', (request, reply) => {
3144
reply.send({ message: 'hello world' });
3245
});
3346

34-
fastify.listen({ port: 3000 }, err => {
47+
fastify.listen({ port }, err => {
3548
if (err) throw err;
3649
});

packages/node/src/lib/process-response.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Entry } from 'har-format';
22
import type { LogOptions } from './construct-payload';
33
import type { ServerResponse } from 'http';
4+
import { STATUS_CODES } from 'http';
45
import removeProperties from 'lodash/omit';
56
import removeOtherProperties from 'lodash/pick';
67

@@ -53,7 +54,14 @@ export default function processResponse(
5354

5455
return {
5556
status: res.statusCode,
56-
statusText: res.statusMessage,
57+
// In fastify, at the point where we have to fetch the statusMessage
58+
// from the response, it is not set because the headers haven't been
59+
// flushed yet, so we need to fetch the default value from Node.
60+
// https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_response_statusmessage
61+
//
62+
// This is the same thing that Node.js does internally:
63+
// https://github.com/nodejs/node/blob/9b8ba2536044ae08a1cd747a3aa52df7d1815e7e/lib/_http_server.js#L318
64+
statusText: res.statusMessage || STATUS_CODES[res.statusCode],
5765
headers: objectToArray(headers),
5866
content: {
5967
text: JSON.stringify(body),

0 commit comments

Comments
 (0)