[QueryStringStatic] service (#3024)

Ref #2673
This commit is contained in:
mbarkhau
2019-02-22 03:12:24 +01:00
committed by Paul Melnikow
parent 9e4a8cec09
commit f6628e62b7
6 changed files with 141 additions and 4 deletions

View File

@@ -71,6 +71,36 @@ function staticBadgeUrl({
return `${baseUrl}/badge/${path}.${format}${suffix}`
}
function queryStringStaticBadgeUrl({
baseUrl = '',
label,
message,
color,
labelColor,
style,
namedLogo,
logoColor,
logoWidth,
logoPosition,
format = 'svg',
}) {
// schemaVersion could be a parameter if we iterate on it,
// for now it's hardcoded to the only supported version.
const schemaVersion = '1'
const suffix = `?${queryString.stringify({
label,
message,
color,
labelColor,
style,
logo: namedLogo,
logoColor,
logoWidth,
logoPosition,
})}`
return `${baseUrl}/static/v${schemaVersion}.${format}${suffix}`
}
function dynamicBadgeUrl({
baseUrl,
datatype,
@@ -109,5 +139,6 @@ module.exports = {
badgeUrlFromPattern,
encodeField,
staticBadgeUrl,
queryStringStaticBadgeUrl,
dynamicBadgeUrl,
}

View File

@@ -6,6 +6,7 @@ const {
badgeUrlFromPattern,
encodeField,
staticBadgeUrl,
queryStringStaticBadgeUrl,
dynamicBadgeUrl,
} = require('./make-badge-url')
@@ -81,6 +82,35 @@ describe('Badge URL generation functions', function() {
}).expect('/badge/-blue-blue.svg')
})
test(queryStringStaticBadgeUrl, () => {
// the query-string library sorts parameters by name
given({
label: 'foo',
message: 'bar',
color: 'blue',
style: 'flat-square',
}).expect(
'/static/v1.svg?color=blue&label=foo&message=bar&style=flat-square'
)
given({
label: 'foo Bar',
message: 'bar Baz',
color: 'blue',
style: 'flat-square',
format: 'png',
namedLogo: 'github',
}).expect(
'/static/v1.png?color=blue&label=foo%20Bar&logo=github&message=bar%20Baz&style=flat-square'
)
given({
label: 'Hello World',
message: 'Привет Мир',
color: '#aabbcc',
}).expect(
'/static/v1.svg?color=%23aabbcc&label=Hello%20World&message=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%9C%D0%B8%D1%80'
)
})
test(dynamicBadgeUrl, () => {
const dataUrl = 'http://example.com/foo.json'
const query = '$.bar'

View File

@@ -62,7 +62,7 @@ module.exports = function coalesceBadge(
labelColor: overrideLabelColor,
} = overrides
// Only use the legacy properties of the new ones are not provided
// Only use the legacy properties if the new ones are not provided
if (typeof overrideColor === 'undefined') {
overrideColor = legacyOverrideColor
}

View File

@@ -179,12 +179,17 @@ export default class Usage extends React.PureComponent {
<VerticalSpace />
<p>Using dash "-" separator</p>
<p>
<Snippet
snippet={`${baseUrl}/badge/<SUBJECT>-<STATUS>-<COLOR>.svg`}
/>
<Snippet snippet={`${baseUrl}/badge/<LABEL>-<MESSAGE>-<COLOR>.svg`} />
</p>
{this.constructor.renderStaticBadgeEscapingRules()}
<p>Using query string parameters</p>
<p>
<Snippet
snippet={`${baseUrl}/static/v1.svg?label=<LABEL>&message=<MESSAGE>&color=<COLOR>`}
/>
</p>
<H3 id="colors">Colors</H3>
<p>

View File

@@ -0,0 +1,32 @@
'use strict'
const Joi = require('joi')
const { BaseStaticService, InvalidParameter } = require('..')
const queryParamSchema = Joi.object({
message: Joi.string().required(),
}).required()
module.exports = class QueryStringStaticBadge extends BaseStaticService {
static get category() {
return 'other'
}
static get route() {
return {
format: 'static/v([0-9])',
capture: ['schemaVersion'],
// All but one of the parameters are parsed via coalesceBadge. This
// reuses what is the override behaviour for other badges.
queryParamSchema,
}
}
handle(namedParams, queryParams) {
if (namedParams.schemaVersion !== '1') {
throw new InvalidParameter({ prettyMessage: 'Invalid schemaVersion' })
}
return { message: queryParams.message }
}
}

View File

@@ -0,0 +1,39 @@
'use strict'
const t = (module.exports = require('../tester').createServiceTester())
t.create('Missing message')
.get('/static/v1.json?label=label&message=&color=blue&style=_shields_test')
.expectJSON({
name: 'label',
value: 'invalid query parameter: message',
color: 'red',
})
t.create('Missing label')
.get('/static/v1.json?label=&message=message&color=blue&style=_shields_test')
.expectJSON({ name: '', value: 'message', color: 'blue' })
t.create('Case is preserved')
.get(
'/static/v1.json?label=LiCeNsE&message=mIt&color=blue&style=_shields_test'
)
.expectJSON({ name: 'LiCeNsE', value: 'mIt', color: 'blue' })
t.create('Set color')
.get(
'/static/v1.json?label=label&message=message&color=yellow&style=_shields_test'
)
.expectJSON({ name: 'label', value: 'message', color: 'yellow' })
t.create('Set color with a number')
.get(
'/static/v1.json?label=label&message=message&color=123&style=_shields_test'
)
.expectJSON({ name: 'label', value: 'message', color: '#123' })
t.create('Set label')
.get(
'/static/v1.json?label=mylabel&message=message&color=blue&style=_shields_test'
)
.expectJSON({ name: 'mylabel', value: 'message', color: 'blue' })