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
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"form-data": "^4.0.0",
"got": "^11.8.3",
"js-yaml": "^4.1.0",
"jsonata": "^2.0.6",
"jsonpath-plus": "^7.2.0",
"liquidless": "^1.2.0",
"liquidless-faker": "^1.0.1",
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export type StepCheckJSONPath = {
[key: string]: any
}

export type StepCheckJSONata = {
[key: string]: any
}

export type StepCheckPerformance = {
[key: string]: number
}
Expand Down
74 changes: 67 additions & 7 deletions src/steps/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import {
StepCheckCaptures,
StepCheckJSONPath,
StepCheckJSONata,
StepCheckMatcher,
StepCheckPerformance,
StepCheckValue,
Expand All @@ -33,6 +34,7 @@ import {
WorkflowOptions,
} from '..'
import { Matcher, checkResult } from '../matcher'
import jsonata from 'jsonata'

export type HTTPStepBase = {
url: string
Expand Down Expand Up @@ -108,6 +110,7 @@ export type HTTPStepCaptures = {
export type HTTPStepCapture = {
xpath?: string
jsonpath?: string
jsonata?: string
header?: string
selector?: string
cookie?: string
Expand All @@ -125,6 +128,7 @@ export type HTTPStepCheck = {
json?: object
schema?: object
jsonpath?: StepCheckJSONPath | StepCheckMatcher
jsonata?: StepCheckJSONata
xpath?: StepCheckValue | StepCheckMatcher
selectors?: StepCheckValue | StepCheckMatcher
cookies?: StepCheckValue | StepCheckMatcher
Expand Down Expand Up @@ -287,17 +291,17 @@ export default async function (
if (typeof params.formData[field] != 'object') {
formData.append(field, params.formData[field])
} else if (Array.isArray(params.formData[field])) {
const stepFiles = params.formData[field] as StepFile[];
const stepFiles = params.formData[field] as StepFile[]
for (const stepFile of stepFiles) {
const filepath = path.join(
path.dirname(options?.path || __dirname),
stepFile.file,
stepFile.file
)
appendOptions.filename = path.parse(filepath).base;
appendOptions.filename = path.parse(filepath).base
formData.append(
field,
await fs.promises.readFile(filepath),
appendOptions,
appendOptions
)
}
} else if ((params.formData[field] as StepFile).file) {
Expand All @@ -307,12 +311,20 @@ export default async function (
stepFile.file
)
appendOptions.filename = path.parse(filepath).base
formData.append(field, await fs.promises.readFile(filepath), appendOptions)
formData.append(
field,
await fs.promises.readFile(filepath),
appendOptions
)
} else {
const requestPart = params.formData[field] as HTTPRequestPart
if ('json' in requestPart) {
appendOptions.contentType = 'application/json'
formData.append(field, JSON.stringify(requestPart.json), appendOptions)
formData.append(
field,
JSON.stringify(requestPart.json),
appendOptions
)
} else {
appendOptions.contentType = requestPart.type
formData.append(field, requestPart.value, appendOptions)
Expand Down Expand Up @@ -441,7 +453,21 @@ export default async function (
if (capture.jsonpath) {
try {
const json = JSON.parse(body)
captures[name] = JSONPath({ path: capture.jsonpath, json, wrap: false })
captures[name] = JSONPath({
path: capture.jsonpath,
json,
wrap: false,
})
} catch {
captures[name] = undefined
}
}

if (capture.jsonata) {
try {
const json = JSON.parse(body)
const expression = jsonata(capture.jsonata)
captures[name] = await expression.evaluate(json)
} catch {
captures[name] = undefined
}
Expand Down Expand Up @@ -550,6 +576,40 @@ export default async function (
}
}

// Check JSONata
if (params.check.jsonata) {
stepResult.checks.jsonata = {}
async function evalJSONata(expression: string, json: any) {
try {
return await jsonata(expression).evaluate(json)
} catch {
return json
}
}
try {
const json = JSON.parse(body)
for (const path in params.check.jsonata) {
const expression = params.check.jsonata[path]
const value = path.match(/^\$/) ? await evalJSONata(path, json) : json
const result = await evalJSONata(expression, value)
const { expected, given, passed } = result || {}
stepResult.checks.jsonata[path] = {
expected: expected !== undefined ? expected : expression,
given: given !== undefined ? given : value,
passed: passed !== undefined ? passed : !!result,
}
}
} catch {
for (const path in params.check.jsonata) {
stepResult.checks.jsonata[path] = {
expected: params.check.jsonata[path],
given: '<body>',
passed: false,
}
}
}
}

// Check XPath
if (params.check.xpath) {
stepResult.checks.xpath = {}
Expand Down
9 changes: 9 additions & 0 deletions tests/jsonata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { runFromFile } from '../src/index'

runFromFile('./tests/jsonata.yml').then(({ result }) => {
for (const step of result.tests[0].steps) {
for (const key in step.checks) {
console.log(JSON.stringify(step.checks[key]))
}
}
})
30 changes: 30 additions & 0 deletions tests/jsonata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '1.1'
name: JSONata test
tests:
example:
steps:
- name: JSONata evaluation test
http:
url: https://httpbin.org/ip
method: GET
captures:
ip:
jsonata: origin
check:
jsonata:
formatcheck: |
$match(origin, /(\d{1,3}\.){3}\d{1,3}/)
# rewrite the above JSONata expression using the property name
$.origin: $match($, /(\d{1,3}\.){3}\d{1,3}/)
- name: JSONata return object sample
http:
url: https://jsonplaceholder.typicode.com/posts/
method: GET
check:
jsonata:
notmatch: |
(
$a := 99;
$b := $count($);
{ "expected": $a, "given": $b, "passed": ($a = $b) }
$count($[userId=1]): 10
1 change: 1 addition & 0 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ const ee = new EventEmitter()
runFromFile('./tests/basic.yml').then(({ result }) => console.log(result.tests[0].steps))
runFromFile('./tests/filelist.yml').then(({ result }) => console.log(result.tests[0].steps))
runFromFile('./tests/multipart.yml').then(({ result }) => console.log(result.tests[0].steps))
runFromFile('./tests/jsonata.yml').then(({ result }) => console.log(result.tests[0].steps))