Skip to content

Commit

Permalink
feat: extract values from xml
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeflug committed Jan 2, 2023
1 parent 66e691b commit 89f34c9
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 5 deletions.
43 changes: 42 additions & 1 deletion package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@release-it/conventional-changelog": "^5.1.1",
"@types/jest": "^26.0.21",
"@types/jsonpath-plus": "^5.0.2",
"@types/xmldom": "^0.1.31",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"eslint": "^7.22.0",
Expand All @@ -51,6 +52,8 @@
},
"dependencies": {
"jsonpath-plus": "^6.0.1",
"sweetalert": "^2.1.2"
"sweetalert": "^2.1.2",
"xmldom": "^0.6.0",
"xpath": "^0.0.32"
}
}
34 changes: 33 additions & 1 deletion src/full-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { deletionResponseHook } from './response-hook/deletion/response-hook'
jest.mock('sweetalert', () => ({}))

describe('Test through entire system', () => {
it('should save variable using request header and response hook', async () => {
it('should save json variable using request header and response hook', async () => {
const ticketNumber = 'ID-123'
const workspaceId = 'wrk_23232323'
const context = {
Expand Down Expand Up @@ -45,6 +45,38 @@ describe('Test through entire system', () => {
expect(result).toEqual(ticketNumber)
})

it('should save xml variable using request header and response hook', async () => {
const ticketNumber = 'ID-123'
const workspaceId = 'wrk_23232323'
const context = {
request: createMockHeaders(),
response: {
getBody: jest.fn().mockReturnValue(`<?xml version="1.0" encoding="UTF-8" ?>
<root>
<ticket>${ticketNumber}</ticket>
<other>doesNotMatter</other>
</root>`),
},
store: createMockStore(),
meta: {
workspaceId,
},
}
const variableDefinition: VariableDefinition = {
variableName: 'ticketNumber',
type: 'bodyXml',
arg: '//ticket/text()',
workspaceId,
}
context.request.setHeader(createVariableDefinitionHeader(variableDefinition), 'doesNotMatter')

await variableDeclarationHeaderRequestHook((context as unknown) as RequestHookContext)
await variableSavingResponseHook((context as unknown) as ResponseHookContext)
const result = await savedVariableTemplateTag.run((context as unknown) as TemplateRunContext, 'ticketNumber')

expect(result).toEqual(ticketNumber)
})

it('should save static variable using request header and response hook', async () => {
const staticValue = 'this is the static value'
const workspaceId = 'wrk_23232323'
Expand Down
3 changes: 2 additions & 1 deletion src/value-extractors/all-value-extractors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { valueExtractorHeader } from './value-extractor-header'
import { valueExtractorJson } from './value-extractor-json'
import { valueExtractorStatic } from './value-extractor-static'
import { valueExtractorXml } from './value-extractor-xml'

export const allValueExtractors = [valueExtractorJson, valueExtractorHeader, valueExtractorStatic]
export const allValueExtractors = [valueExtractorJson, valueExtractorHeader, valueExtractorStatic, valueExtractorXml]
2 changes: 1 addition & 1 deletion src/value-extractors/value-extractor-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ValueExtractor } from './value-extractor'
export const valueExtractorJson: ValueExtractor = {
type: 'bodyJson',
display: {
name: 'Body Attribute',
name: 'Body Attribute (JSON)',
description: 'value of response body',
argument: 'Response JSON Path',
},
Expand Down
23 changes: 23 additions & 0 deletions src/value-extractors/value-extractor-xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { VariableDefinition } from '../custom-header-format/variable-definition/variable-definition'
import { ResponseHookContext } from '../insomnia/types/response-hook-context'
import { ValueExtractor } from './value-extractor'
import { DOMParser } from 'xmldom'
import { select } from 'xpath'

export const valueExtractorXml: ValueExtractor = {
type: 'bodyXml',
display: {
name: 'Body Attribute (XML)',
description: 'value of response body',
argument: 'Response XPath',
},
extractFromResponse: async (
variableDefinition: VariableDefinition,
context: ResponseHookContext,
): Promise<string | null | undefined> => {
const body = context.response.getBody()?.toString('utf-8') || ''
const xmlDoc = new DOMParser().parseFromString(body)
const nodes = select(variableDefinition.arg, xmlDoc)
return nodes ? String(nodes) : null
},
}

0 comments on commit 89f34c9

Please sign in to comment.