fix type errors in [DynamicXml] service (#4041)
* fix: type errors in DynamicXml service * tests: add more tests for DynamicXml * tests: another DynamicXml service test
This commit is contained in:
@@ -27,18 +27,11 @@ module.exports = class DynamicXml extends BaseService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async handle(namedParams, { url, query: pathExpression, prefix, suffix }) {
|
transform({ pathExpression, buffer }) {
|
||||||
// e.g. //book[2]/@id
|
// e.g. //book[2]/@id
|
||||||
const pathIsAttr = (
|
const pathIsAttr = (
|
||||||
pathExpression.split('/').slice(-1)[0] || ''
|
pathExpression.split('/').slice(-1)[0] || ''
|
||||||
).startsWith('@')
|
).startsWith('@')
|
||||||
|
|
||||||
const { buffer } = await this._request({
|
|
||||||
url,
|
|
||||||
options: { headers: { Accept: 'application/xml, text/xml' } },
|
|
||||||
errorMessages,
|
|
||||||
})
|
|
||||||
|
|
||||||
const parsed = new DOMParser().parseFromString(buffer)
|
const parsed = new DOMParser().parseFromString(buffer)
|
||||||
|
|
||||||
let values
|
let values
|
||||||
@@ -47,14 +40,42 @@ module.exports = class DynamicXml extends BaseService {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new InvalidParameter({ prettyMessage: e.message })
|
throw new InvalidParameter({ prettyMessage: e.message })
|
||||||
}
|
}
|
||||||
values = values.map((node, i) =>
|
|
||||||
pathIsAttr ? node.value : node.firstChild.data
|
if (!Array.isArray(values)) {
|
||||||
)
|
throw new InvalidResponse({
|
||||||
|
prettyMessage: 'unsupported query',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
values = values.reduce((accum, node) => {
|
||||||
|
if (pathIsAttr) {
|
||||||
|
accum.push(node.value)
|
||||||
|
} else if (node.firstChild) {
|
||||||
|
accum.push(node.firstChild.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accum
|
||||||
|
}, [])
|
||||||
|
|
||||||
if (!values.length) {
|
if (!values.length) {
|
||||||
throw new InvalidResponse({ prettyMessage: 'no result' })
|
throw new InvalidResponse({ prettyMessage: 'no result' })
|
||||||
}
|
}
|
||||||
|
|
||||||
return renderDynamicBadge({ value: values, prefix, suffix })
|
return { values }
|
||||||
|
}
|
||||||
|
|
||||||
|
async handle(_namedParams, { url, query: pathExpression, prefix, suffix }) {
|
||||||
|
const { buffer } = await this._request({
|
||||||
|
url,
|
||||||
|
options: { headers: { Accept: 'application/xml, text/xml' } },
|
||||||
|
errorMessages,
|
||||||
|
})
|
||||||
|
|
||||||
|
const { values: value } = this.transform({
|
||||||
|
pathExpression,
|
||||||
|
buffer,
|
||||||
|
})
|
||||||
|
|
||||||
|
return renderDynamicBadge({ value, prefix, suffix })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
services/dynamic/dynamic-xml.spec.js
Normal file
31
services/dynamic/dynamic-xml.spec.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const { expect } = require('chai')
|
||||||
|
const sinon = require('sinon')
|
||||||
|
const xpath = require('xpath')
|
||||||
|
const { exampleXml } = require('./dynamic-response-fixtures')
|
||||||
|
const DynamicXml = require('./dynamic-xml.service')
|
||||||
|
const { InvalidResponse } = require('..')
|
||||||
|
|
||||||
|
describe('DynamicXml', function() {
|
||||||
|
describe('transform()', function() {
|
||||||
|
beforeEach(function() {
|
||||||
|
sinon.stub(xpath, 'select').returns(undefined)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws InvalidResponse on unsupported query', function() {
|
||||||
|
expect(() =>
|
||||||
|
DynamicXml.prototype.transform({
|
||||||
|
pathExpression: '//book/title',
|
||||||
|
buffer: exampleXml,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.to.throw(InvalidResponse)
|
||||||
|
.with.property('prettyMessage', 'unsupported query')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -176,3 +176,33 @@ t.create('request should set Accept header')
|
|||||||
.reply(200, exampleXml)
|
.reply(200, exampleXml)
|
||||||
)
|
)
|
||||||
.expectBadge({ label: 'custom badge', message: 'Midnight Rain' })
|
.expectBadge({ label: 'custom badge', message: 'Midnight Rain' })
|
||||||
|
|
||||||
|
// https://github.com/badges/shields/issues/3814
|
||||||
|
t.create('no result')
|
||||||
|
.get(
|
||||||
|
`.json?${queryString.stringify({
|
||||||
|
url: exampleUrl,
|
||||||
|
query: '//book[1]/title/text()',
|
||||||
|
})}`
|
||||||
|
)
|
||||||
|
.intercept(withExampleXml)
|
||||||
|
.expectBadge({
|
||||||
|
label: 'custom badge',
|
||||||
|
message: 'no result',
|
||||||
|
color: 'lightgrey',
|
||||||
|
})
|
||||||
|
|
||||||
|
// https://github.com/badges/shields/issues/4017
|
||||||
|
t.create('unsupported query')
|
||||||
|
.get(
|
||||||
|
`.json?${queryString.stringify({
|
||||||
|
url: exampleUrl,
|
||||||
|
query: 'string(//book[1]/title)',
|
||||||
|
})}`
|
||||||
|
)
|
||||||
|
.intercept(withExampleXml)
|
||||||
|
.expectBadge({
|
||||||
|
label: 'custom badge',
|
||||||
|
message: 'unsupported query',
|
||||||
|
color: 'lightgrey',
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user