Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: align http responses of mock collector with actual collector #3

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]',
);
}
}