Skip to content

Commit

Permalink
Log errors encountered when reporting responses (#33)
Browse files Browse the repository at this point in the history
* Log errors encountered when reporting responses

* fixup! Log errors encountered when reporting responses
  • Loading branch information
jugglinmike authored Dec 6, 2023
1 parent 298d7d4 commit 35dea7e
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/host/cli-run-plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import path from 'path';
import { Readable } from 'stream';
import fetch from 'node-fetch';
import fetch, { Response } from 'node-fetch';

import yargs from 'yargs';
import { pickAgentCliOptions } from '../agent/cli.js';
Expand Down Expand Up @@ -202,10 +202,20 @@ function mainFetchMiddleware(argv) {
if (!argv.agentMock) {
argv.fetch = fetch;
} else {
argv.fetch = (...params) =>
argv.fetch = (url, ...params) =>
new Promise(resolve => {
console.log('Callback Fetch Mocked: ', ...params);
resolve();
const { searchParams } = new URL(url);
const status = parseInt(searchParams.get('TEST-STATUS'), 10) || 200;
const response = new Response('a body', { status });

if (searchParams.has('TEST-BAD-BODY')) {
// Disturb the response body stream in order to trigger failure in
// any future attempt to read.
response.text();
}

console.log('Callback Fetch Mocked: ', url, ...params);
resolve(response);
});
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/host/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ import {
addTestResultToTestPlan,
} from './plan-object.js';

/**
* @param {AriaATCIHost.Log} log
* @param {Response} response
*/
const logUnsuccessfulHTTP = async (log, response) => {
if (!response.ok) {
const { status } = response;
const body = await response.text().catch(() => 'Unknown error - unable to read response body.');

log(HostMessage.REPORTING_ERROR, { status, body });
}
};

/**
* @param {object} options
* @param {AriaATCIHost.Log} options.log
Expand Down Expand Up @@ -84,7 +97,7 @@ export async function hostMain({
method: 'post',
body,
headers,
})
}).then(logUnsuccessfulHTTP.bind(null, log))
);
}
plan = addTestResultToTestPlan(plan, test.filepath, result);
Expand Down
4 changes: 4 additions & 0 deletions src/host/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const HostMessage = {
AGENT_CRASHED: 'agentCrashed',
/** @type {'startTest'} */
START_TEST: 'startTest',
/** @type {'reportingError'} */
REPORTING_ERROR: 'reportingError',
};

export const HOST_TEMPLATES = {
Expand All @@ -60,6 +62,8 @@ export const HOST_TEMPLATES = {
[HostMessage.AGENT_LOG]: ({ text }) => `[Agent]: ${text}`,
[HostMessage.AGENT_CRASHED]: () => `Agent crashed.`,
[HostMessage.START_TEST]: () => `Starting test.`,
[HostMessage.REPORTING_ERROR]: ({ status, body }) =>
`HTTP ${status} response received when reporting result: '${body}'.`,
};

/**
Expand Down
26 changes: 26 additions & 0 deletions src/host/tests/cli-run-plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ test('plan3 with callback', async t => {
);
});

test('plan3 with callback request which fails', async t => {
t.snapshot(
await spawnRunPlan([
'--plan-workingdir=fixtures/host-bin/plan3',
'"**"',
'--agent-mock',
'--debug',
'--callback-url=http://example.com/?TEST-STATUS=418',
'--callback-header=x:y',
])
);
});

test('plan3 with callback request which fails with a faulty response body', async t => {
t.snapshot(
await spawnRunPlan([
'--plan-workingdir=fixtures/host-bin/plan3',
'"**"',
'--agent-mock',
'--debug',
'--callback-url="http://example.com/?TEST-STATUS=418&TEST-BAD-BODY"',
'--callback-header=x:y',
])
);
});

async function spawnRunPlan(args) {
const dirname = path.dirname(fileURLToPath(import.meta.url));
const hostBin = path.join(dirname, '../../../bin/host.js');
Expand Down
80 changes: 80 additions & 0 deletions src/host/tests/snapshots/cli-run-plan.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,83 @@ Generated by [AVA](https://avajs.dev).
{"name":"unknown","tests":[{"filepath":"tests/test-1.json","log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}],"results":[{"testId":1,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"commands":[{"command":"UP_ARROW","expectation":"role up","output":"mocked output for role up","pass":true}]}]},{"filepath":"tests/test-2.json","log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}],"results":[{"testId":2,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"commands":[{"command":"UP_ARROW,DOWN_ARROW","expectation":"role down","output":"mocked output for role down","pass":true}]}]}],"log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."},{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}]}␊
`,
}

## plan3 with callback request which fails

> Snapshot 1
{
stderr: `Starting...␊
Reference server listening on 'http://localhost:8888'.␊
Plan 'unknown' with 2 tests and 4 files read from source 'fork'.␊
Reference available on 'http://localhost:8888/static␊
Starting test agent.␊
[Agent]: Starting...␊
Agent running with protocol 'fork'.␊
Starting test.␊
[Server]: Serving '/static/reference/index.html'.␊
[Agent]: Open page: 'http://localhost:8888/static/reference/index.html'.␊
HTTP 418 response received when reporting result: 'a body'.␊
Starting test.␊
[Server]: Serving '/static/reference/index.html'.␊
[Agent]: Open page: 'http://localhost:8888/static/reference/index.html'.␊
HTTP 418 response received when reporting result: 'a body'.␊
Removing reference from 'http://localhost:8888/static␊
Stopping test agent.␊
[Agent]: Stopping...␊
Stopping reference server.␊
Stopping...␊
`,
stdout: `Callback Fetch Mocked: http://example.com/?TEST-STATUS=418 {␊
method: 'post',␊
body: '{"testCsvRow":1,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"responses":["mocked output for role up"],"atVersionName":"1.0","browserVersionName":"1.0"}',␊
headers: { 'Content-Type': 'application/json', x: 'y' }␊
}␊
Callback Fetch Mocked: http://example.com/?TEST-STATUS=418 {␊
method: 'post',␊
body: '{"testCsvRow":2,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"responses":["mocked output for role down"],"atVersionName":"1.0","browserVersionName":"1.0"}',␊
headers: { 'Content-Type': 'application/json', x: 'y' }␊
}␊
{"name":"unknown","tests":[{"filepath":"tests/test-1.json","log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}],"results":[{"testId":1,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"commands":[{"command":"UP_ARROW","expectation":"role up","output":"mocked output for role up","pass":true}]}]},{"filepath":"tests/test-2.json","log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}],"results":[{"testId":2,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"commands":[{"command":"UP_ARROW,DOWN_ARROW","expectation":"role down","output":"mocked output for role down","pass":true}]}]}],"log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."},{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}]}␊
`,
}

## plan3 with callback request which fails with a faulty response body

> Snapshot 1
{
stderr: `Starting...␊
Reference server listening on 'http://localhost:8888'.␊
Plan 'unknown' with 2 tests and 4 files read from source 'fork'.␊
Reference available on 'http://localhost:8888/static␊
Starting test agent.␊
[Agent]: Starting...␊
Agent running with protocol 'fork'.␊
Starting test.␊
[Server]: Serving '/static/reference/index.html'.␊
[Agent]: Open page: 'http://localhost:8888/static/reference/index.html'.␊
HTTP 418 response received when reporting result: 'Unknown error - unable to read response body.'.␊
Starting test.␊
[Server]: Serving '/static/reference/index.html'.␊
[Agent]: Open page: 'http://localhost:8888/static/reference/index.html'.␊
HTTP 418 response received when reporting result: 'Unknown error - unable to read response body.'.␊
Removing reference from 'http://localhost:8888/static␊
Stopping test agent.␊
[Agent]: Stopping...␊
Stopping reference server.␊
Stopping...␊
`,
stdout: `Callback Fetch Mocked: http://example.com/?TEST-STATUS=418&TEST-BAD-BODY {␊
method: 'post',␊
body: '{"testCsvRow":1,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"responses":["mocked output for role up"],"atVersionName":"1.0","browserVersionName":"1.0"}',␊
headers: { 'Content-Type': 'application/json', x: 'y' }␊
}␊
Callback Fetch Mocked: http://example.com/?TEST-STATUS=418&TEST-BAD-BODY {␊
method: 'post',␊
body: '{"testCsvRow":2,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"responses":["mocked output for role down"],"atVersionName":"1.0","browserVersionName":"1.0"}',␊
headers: { 'Content-Type': 'application/json', x: 'y' }␊
}␊
{"name":"unknown","tests":[{"filepath":"tests/test-1.json","log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}],"results":[{"testId":1,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"commands":[{"command":"UP_ARROW","expectation":"role up","output":"mocked output for role up","pass":true}]}]},{"filepath":"tests/test-2.json","log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}],"results":[{"testId":2,"capabilities":{"browserName":"mock","browserVersion":"1.0","atName":"mock","atVersion":"1.0","platformName":"mock"},"commands":[{"command":"UP_ARROW,DOWN_ARROW","expectation":"role down","output":"mocked output for role down","pass":true}]}]}],"log":[{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."},{"data":{"type":"openPage","date":"2000-01-01T12:00:00.000Z","url":{}},"text":"Open page: 'http://localhost:8888/static/reference/index.html'."}]}␊
`,
}
Binary file modified src/host/tests/snapshots/cli-run-plan.js.snap
Binary file not shown.

0 comments on commit 35dea7e

Please sign in to comment.