Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions backend/src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
})
Expand Down
2 changes: 1 addition & 1 deletion testing/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 5 additions & 0 deletions testing/src/generate/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export enum DataSection {
RESPONSE_HEADER = "resHeaders",
RESPONSE_BODY = "resBody",
}

export enum ExportTestType {
CURL = "curl",
HTTP = "http",
}
97 changes: 97 additions & 0 deletions testing/src/generate/export.ts
Original file line number Diff line number Diff line change
@@ -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<string, string | object>,
) => {
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
}
1 change: 1 addition & 0 deletions testing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"