From 4cb7b354f5d82874237b20940074058deb030da7 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 10 Nov 2026 11:21:57 +0100 Subject: [PATCH 1/2] Run defaults for journeys: viewport, user agent and headers JourneyOptions.Viewport, UserAgent and Headers (viewport, userAgent, headers in @nullean/journeys; --viewport, --user-agent and --header on cdp run) are in place before the first step, so a harness running against a live site does not repeat them in every journey. A journey's set headers adds to the run's, set device keeps the run's user agent, and set viewport and set useragent replace theirs. Co-Authored-By: Claude Sonnet 5.5 --- DECISIONS.md | 14 +++ docs/cli/index.md | 3 + docs/journeys/index.md | 14 +++ docs/journeys/playwright.md | 1 + js/journeys/src/run.ts | 43 ++++++++-- js/journeys/test/defaults.test.ts | 85 +++++++++++++++++++ src/Nullean.Cdp.Cli/CdpCli.cs | 47 +++++++++- src/Nullean.Cdp.Cli/Program.cs | 8 +- src/Nullean.Cdp.Journeys/JourneyRunner.cs | 50 ++++++++++- tests/Nullean.Cdp.Tests/Live/EchoServer.cs | 71 ++++++++++++++++ .../Live/RunDefaultsTests.cs | 71 ++++++++++++++++ 11 files changed, 394 insertions(+), 13 deletions(-) create mode 100644 js/journeys/test/defaults.test.ts create mode 100644 tests/Nullean.Cdp.Tests/Live/EchoServer.cs create mode 100644 tests/Nullean.Cdp.Tests/Live/RunDefaultsTests.cs diff --git a/DECISIONS.md b/DECISIONS.md index 2daa46b..c198fe4 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -560,3 +560,17 @@ published by hand by the owner. write to an allowed host fails the step. A page that writes on its own (telemetry) needs `WritableUrls`. - **`BlockedRequest.RefusedWrite` is `null` for allow-list refusals**, so existing `BlockedRequest` equality is unchanged. +## Run defaults + +- **A harness sets the viewport, user agent and headers once per run.** A live site tells monitors from people by user + agent and headers (the docs site turns its telemetry off for a monitor), and a responsive site is in its phone layout + at Chrome's default viewport. Repeating `set viewport`, `set useragent` and `set headers` in every journey is how one + gets forgotten. +- **The journey builds on the run, it does not undo it.** `set headers` merges (a header it repeats wins) because + Chrome's own call replaces the whole set and a journey adding one header should not drop the monitor header; + `set device` leaves the run's user agent in place for the same reason. `set viewport`, `set device` and `set useragent` + still replace what they name. +- **No allow-list option was added**: `AllowedHosts` already is the run-level default. +- **A click that hits nothing is not this change**: at the phone viewport `find role link click` completed with no effect. + The default viewport is the fix for that site; a click that reports it missed its target is separate. + diff --git a/docs/cli/index.md b/docs/cli/index.md index 6e18866..70331d6 100644 --- a/docs/cli/index.md +++ b/docs/cli/index.md @@ -35,6 +35,9 @@ cdp run site/journeys/*.journey --base-url ./site --artifacts .artifacts/journey | `--build-type`, `--env` | the build type and the environment's name, for `build-type:` and `env:` conditions | | `--read-only` | refuse every request that is not GET, HEAD or OPTIONS, so the run cannot change a live site; see [safety](../journeys/safety.md#read-only-runs) | | `--writable` | a URL glob a read-only run may write to anyway (repeatable), such as a telemetry endpoint | +| `--viewport` | the viewport the run starts with, `1280x800`; without it Chrome's default applies, which a responsive site may treat as a phone | +| `--user-agent` | the user agent of the run | +| `--header` | a request header every page sends (repeatable), `"Name: value"` | | `--file-root` | the folder `file://` URLs may point into, when the base URL is not a folder | | `--artifacts` | the folder `screenshot` and `network har stop` write into; off unless given | | `--timeout` | seconds an action waits for its element (default 5) | diff --git a/docs/journeys/index.md b/docs/journeys/index.md index 406d159..45b8b79 100644 --- a/docs/journeys/index.md +++ b/docs/journeys/index.md @@ -72,6 +72,20 @@ end as two journey files with different `@requires`, one per flag state. - Indentation is for people; it means nothing to the parser. - agent-browser has no conditions, so a journey with blocks cannot be exported to it (`ToAgentBrowserBatch` says so). +## Run defaults + +A harness that runs journeys against a live site sets three things once for the run, so no journey repeats them: + +| Option | Does | +|---|---| +| `JourneyOptions.Viewport` (`viewport`, `--viewport 1280x800`) | the size the run starts with. Without it Chrome's default applies, and a responsive site may be in its mobile layout, where a link in a collapsed menu can be clicked with no effect and no error | +| `JourneyOptions.UserAgent` (`userAgent`, `--user-agent`) | the user agent of every request and of `navigator.userAgent`, e.g. one that tells the site the traffic is a monitor | +| `JourneyOptions.Headers` (`headers`, `--header "Name: value"`) | headers every page sends | + +They are in place before the first step. A journey's own lines build on them: `set viewport` and `set device` replace the +viewport, `set useragent` replaces the user agent (`set device` does not: the run's identity stays), and `set headers` +adds to the run's headers, winning for a name it repeats, instead of replacing them. The hosts a run may reach are +`JourneyOptions.AllowedHosts` (`--allow`), as before. ## URLs diff --git a/docs/journeys/playwright.md b/docs/journeys/playwright.md index 00a1446..b9b0d28 100644 --- a/docs/journeys/playwright.md +++ b/docs/journeys/playwright.md @@ -104,6 +104,7 @@ adapters do. |---|---| | `baseUrl` | what relative `open` URLs resolve against; its host is allowed | | `allowedHosts` | host patterns allowed beyond the base URL's host and the journey's `allow` lines | +| `viewport`, `userAgent`, `headers` | the run's defaults, in place before the first step (see [run defaults](index.md#run-defaults)); `userAgent` needs Chromium | | `secrets` | resolves `$env:NAME`; the process environment by default | | `artifactsDir` | where `screenshot` writes; without it screenshot steps are skipped | | `journeyDir` | the folder `upload` reads from | diff --git a/js/journeys/src/run.ts b/js/journeys/src/run.ts index 135ad3e..b258b2f 100644 --- a/js/journeys/src/run.ts +++ b/js/journeys/src/run.ts @@ -30,6 +30,15 @@ export interface RunOptions { readOnly?: boolean; /** URL globs (`**` anything, `*` anything but `/`) a read-only run may write to anyway, such as a telemetry endpoint. */ writableUrls?: string[]; + /** + * Request headers every page of the run sends, such as one that marks the traffic as a monitor. A journey's own + * `set headers` adds to them (and wins for a header it names) rather than replacing them. + */ + headers?: Record; + /** The user agent of the run. A journey's own `set useragent` replaces it; `set device` does not. Needs Chromium. */ + userAgent?: string; + /** The viewport the run starts with; without it the context's applies. A journey's `set viewport` or `set device` replaces it. */ + viewport?: { width: number; height: number }; /** Resolves `$env:NAME`; the process environment by default. */ secrets?: (name: string) => string | undefined; /** Where `screenshot` writes. Without it, screenshot steps are skipped with a warning. */ @@ -236,6 +245,10 @@ export class JourneyRun { }; await page.route('**/*', guard); this.disposers.push(() => page.unroute('**/*', guard)); + // The run's viewport, user agent and headers, set before the first step so the journey starts as the harness wants. + if (this.options.viewport) await page.setViewportSize(this.options.viewport); + if (this.options.userAgent !== undefined) await this.setUserAgent(this.options.userAgent); + if (this.options.headers && Object.keys(this.options.headers).length > 0) await page.context().setExtraHTTPHeaders(this.options.headers); const commands = journey.steps.map((s) => s.command); if (commands.some((c) => c.type === 'har' || c.type === 'requests' || c.type === 'expectRequest')) { @@ -541,6 +554,8 @@ export class JourneyRun { if (!device) throw new StepError(`Playwright has no device called '${c.device}'.`); await this.page.setViewportSize(device.viewport); this.warn('set device changes only the viewport: user agent and touch are fixed when the context is created (use a Playwright project).'); + // The run's identity stays: only the journey's own `set useragent` replaces it. + if (this.options.userAgent !== undefined) await this.setUserAgent(this.options.userAgent); break; } case 'geo': @@ -551,16 +566,9 @@ export class JourneyRun { this.offline = c.offline ?? !this.offline; await context.setOffline(this.offline); break; - case 'headers': await context.setExtraHTTPHeaders(JSON.parse(c.headers!) as Record); break; + case 'headers': await context.setExtraHTTPHeaders(mergeHeaders(this.options.headers, JSON.parse(c.headers!) as Record)); break; case 'useragent': - // Playwright fixes the user agent when the context is created; Chromium's own override changes it for the page, - // header and navigator.userAgent both. - try { - const session = await context.newCDPSession(this.page); - await session.send('Emulation.setUserAgentOverride', { userAgent: c.userAgent! }); - } catch { - throw new StepError('set useragent needs Chromium: set userAgent in the Playwright config for other browsers.'); - } + await this.setUserAgent(c.userAgent!); break; case 'credentials': throw new StepError('set credentials is not supported by the Playwright runner: set httpCredentials in the Playwright config (an extra header would be sent to every host).'); @@ -569,6 +577,17 @@ export class JourneyRun { return undefined; } + // Playwright fixes the user agent when the context is created; Chromium's own override changes it for the page, + // header and navigator.userAgent both. + private async setUserAgent(userAgent: string) { + try { + const session = await this.page.context().newCDPSession(this.page); + await session.send('Emulation.setUserAgentOverride', { userAgent }); + } catch { + throw new StepError('set useragent needs Chromium: set userAgent in the Playwright config for other browsers.'); + } + } + private async audit(c: Extract): Promise { const Axe = this.options.axe; if (!Axe) throw new StepError("a11y needs axe: pass `axe: (await import('@axe-core/playwright')).AxeBuilder` in the run options."); @@ -780,6 +799,12 @@ function describeRequestExpectation(c: Extract | undefined, journey: Record): Record { + const named = new Set(Object.keys(journey).map((n) => n.toLowerCase())); + return { ...Object.fromEntries(Object.entries(defaults ?? {}).filter(([n]) => !named.has(n.toLowerCase()))), ...journey }; +} + const globToRegExp = (glob: string) => new RegExp('^' + glob.split('**').map((part) => part.split('*').map((s) => s.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('[^/]*')).join('.*') + '$'); diff --git a/js/journeys/test/defaults.test.ts b/js/journeys/test/defaults.test.ts new file mode 100644 index 0000000..75d897e --- /dev/null +++ b/js/journeys/test/defaults.test.ts @@ -0,0 +1,85 @@ +// A run's defaults (viewport, user agent, headers) are in place before the first step, a journey's own `set` lines +// build on them, and they show on both sides: what the page sees and what the server receives. The same cases as +// RunDefaultsTests in the .NET tests. Skips when Chrome is missing; CDP_BROWSER_REQUIRED=1 makes that a failure. + +import assert from 'node:assert/strict'; +import { createServer } from 'node:http'; +import type { AddressInfo } from 'node:net'; +import { after, before, test } from 'node:test'; +import type { Browser } from 'playwright-core'; +import { parseJourney, runJourney } from '../src/index.ts'; +import type { RunOptions } from '../src/index.ts'; +import { launchChrome } from './site.ts'; + +let browser: Browser | undefined; +let skip: string | false = false; +let origin = ''; +let stop = () => {}; + +const header = (v: string | string[] | undefined) => String(v ?? 'none').replace(/&/g, '&').replace(/ { + const server = createServer((req, res) => { + res.writeHead(200, { 'content-type': 'text/html' }).end( + 'Echo' + + `

