Skip to content

Commit

Permalink
feat(integration): add fastify to the integration test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
domharrington committed Aug 10, 2022
1 parent ea92da3 commit d76fb46
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions __tests__/integrations/node.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ WORKDIR /src/packages/node/examples/hapi
ADD packages/node/examples/hapi/package*.json ./
RUN npm ci

WORKDIR /src/packages/node/examples/fastify
ADD packages/node/examples/fastify/package*.json ./
RUN npm ci

# Install top level dependencies
WORKDIR /src
ADD __tests__ ./__tests__
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ services:
environment:
- EXAMPLE_SERVER=node ./packages/node/examples/hapi/index.js

integration_metrics_node_fastify:
build:
context: .
dockerfile: ./__tests__/integrations/node.Dockerfile
command: npm run test:integration-metrics
environment:
- EXAMPLE_SERVER=node ./packages/node/examples/fastify/index.js

integration_metrics_dotnet_v6.0:
build:
context: .
Expand Down
21 changes: 17 additions & 4 deletions packages/node/examples/fastify/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import Fastify from 'fastify';
import readmeio from 'readmeio';

if (!process.env.README_API_KEY) {
// eslint-disable-next-line no-console
console.error('Missing `README_API_KEY` environment variable');
process.exit(1);
}

const fastify = Fastify({
logger: true,
});
const port = process.env.PORT || 4000;

fastify.decorateRequest('readmeStartTime', '');
fastify.decorateRequest('readmeStartTime', null);
fastify.decorateReply('payload', null);
fastify.addHook('onRequest', async request => {
request.readmeStartTime = new Date();
});

fastify.addHook('onSend', async (request, reply, payload) => {
// eslint-disable-next-line no-param-reassign
reply.payload = payload;
});

fastify.addHook('onResponse', async (request, reply) => {
const payloadData = {
// User's API Key
apiKey: 'owlbert-api-key',
Expand All @@ -21,16 +34,16 @@ fastify.addHook('onSend', async (request, reply, payload) => {

startedDateTime: new Date(request.readmeStartTime),
responseEndDateTime: new Date(),
responseBody: payload,
responseBody: reply.payload,
};

readmeio.log(process.env.README_API_KEY, request, reply, payloadData, { fireAndForget: true });
readmeio.log(process.env.README_API_KEY, request.raw, reply, payloadData, { fireAndForget: true });
});

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

fastify.listen({ port: 3000 }, err => {
fastify.listen({ port }, err => {
if (err) throw err;
});
10 changes: 9 additions & 1 deletion packages/node/src/lib/process-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Entry } from 'har-format';
import type { LogOptions } from './construct-payload';
import type { ServerResponse } from 'http';
import { STATUS_CODES } from 'http';
import removeProperties from 'lodash/omit';
import removeOtherProperties from 'lodash/pick';

Expand Down Expand Up @@ -53,7 +54,14 @@ export default function processResponse(

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

0 comments on commit d76fb46

Please sign in to comment.