Skip to content

Commit

Permalink
test: align http responses of mock collector with actual collector
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed May 14, 2024
1 parent 4b57a28 commit 49f4db9
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions test/collector/HttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ export default class HttpServer extends AbstractServer {

handleHttpRequest(req: IncomingMessage, res: ServerResponse) {
const pathname = new URL(req.url ?? '', `http://${req.headers.host}`).pathname;
if (req.method !== 'POST') {
res.statusCode = 405;
res.end();
return;
}

switch (pathname) {
case '/v1/traces':
this.handleTraceRequest(req, res);
Expand All @@ -46,8 +40,7 @@ export default class HttpServer extends AbstractServer {
return;
}

res.statusCode = 404;
res.end();
this.sendPlainTextResponse(res, 404, '404 page not found\n');
}

handleTraceRequest(req: IncomingMessage, res: ServerResponse) {
Expand Down Expand Up @@ -89,25 +82,31 @@ export default class HttpServer extends AbstractServer {
addToSink: (data: any) => void,
responseProperty: string,
) {
if (req.method !== 'POST') {
return this.sendPlainTextResponse(res, 405, '405 method not allowed, supported: [POST]');
}

let contentType = req.headers['content-type'];
if (!contentType) {
return this.sendUnsupportedMediaType(res);
}
// "application/json; charset=utf-8" -> "application/json"
contentType = contentType.split(';')[0].trim().toLowerCase();
if (contentType !== 'application/json' && contentType !== 'application/x-protobuf') {
return this.sendUnsupportedMediaType(res);
}

const chunks: Buffer[] = [];
req
.on('data', chunk => {
chunks.push(chunk);
})
.on('end', () => {
const buffer = Buffer.concat(chunks);
const contentType = req.headers['content-type'];
if (contentType?.toLowerCase().startsWith('application/x-protobuf')) {
if (contentType === 'application/x-protobuf') {
this.handleHttpProtobuf(buffer, res, decodeFunction, addToSink, responseProperty);
} else if (contentType?.toLowerCase().startsWith('application/json')) {
} else if (contentType === 'application/json') {
this.handleHttpJson(res);
} else {
if (!contentType) {
console.warn('request had no content type, assuming http/protobuf');
} else {
console.warn(`request had unexpected content type (${contentType}), assuming http/protobuf`);
}
this.handleHttpProtobuf(buffer, res, decodeFunction, addToSink, responseProperty);
}
});
}
Expand All @@ -133,4 +132,18 @@ export default class HttpServer extends AbstractServer {
res.setHeader('Content-Type', 'text/plain');
res.end('http/json is not supported (yet), only grpc and http/protobuf\n');
}

sendPlainTextResponse(res: ServerResponse, status: number, content: string) {
res.statusCode = status;
res.setHeader('Content-Type', 'text/plain');
res.end(content);
}

sendUnsupportedMediaType(res: ServerResponse) {
this.sendPlainTextResponse(
res,
415,
'415 unsupported media type, supported: [application/json, application/x-protobuf]',
);
}
}

0 comments on commit 49f4db9

Please sign in to comment.