${header(req.headers['x-journey'])}

${header(req.headers['user-agent'])}

` + + '

' + + "", + ); + }); + await new Promise((done) => server.listen(0, '127.0.0.1', done)); + origin = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; + stop = () => server.close(); + ({ browser, skip } = await launchChrome()); +}); + +after(async () => { + await browser?.close(); + stop(); +}); + +const reads = 'open /echo\nget text #ua\nget text #width\nget text #header\nget text #journey\nget text #request-ua\n'; +const monitor: Partial = { headers: { 'X-Docs-Synthetic-Monitor': 'true' }, userAgent: 'Elastic/Synthetics', viewport: { width: 1111, height: 700 } }; + +// What the page saw (user agent, width) and what the server saw (header, journey header, user agent). +async function run(text: string, defaults: Partial) { + const page = await (await browser!.newContext()).newPage(); + const result = await runJourney(page, parseJourney(text, 'defaults.journey'), { baseUrl: origin + '/', actionTimeout: 3000, warn: () => {}, ...defaults }); + assert.equal(result.failure, undefined, JSON.stringify(result.failure)); + return result.checkpoints.filter((c) => c.command.startsWith('get text')).map((c) => c.output); +} + +test('the run starts with its viewport, user agent and headers', async (t) => { + if (skip) return t.skip(skip); + assert.deepEqual(await run(reads, monitor), ['Elastic/Synthetics', '1111', 'true', 'none', 'Elastic/Synthetics']); +}); + +test('without defaults nothing changes', async (t) => { + if (skip) return t.skip(skip); + const seen = await run(reads, {}); + assert.match(seen[0], /Chrome/); + assert.doesNotMatch(seen[0], /Elastic\/Synthetics/); + assert.equal(seen[2], 'none'); + assert.notEqual(seen[1], '1111'); +}); + +test("a journey's headers add to the run's and win when they name the same", async (t) => { + if (skip) return t.skip(skip); + assert.deepEqual(await run('set headers \'{"x-journey": "1", "X-Docs-Synthetic-Monitor": "override"}\'\n' + reads, monitor), ['Elastic/Synthetics', '1111', 'override', '1', 'Elastic/Synthetics']); + assert.deepEqual((await run('set headers \'{"x-journey": "1"}\'\n' + reads, monitor)).slice(2), ['true', '1', 'Elastic/Synthetics']); +}); + +test("a journey's user agent replaces the run's and the header stays", async (t) => { + if (skip) return t.skip(skip); + assert.deepEqual(await run('set useragent Other/1.0\n' + reads, monitor), ['Other/1.0', '1111', 'true', 'none', 'Other/1.0']); +}); + +test("a journey's viewport replaces the run's", async (t) => { + if (skip) return t.skip(skip); + assert.equal((await run('set viewport 800 600\n' + reads, monitor))[1], '800'); +}); + +test("a device keeps the run's user agent", async (t) => { + if (skip) return t.skip(skip); + assert.equal((await run("set device 'iPhone 14'\n" + reads, monitor))[0], 'Elastic/Synthetics'); +}); diff --git a/src/Nullean.Cdp.Cli/CdpCli.cs b/src/Nullean.Cdp.Cli/CdpCli.cs index a0b798e..797f8b9 100644 --- a/src/Nullean.Cdp.Cli/CdpCli.cs +++ b/src/Nullean.Cdp.Cli/CdpCli.cs @@ -82,6 +82,43 @@ public int Batch(string file, string? baseUrl, bool resolveSecrets) return ExitCode.Ok; } + // "Name: value", as curl takes it. Null, after saying why, when one is not. + private Dictionary? ParseHeaders(IReadOnlyList headers) + { + var parsed = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var header in headers) + { + var colon = header.IndexOf(':', StringComparison.Ordinal); + var name = colon > 0 ? header[..colon].Trim() : ""; + if (name.Length == 0 || name.Any(c => c is <= ' ' or > '~' or ':')) + { + error.WriteLine($"'{header}' is not a header: use --header \"Name: value\"."); + return null; + } + + parsed[name] = header[(colon + 1)..].Trim(); + } + + return parsed; + } + + // "1280x800": the size the run starts with; none when not given. False, after saying why, when it is malformed. + private bool ParseViewport(string? viewport, out JourneyViewport? size) + { + size = null; + if (viewport is null) + return true; + var parts = viewport.Split('x'); + if (parts.Length == 2 && int.TryParse(parts[0], NumberStyles.None, CultureInfo.InvariantCulture, out var width) && int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var height) && width > 0 && height > 0) + { + size = new JourneyViewport(width, height); + return true; + } + + error.WriteLine($"'{viewport}' is not a viewport: use --viewport x, e.g. 1280x800."); + return false; + } + public async Task RunAsync( IReadOnlyList files, string? baseUrl, @@ -97,11 +134,14 @@ public async Task RunAsync( string? buildType = null, string? env = null, bool readOnly = false, - IReadOnlyList? writable = null + IReadOnlyList? writable = null, + IReadOnlyList? headers = null, + string? userAgent = null, + string? viewport = null ) { var (ok, root) = ParseBase(baseUrl); - if (!ok || !ValidAllow(allow) || Load(files) is not { } journeys) + if (!ok || !ValidAllow(allow) || ParseHeaders(headers ?? []) is not { } extraHeaders || !ParseViewport(viewport, out var size) || Load(files) is not { } journeys) return ExitCode.Usage; if (await LaunchAsync(headed, cancellationToken).ConfigureAwait(false) is not { } browser) return ExitCode.Browser; @@ -118,6 +158,9 @@ public async Task RunAsync( Env = env, ReadOnly = readOnly, WritableUrls = writable ?? [], + Headers = extraHeaders, + UserAgent = userAgent, + Viewport = size, FileRoot = fileRoot is null ? null : Path.GetFullPath(fileRoot), ArtifactsDirectory = artifacts is null ? null : Path.GetFullPath(artifacts), Secrets = environment, diff --git a/src/Nullean.Cdp.Cli/Program.cs b/src/Nullean.Cdp.Cli/Program.cs index 124cec4..54a02fd 100644 --- a/src/Nullean.Cdp.Cli/Program.cs +++ b/src/Nullean.Cdp.Cli/Program.cs @@ -27,6 +27,9 @@ internal sealed class CdpCommands /// The environment's name, for env: conditions. /// Refuse every request that is not GET, HEAD or OPTIONS, so the run cannot change a live site. /// URL glob a read-only run may write to anyway (repeatable): ** anything, * anything but /. + /// A request header every page sends (repeatable): "Name: value". + /// The user agent of the run. + /// The viewport the run starts with: 1280x800. Without it Chrome's default applies, which a responsive site may treat as a phone. /// Show the browser window. /// Print JSON; everything under a "page" key came from the page. /// Characters per checkpoint before it is cut (0 for no limit). @@ -43,12 +46,15 @@ public static Task Run( string? env = null, bool readOnly = false, string[]? writable = null, + string[]? header = null, + string? userAgent = null, + string? viewport = null, bool headed = false, bool json = false, int maxOutput = CdpCli.DefaultMaxOutput, double? timeout = null, CancellationToken cancellationToken = default - ) => Cli().RunAsync(files, baseUrl, allow ?? [], fileRoot, headed, json, maxOutput, timeout, cancellationToken, artifacts, feature, buildType, env, readOnly, writable); + ) => Cli().RunAsync(files, baseUrl, allow ?? [], fileRoot, headed, json, maxOutput, timeout, cancellationToken, artifacts, feature, buildType, env, readOnly, writable, header, userAgent, viewport); /// Parse journeys without running them, and report every error with its line. /// Journey files. diff --git a/src/Nullean.Cdp.Journeys/JourneyRunner.cs b/src/Nullean.Cdp.Journeys/JourneyRunner.cs index a54027e..45e3e9c 100644 --- a/src/Nullean.Cdp.Journeys/JourneyRunner.cs +++ b/src/Nullean.Cdp.Journeys/JourneyRunner.cs @@ -4,6 +4,12 @@ namespace Nullean.Cdp.Journeys; +/// A viewport size, and its device scale factor. +/// CSS pixels. +/// CSS pixels. +/// The device scale factor. +public sealed record JourneyViewport(int Width, int Height, double Scale = 1); + /// How a journey runs. public sealed class JourneyOptions { @@ -37,6 +43,24 @@ public sealed class JourneyOptions /// Resolves $env:NAME references; defaults to the process environment. public Func Secrets { get; init; } = Environment.GetEnvironmentVariable; + /// + /// Request headers every page of the run sends, such as one that marks the traffic as a monitor. A journey's own + /// set headers adds to them (and wins for a header it names) rather than replacing them. + /// + public IReadOnlyDictionary Headers { get; init; } = new Dictionary(); + + /// + /// The user agent of the run, for example one that tells the site it is a monitor. A journey's own set useragent + /// replaces it; set device does not. + /// + public string? UserAgent { get; init; } + + /// + /// The viewport the run starts with. Without one the browser's default applies, which a responsive site may treat as + /// a phone. A journey's own set viewport or set device replaces it. + /// + public JourneyViewport? Viewport { get; init; } + /// /// Refuse requests outside the allow list (on by default). Turning it off also lets open go anywhere. /// @@ -147,6 +171,7 @@ public static async Task RunAsync(Journey journey, Page page, Jou ? await page.GuardNetworkAsync(options.GuardNetwork ? run.AllowList.Allows : _ => true, options.GuardOptions(), cancellationToken).ConfigureAwait(false) : null; run.Guard = guard; + await ApplyDefaultsAsync(page, options, cancellationToken).ConfigureAwait(false); // The network is recorded only for journeys that read it. await using var network = journey.Steps.Any(s => s.Command is HarCommand or RequestsCommand or ExpectRequestCommand) ? await page.RecordNetworkAsync(cancellationToken).ConfigureAwait(false) @@ -200,6 +225,17 @@ public static async Task RunAsync(Journey journey, Page page, Jou return new JourneyResult(journey.Source, checkpoints, null, blocked); } + // The run's viewport, user agent and headers, set before the first step so the journey starts as the harness wants. + private static async Task ApplyDefaultsAsync(Page page, JourneyOptions options, CancellationToken cancellationToken) + { + if (options.Viewport is { } viewport) + await page.SetViewportAsync(viewport.Width, viewport.Height, viewport.Scale, cancellationToken).ConfigureAwait(false); + if (options.UserAgent is { } userAgent) + await page.SetUserAgentAsync(userAgent, cancellationToken).ConfigureAwait(false); + if (options.Headers.Count > 0) + await page.SetExtraHeadersAsync(options.Headers, cancellationToken).ConfigureAwait(false); + } + private sealed record Run(Journey Journey, Page Page, JourneyOptions Options, JourneyAllowList AllowList, JourneySecrets Secrets) { public NetworkGuard? Guard { get; set; } @@ -319,6 +355,9 @@ or TimeoutException or CdpException or CdpDisconnectedException or ArgumentExcep return null; case SetCommand { Setting: "device" } device: await page.SetDeviceAsync(device.Device!, ct).ConfigureAwait(false); + // A device brings its own user agent; the run's identity stays unless the journey sets another. + if (run.Options.UserAgent is { } identity) + await page.SetUserAgentAsync(identity, ct).ConfigureAwait(false); return null; case SetCommand { Setting: "geo" } geo: await page.SetGeolocationAsync(geo.Position.Latitude, geo.Position.Longitude, ct).ConfigureAwait(false); @@ -328,7 +367,7 @@ or TimeoutException or CdpException or CdpDisconnectedException or ArgumentExcep await page.SetOfflineAsync(run.Offline, ct).ConfigureAwait(false); return null; case SetCommand { Setting: "headers" } headers: - await page.SetExtraHeadersAsync(Headers(headers.Headers!), ct).ConfigureAwait(false); + await page.SetExtraHeadersAsync(MergeHeaders(run.Options.Headers, Headers(headers.Headers!)), ct).ConfigureAwait(false); return null; case SetCommand { Setting: "useragent" } userAgent: await page.SetUserAgentAsync(userAgent.UserAgent!, ct).ConfigureAwait(false); @@ -526,6 +565,15 @@ private static string ArtifactFile(Run run, string path, string command) return full; } + // The run's headers with the journey's on top: a header the journey names wins, the others stay. + private static Dictionary MergeHeaders(IReadOnlyDictionary defaults, Dictionary journey) + { + var merged = new Dictionary(defaults, StringComparer.OrdinalIgnoreCase); + foreach (var (name, value) in journey) + merged[name] = value; + return merged; + } + private static Dictionary Headers(string json) { using var document = System.Text.Json.JsonDocument.Parse(json); diff --git a/tests/Nullean.Cdp.Tests/Live/EchoServer.cs b/tests/Nullean.Cdp.Tests/Live/EchoServer.cs new file mode 100644 index 0000000..ba67ea7 --- /dev/null +++ b/tests/Nullean.Cdp.Tests/Live/EchoServer.cs @@ -0,0 +1,71 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Nullean.Cdp.Tests.Live; + +/// +/// A local HTTP server whose /echo page shows what reached it (two request headers, the user agent) and what the page +/// itself sees (navigator.userAgent, the viewport width), so a run's defaults can be checked from both sides. +/// +internal sealed class EchoServer : IAsyncDisposable +{ + private readonly HttpListener _listener = new(); + private readonly Task _serving; + + public EchoServer() + { + Url = $"http://localhost:{FreePort()}/"; + _listener.Prefixes.Add(Url); + _listener.Start(); + _serving = Task.Run(ServeAsync); + } + + public string Url { get; } + + private async Task ServeAsync() + { + while (_listener.IsListening) + { + HttpListenerContext context; + try + { + context = await _listener.GetContextAsync(); + } + catch (Exception e) when (e is HttpListenerException or ObjectDisposedException or InvalidOperationException) + { + return; + } + + var response = context.Response; + try + { + string Header(string name) => WebUtility.HtmlEncode(context.Request.Headers[name] ?? "none"); + response.ContentType = "text/html; charset=utf-8"; + await response.OutputStream.WriteAsync(Encoding.UTF8.GetBytes( + "Echo" + + $"

{Header("x-docs-synthetic-monitor")}

{Header("x-journey")}

{Header("user-agent")}

" + + "

" + + "")); + response.Close(); + } + catch (Exception e) when (e is HttpListenerException or ObjectDisposedException or IOException) + { + } + } + } + + private static int FreePort() + { + using var probe = new TcpListener(IPAddress.Loopback, 0); + probe.Start(); + return ((IPEndPoint)probe.LocalEndpoint).Port; + } + + public async ValueTask DisposeAsync() + { + _listener.Stop(); + _listener.Close(); + await _serving; + } +} diff --git a/tests/Nullean.Cdp.Tests/Live/RunDefaultsTests.cs b/tests/Nullean.Cdp.Tests/Live/RunDefaultsTests.cs new file mode 100644 index 0000000..0c73ce8 --- /dev/null +++ b/tests/Nullean.Cdp.Tests/Live/RunDefaultsTests.cs @@ -0,0 +1,71 @@ +using AwesomeAssertions; +using Nullean.Cdp.Journeys; + +namespace Nullean.Cdp.Tests.Live; + +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class RunDefaultsTests(ChromeFixture chrome) +{ + private const string Reads = "open /echo\nget text #ua\nget text #width\nget text #header\nget text #journey\nget text #request-ua\n"; + + private static readonly JourneyOptions Monitor = new() + { + Headers = new Dictionary { ["X-Docs-Synthetic-Monitor"] = "true" }, + UserAgent = "Elastic/Synthetics", + Viewport = new JourneyViewport(1111, 700), + }; + + // What the page saw (user agent, width) and what the server saw (header, journey header, user agent). + private async Task Run(string text, JourneyOptions defaults) + { + await using var server = new EchoServer(); + var result = await JourneyRunner.RunAsync(Journey.Parse(text, "defaults.journey"), chrome.Browser, new JourneyOptions + { + BaseUrl = new Uri(server.Url), + Headers = defaults.Headers, + UserAgent = defaults.UserAgent, + Viewport = defaults.Viewport, + ActionTimeout = TimeSpan.FromSeconds(3), + }); + result.Failure.Should().BeNull(); + return [.. result.Checkpoints.Where(c => c.Command.StartsWith("get text", StringComparison.Ordinal)).Select(c => c.Output)]; + } + + [Test] + public async Task TheRunStartsWithItsViewportUserAgentAndHeaders() => + (await Run(Reads, Monitor)).Should().Equal("Elastic/Synthetics", "1111", "true", "none", "Elastic/Synthetics"); + + [Test] + public async Task WithoutDefaultsNothingChanges() + { + var seen = await Run(Reads, new JourneyOptions()); + + seen[0].Should().Contain("Chrome").And.NotContain("Elastic/Synthetics"); + seen[2].Should().Be("none"); + int.Parse(seen[1], System.Globalization.CultureInfo.InvariantCulture).Should().NotBe(1111); + } + + [Test] + public async Task AJourneysHeadersAddToTheRunsAndWinWhenTheyNameTheSame() => + (await Run("set headers '{\"x-journey\": \"1\", \"X-Docs-Synthetic-Monitor\": \"override\"}'\n" + Reads, Monitor)) + .Should().Equal("Elastic/Synthetics", "1111", "override", "1", "Elastic/Synthetics"); + + [Test] + public async Task AJourneysHeadersKeepTheRunsOthers() => + (await Run("set headers '{\"x-journey\": \"1\"}'\n" + Reads, Monitor)).Skip(2).Should().Equal("true", "1", "Elastic/Synthetics"); + + [Test] + public async Task AJourneysUserAgentReplacesTheRunsAndTheHeaderStays() => + (await Run("set useragent Other/1.0\n" + Reads, Monitor)).Should().Equal("Other/1.0", "1111", "true", "none", "Other/1.0"); + + [Test] + public async Task AJourneysViewportReplacesTheRuns() => + (await Run("set viewport 800 600\n" + Reads, Monitor))[1].Should().Be("800"); + + [Test] + public async Task ADeviceKeepsTheRunsUserAgentButWithoutOneBringsItsOwn() + { + (await Run("set device 'iPhone 14'\n" + Reads, Monitor))[0].Should().Be("Elastic/Synthetics"); + (await Run("set device 'iPhone 14'\n" + Reads, new JourneyOptions()))[0].Should().Contain("iPhone"); + } +} From 98485e11e36fb716cb4da786bf113c9101d02fb6 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 10 Nov 2026 11:33:58 +0100 Subject: [PATCH 2/2] Tests: ignore the argument error a closing HttpListener raises on Windows Disposing the test server while its serve loop starts another read makes Windows throw ArgumentException ("The handle is invalid") from BeginGetContext. The loop already returns on the other disposal errors; this adds that one. It failed the Windows build of two PRs. Co-Authored-By: Claude Opus 5.5 --- tests/Nullean.Cdp.Tests/Live/EchoServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Nullean.Cdp.Tests/Live/EchoServer.cs b/tests/Nullean.Cdp.Tests/Live/EchoServer.cs index ba67ea7..b9c1819 100644 --- a/tests/Nullean.Cdp.Tests/Live/EchoServer.cs +++ b/tests/Nullean.Cdp.Tests/Live/EchoServer.cs @@ -32,7 +32,7 @@ private async Task ServeAsync() { context = await _listener.GetContextAsync(); } - catch (Exception e) when (e is HttpListenerException or ObjectDisposedException or InvalidOperationException) + catch (Exception e) when (e is HttpListenerException or ObjectDisposedException or InvalidOperationException or ArgumentException) { return; }