diff --git a/backend/src/analyzer.ts b/backend/src/analyzer.ts index b1b55ae5..a83b1e99 100644 --- a/backend/src/analyzer.ts +++ b/backend/src/analyzer.ts @@ -44,10 +44,13 @@ const filteredProcessedData = ( ) => { const entry = {} Object.keys(processedDataEntry ?? {}).forEach(e => { - const isGraphqlSection = GRAPHQL_SECTIONS.includes( - e.split(".")[0] as DataSection, - ) - if ((isGraphqlSection && e.includes(`${filter}`)) || !isGraphqlSection) { + const split = e.split(".") + const isGraphqlSection = GRAPHQL_SECTIONS.includes(split[0] as DataSection) + if ( + (isGraphqlSection && + (e.includes(`${filter}.`) || e === `${split[0]}.${filter}`)) || + !isGraphqlSection + ) { entry[e] = processedDataEntry[e] } }) diff --git a/testing/package.json b/testing/package.json index 5fbf2482..ac498404 100644 --- a/testing/package.json +++ b/testing/package.json @@ -1,6 +1,6 @@ { "name": "@metlo/testing", - "version": "0.3.22", + "version": "0.3.23", "license": "MIT", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/testing/src/generate/enums.ts b/testing/src/generate/enums.ts index 10aede32..8c933a14 100644 --- a/testing/src/generate/enums.ts +++ b/testing/src/generate/enums.ts @@ -16,3 +16,8 @@ export enum DataSection { RESPONSE_HEADER = "resHeaders", RESPONSE_BODY = "resBody", } + +export enum ExportTestType { + CURL = "curl", + HTTP = "http", +} diff --git a/testing/src/generate/export.ts b/testing/src/generate/export.ts new file mode 100644 index 00000000..fbe98ef0 --- /dev/null +++ b/testing/src/generate/export.ts @@ -0,0 +1,97 @@ +import * as Handlebars from "handlebars" +import { TestConfig, TestStep } from "../types/test" +import { Context } from "../types/context" +import { makeRequest } from "../runner/request" +import { ExportTestType } from "./enums" + +const getCurlStepExport = (step: TestStep, context: Context) => { + const reqConfig = makeRequest(step.request, context) + if (!reqConfig.url) { + throw new Error("No URL defined for test.") + } + if (!reqConfig.method) { + throw new Error("No method defined for test.") + } + let cmd = `curl -X ${reqConfig.method} ${reqConfig.url}` + if (reqConfig.headers) { + for (const header in reqConfig.headers) { + if (header !== "undefined") { + cmd += ` -H "${header}: ${reqConfig.headers[header]}"` + } + } + } + if (reqConfig.data) { + cmd += ` -d '${reqConfig.data}'` + } + return cmd +} + +const getHttpStepExport = (step: TestStep, context: Context) => { + const reqConfig = makeRequest(step.request, context) + if (!reqConfig.url) { + throw new Error("No URL defined for test.") + } + if (!reqConfig.method) { + throw new Error("No method defined for test.") + } + const url = new URL(reqConfig.url) + const path = url.pathname + const host = url.hostname + + let res = `${reqConfig.method} ${path} HTTP/1.1` + if (reqConfig.headers) { + if (!reqConfig.headers["host"] || !reqConfig.headers["Host"]) { + res += `\nHost: ${host}` + } + for (const header in reqConfig.headers) { + if (header !== "undefined") { + res += `\n${header}: ${reqConfig.headers[header]}` + } + } + } + if (reqConfig.data) { + res += `\n\n${reqConfig.data}` + } + return res +} + +const getStepRes = (step: TestStep, context: Context, exportType: string) => { + switch (exportType) { + case ExportTestType.CURL: + return getCurlStepExport(step, context) + case ExportTestType.HTTP: + return getHttpStepExport(step, context) + default: + return "Invalid Export Type" + } +} + +export const getExportedTestSteps = ( + test: TestConfig, + exportType: string, + env?: Record, +) => { + const context = { + cookies: {}, + envVars: env || {}, + } + + if (test.env) { + let currentEnv = { ...env } + test.env.forEach( + e => (currentEnv[e.name] = Handlebars.compile(e.value)(currentEnv)), + ) + context.envVars = currentEnv + } + const testStack = [...test.test] + const resp: string[] = [] + for (let i = 0; i < testStack.length; i++) { + const step = testStack[i] as TestStep + if (step.payload) { + throw new Error("Cannot export for tests with payload option.") + } + const stepRes = getStepRes(step, context, exportType) + resp.push(stepRes) + } + return resp +} diff --git a/testing/src/index.ts b/testing/src/index.ts index 62b1dd2d..668f984b 100644 --- a/testing/src/index.ts +++ b/testing/src/index.ts @@ -146,3 +146,4 @@ export { TemplateConfig, ResourceConfigParseRes } from "./types/resource_config" export { runTest, estimateTest } from "./runner" export { generateAuthTests } from "./generate/generate-auth-tests" export { findEndpointResourcePermissions } from "./generate/permissions" +export { getExportedTestSteps } from "./generate/export"