Skip to content

Commit

Permalink
test: add test for request payload
Browse files Browse the repository at this point in the history
  • Loading branch information
therightstuff committed Nov 2, 2023
1 parent 84b05e1 commit 3493dcf
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"log-timestamp": "^0.3.0",
"mock-fs": "^5.2.0",
"mock-http-server": "^1.4.5",
"node-fetch": "^2.7.0",
"node-notifier": "^10.0.0",
"npm-install-peers": "^1.2.2",
"prettier": "^2.2.1",
Expand Down
7 changes: 6 additions & 1 deletion test/instrumentations/fastify/app/fastify_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ fastify.get('/', async (request, reply) => {
});

fastify.get('/basic', async (request, reply) => {
await tracerProvider.forceFlush();
reply.header('Content-Type', 'text/plain').send('Hello world');
});

fastify.post('/echo', async (request, reply) => {
reply
.header('Content-Type', 'text/plain')
.send(typeof request.body === 'string' ? request.body : JSON.stringify(request.body));
});

fastify.get('/quit', async (request, reply) => {
console.error('Received quit command');
await tracerProvider.forceFlush();
Expand Down
67 changes: 67 additions & 0 deletions test/instrumentations/fastify/fastify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,72 @@ describe.each(versionsToTest(INSTRUMENTATION_NAME, INSTRUMENTATION_NAME))(
});
}
);

itTest(
{
testName: `echo: ${versionToTest}`,
packageName: INSTRUMENTATION_NAME,
version: versionToTest,
timeout: TEST_TIMEOUT,
},
async function () {
const exporterFile = `${SPANS_DIR}/echo.${INSTRUMENTATION_NAME}@${versionToTest}.json`;

testApp = new TestApp(TEST_APP_DIR, INSTRUMENTATION_NAME, exporterFile, {
OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT: '4096',
});

const echoResponse = await testApp.invokePostPath('/echo', { some: 'data' });
expect(echoResponse.status).toEqual(200);
expect(await echoResponse.json()).toMatchObject({ some: 'data' });

const spans = await testApp.getFinalSpans(2);

expect(getSpanByKind(spans, SpanKind.SERVER)).toMatchObject({
traceId: expect.any(String),
name: 'POST /echo',
id: expect.any(String),
kind: SpanKind.SERVER,
timestamp: expect.any(Number),
duration: expect.any(Number),
resource: expectedResourceAttributes,
attributes: {
'http.method': 'POST',
'http.target': '/echo',
'http.host': expect.stringMatching(/localhost:\d+/),
'http.scheme': 'http',
'net.peer.ip': expect.any(String),
'http.route': '/echo',
'http.status_code': 200,
},
status: {
code: SpanStatusCode.UNSET,
},
events: [],
});

expect(getSpanByKind(spans, SpanKind.INTERNAL)).toMatchObject({
traceId: expect.any(String),
parentId: expect.any(String),
name: expect.stringMatching(/request handler - .+/),
id: expect.any(String),
kind: SpanKind.INTERNAL,
timestamp: expect.any(Number),
duration: expect.any(Number),
resource: expectedResourceAttributes,
attributes: {
'http.request.query': '{}',
'http.request.headers': expect.stringMatching(/\{.*\}/),
// 'http.response.headers': expect.stringMatching(/\{.*\}/),
// 'http.response.body': '"Hello world"',
'http.route': '/echo',
},
status: {
code: SpanStatusCode.UNSET,
},
events: [],
});
}
);
}
);
11 changes: 11 additions & 0 deletions test/utils/test-apps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChildProcessWithoutNullStreams, execSync, spawn } from 'child_process';
import { existsSync, unlinkSync } from 'fs';
import fetch from 'node-fetch';
import waitOn from 'wait-on';
import { Span, readSpanDump } from './spans';
import { sleep } from './time';
Expand Down Expand Up @@ -172,6 +173,16 @@ export class TestApp {
});
}

public async invokePostPath(path: string, payload: any): Promise<any> {
const port = await this.port()

return await fetch(`http://localhost:${port}/${path.replace(/^\/+/, '')}`, {
method: 'POST',
body: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' },
});
}

public async invokeGetPathAndRetrieveSpanDump(path: string): Promise<Span[]> {
const port = await this.port()

Expand Down

0 comments on commit 3493dcf

Please sign in to comment.