diff --git a/packages/core/src/core/pardon.ts b/packages/core/src/core/pardon.ts index 0b55ad3d..2f555a0f 100644 --- a/packages/core/src/core/pardon.ts +++ b/packages/core/src/core/pardon.ts @@ -159,6 +159,16 @@ export const PardonFetchExecution = pardonExecution({ async match({ context: { url, init, ...context } }) { const fetchObject = fetchIntoObject(url, init); + if (typeof context.values?.method === "string") { + fetchObject.method ??= context.values?.method; + + if (context.values?.method !== fetchObject.method) { + throw new Error( + "specified values method does not match reqeust method", + ); + } + } + // pathname undefined in some places is allowed (matches any template), // but we want to ensure it's set when matching requests. // but allow undefined origin and pathname for undefined URLs matched/rendered only by values. diff --git a/packages/core/src/entry/main/cli/options.ts b/packages/core/src/entry/main/cli/options.ts index 2c13d028..7eda781b 100644 --- a/packages/core/src/entry/main/cli/options.ts +++ b/packages/core/src/entry/main/cli/options.ts @@ -123,8 +123,15 @@ export async function processOptions( method = args.shift()!; } + if (values.method !== undefined && typeof values.method !== "string") { + throw new Error("method should be a string"); + } + if (!args.length) { - return { values, init: { method: method ?? "GET" } }; + return { + values, + init: { method: (method ?? values.method ?? "GET") as string }, + }; } const request = await parseMainArgument(args.shift()!); @@ -138,7 +145,15 @@ export async function processOptions( throw new PardonError("http method specified twice"); } - request.method ??= method; + if (typeof values.method !== "string") { + throw new Error("method in values must be a string."); + } + + request.method ??= method ?? values.method ?? "GET"; + + if (values.method && values.method && values.method !== request.method) { + throw new Error("method in input does not match method in request"); + } if (data !== undefined && dataRaw !== undefined) { throw new Error("both --data and --data-raw should not be specified"); @@ -160,10 +175,6 @@ export async function processOptions( request!.headers.append(key, value); } - if (!values.endpoint && !values.method) { - request.method ??= method ?? "GET"; - } - return { url: intoURL(request), init: request, values }; } diff --git a/packages/docs/src/components/Exercises.astro b/packages/docs/src/components/Exercises.astro index db1b9d68..59b41873 100644 --- a/packages/docs/src/components/Exercises.astro +++ b/packages/docs/src/components/Exercises.astro @@ -19,7 +19,7 @@ const props = Astro.props;