Skip to content

Commit

Permalink
feat: options method unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Dec 17, 2024
1 parent de90bec commit aed0fa6
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/command/http-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class HttpCommand implements CommandInterface<HttpOptions> {
rawOutput = result.error;
} else if (result.error) {
rawOutput = `HTTP/${result.httpVersion} ${result.statusCode}\n${result.rawHeaders}\n\n${result.error}`;
} else if (cmdOptions.request.method === 'HEAD') {
} else if (cmdOptions.request.method === 'HEAD' || !result.rawBody) {
rawOutput = `HTTP/${result.httpVersion} ${result.statusCode}\n${result.rawHeaders}`;
} else {
rawOutput = `HTTP/${result.httpVersion} ${result.statusCode}\n${result.rawHeaders}\n\n${result.rawBody}`;
Expand Down
109 changes: 109 additions & 0 deletions test/unit/command/http-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,115 @@ describe('http command', () => {
]);
});

it('should respond with 200 on OPTIONS request and response with body', async () => {
nock('http://google.com').options('/').reply(200, function () {
const request = this.req as typeof this.req & {response: Response & {socket: { getPeerCertificate }}};
request.response.httpVersion = '1.1';
request.response.socket.getPeerCertificate = false;
return 'response body';
});

const options = {
type: 'http' as const,
target: 'google.com',
inProgressUpdates: false,
protocol: 'HTTP',
request: {
method: 'OPTIONS',
path: '/',
query: '',
},
ipVersion: 4,
};

const http = new HttpCommand(httpCmd);
await http.run(mockedSocket as any, 'measurement', 'test', options);

expect(mockedSocket.emit.callCount).to.equal(1);

expect(mockedSocket.emit.firstCall.args).to.deep.equal([
'probe:measurement:result',
{
testId: 'test',
measurementId: 'measurement',
result: {
status: 'finished',
resolvedAddress: '127.0.0.1',
headers: {},
rawHeaders: null,
rawBody: 'response body',
rawOutput: 'HTTP/1.1 200\n\n\nresponse body',
truncated: false,
statusCode: 200,
statusCodeName: 'OK',
timings: {
total: 0,
download: 0,
firstByte: 0,
dns: 0,
tls: null,
tcp: 0,
},
tls: null,
},
},
]);
});

it('should respond with 200 on OPTIONS request and response without body', async () => {
nock('http://google.com').options('/').reply(200, function () {
const request = this.req as typeof this.req & {response: Response & {socket: { getPeerCertificate }}};
request.response.httpVersion = '1.1';
request.response.socket.getPeerCertificate = false;
});

const options = {
type: 'http' as const,
target: 'google.com',
inProgressUpdates: false,
protocol: 'HTTP',
request: {
method: 'OPTIONS',
path: '/',
query: '',
},
ipVersion: 4,
};

const http = new HttpCommand(httpCmd);
await http.run(mockedSocket as any, 'measurement', 'test', options);

expect(mockedSocket.emit.callCount).to.equal(1);

expect(mockedSocket.emit.firstCall.args).to.deep.equal([
'probe:measurement:result',
{
testId: 'test',
measurementId: 'measurement',
result: {
status: 'finished',
resolvedAddress: '127.0.0.1',
headers: {},
rawHeaders: null,
rawBody: null,
rawOutput: 'HTTP/1.1 200\n',
truncated: false,
statusCode: 200,
statusCodeName: 'OK',
timings: {
total: 0,
download: 0,
firstByte: 0,
dns: 0,
tls: null,
tcp: 0,
},
tls: null,
},
},
]);
});

it('should respond with 400 with progress messages', async () => {
nock('http://google.com').get('/400').times(3).reply(400, function () {
const request = this.req as typeof this.req & {response: Response & {socket: { getPeerCertificate }}};
Expand Down
3 changes: 1 addition & 2 deletions wallaby.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as url from 'node:url';

export default function wallaby () {
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

return {
testFramework: 'mocha',
files: [
Expand All @@ -12,7 +13,6 @@ export default function wallaby () {
'test/plugins/**/*',
'test/utils.ts',
'test/hooks.ts',
'test/setup.ts',
'test/snapshots/**/*.json',
'package.json',
],
Expand All @@ -22,7 +22,6 @@ export default function wallaby () {
setup (w) {
const path = require('path');
w.testFramework.files.unshift(path.resolve(process.cwd(), 'test/hooks.js'));
w.testFramework.files.unshift(path.resolve(process.cwd(), 'test/setup.js'));
const mocha = w.testFramework;
mocha.timeout(5000);
},
Expand Down

0 comments on commit aed0fa6

Please sign in to comment.