Skip to content

Commit

Permalink
trying to get some tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankcorn committed Oct 31, 2023
1 parent c9217e9 commit 0726513
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/http-plugins/captureBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ import { ClientRequest } from "http";
export function captureBody(request: ClientRequest): Promise<string> {
return new Promise((resolve, reject) => {
const chunks: string[] = [];
const oldWrite = request.write.bind(request);
const oldEnd = request.end.bind(request);
console.log(request)
// const oldWrite = request.write.bind(request);
// const oldEnd = request.end.bind(request);
request.on('data', chunk => {
console.log(chunk.toString());
chunks.push(decodeURIComponent(chunk.toString()))
return oldWrite(chunk);
// return oldWrite(chunk);
});
request.on('end', (chunk) => {
console.log(chunk.toString());
if (chunk) {
console.log(chunk.toString());
chunks.push(decodeURIComponent(chunk.toString()));
}
oldEnd(chunk);
// oldEnd(chunk);
return resolve(chunks.join(''))
});
request.on('error', (err) => {
reject(err);
});
});
}
118 changes: 118 additions & 0 deletions tests/capture-body.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { afterEach, beforeEach, describe, expect, it, test } from "vitest";
import { captureBody } from "../src/http-plugins/captureBody";
import { getLocal, completionCheckers, MockedEndpoint, CompletedRequest } from 'mockttp';
import { URL } from "url";
import { Client } from "undici";
import { BaselimeSDK } from "../src";
import { trace } from "@opentelemetry/api";
import { waitForCollector } from "./utils/otel";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { ClientRequest } from "http";


describe("Test BaselimeSDK for opentelemetry", () => {

const mockServer = getLocal({ debug: true });
beforeEach(() => mockServer.start())
afterEach(() => mockServer.stop())

it.skip("Capture Request bodies", async (t) => {
const mocked = await mockServer.forAnyRequest().once().thenReply(200, "Ok");
console.log('hi')
const postData = JSON.stringify({
'msg': 'Hello World!',
});

const url = new URL(mockServer.url);

const options = {
hostname: url.hostname,
port: mockServer.port,
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
},
};

const clientRequest = new ClientRequest(options);


clientRequest.write(postData);

console.log('waiting for data')
clientRequest.on('data', (d) => {
console.log('data', d)
})
await new Promise((resolve) => setTimeout(resolve, 1000))
clientRequest.end();
clientRequest.on('end', (d) => {
console.log('end', d)
})

// expect(requests).toMatchInlineSnapshot('[]');
// const body = await captureBody(clientRequest);


await new Promise((resolve) => setTimeout(resolve, 1000))

// expect(body).toBe("Hello World");

});

it.skip('Instrument Req Body', async () => {
const collector = await mockServer.forAnyRequest().twice().thenReply(200, "Ok");
const baselimeKey = "love is magic"
const sdk = new BaselimeSDK({
collectorUrl: mockServer.url,
serverless: true,
baselimeKey: baselimeKey,
instrumentations: [
new HttpInstrumentation({
requestHook: async (span, request) => {
console.log(request)
if (request instanceof ClientRequest) {
const body = await captureBody(request);
span.setAttribute('body', body)
return
}
},
})
]
})

sdk.start();

const span = await trace.getTracer("test").startActiveSpan("test", async (span) => {
const postData = JSON.stringify({
'msg': 'Hello World!',
});

const url = new URL(mockServer.url);

const options = {
hostname: url.hostname,
port: mockServer.port,
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
},
};
const { request } = await import('http');
const req = request(options, () => span.end());

req.write(postData)
req.end();
});



const [data, spans] = await waitForCollector(collector)
console.log(JSON.stringify(await data.body.getJson(), null, 2))
console.log(JSON.stringify(await spans.body.getJson(), null, 2))
expect(spans.headers["x-api-key"]).toBe(baselimeKey);
})
});
1 change: 0 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ describe("Test BaselimeSDK for opentelemetry", async () => {
const [request] = await waitForCollector(collector)

expect(request.headers["x-api-key"]).toBe(baselimeKey);

})
});

0 comments on commit 0726513

Please sign in to comment.