refactor(Website): move url to query param (#4028)

This commit is contained in:
Caleb Cartwright
2019-09-15 20:19:55 -05:00
committed by repo-ranger[bot]
parent 76b2e2634a
commit d1c0165afd
4 changed files with 96 additions and 50 deletions

View File

@@ -51,35 +51,57 @@ Old documentation, for reference:
</p>
*/
module.exports = redirector({
category: 'monitoring',
route: {
base: '',
format:
'website-(([^-/]|--|//)+)-(([^-/]|--|//)+)(-(([^-/]|--|//)+)-(([^-/]|--|//)+))?/([^/]+)/(.+?)',
capture: [
// Some of these could be made into non-capturing groups so these unused
// params would not need to be declared.
'upMessage',
'unused2',
'downMessage',
'unused4',
'unused5',
'upColor',
'unused7',
'downColor',
'unused8',
'protocol',
'hostAndPath',
],
},
transformPath: ({ protocol, hostAndPath }) =>
`/website/${protocol}/${hostAndPath}`,
transformQueryParams: ({ upMessage, downMessage, upColor, downColor }) => ({
up_message: upMessage ? escapeFormatSlashes(upMessage) : undefined,
down_message: downMessage ? escapeFormatSlashes(downMessage) : undefined,
up_color: upColor,
down_color: downColor,
module.exports = [
redirector({
category: 'monitoring',
route: {
base: '',
format:
'website-(([^-/]|--|//)+)-(([^-/]|--|//)+)(-(([^-/]|--|//)+)-(([^-/]|--|//)+))?/([^/]+)/(.+?)',
capture: [
// Some of these could be made into non-capturing groups so these unused
// params would not need to be declared.
'upMessage',
'unused2',
'downMessage',
'unused4',
'unused5',
'upColor',
'unused7',
'downColor',
'unused8',
'protocol',
'hostAndPath',
],
},
transformPath: () => '/website',
transformQueryParams: ({
upMessage,
downMessage,
upColor,
downColor,
protocol,
hostAndPath,
}) => ({
up_message: upMessage ? escapeFormatSlashes(upMessage) : undefined,
down_message: downMessage ? escapeFormatSlashes(downMessage) : undefined,
up_color: upColor,
down_color: downColor,
url: `${protocol}://${hostAndPath}`,
}),
dateAdded: new Date('2019-03-08'),
}),
dateAdded: new Date('2019-03-08'),
})
redirector({
category: 'monitoring',
name: 'WebsiteUrlQueryParamRedirect',
route: {
base: 'website',
pattern: ':protocol(https|http)/:hostAndPath+',
},
transformPath: () => '/website',
transformQueryParams: ({ protocol, hostAndPath }) => ({
url: `${protocol}://${hostAndPath}`,
}),
dateAdded: new Date('2019-09-17'),
}),
]

View File

@@ -15,7 +15,9 @@ t.create('Website with custom messages')
.expectStatus(301)
.expectHeader(
'Location',
'/website/https/www.google.com.svg?down_message=down&up_message=up'
`/website.svg?down_message=down&up_message=up&url=${encodeURIComponent(
'https://www.google.com'
)}`
)
t.create('Website with custom messages and colors')
@@ -25,5 +27,22 @@ t.create('Website with custom messages and colors')
.expectStatus(301)
.expectHeader(
'Location',
'/website/https/www.google.com.svg?down_color=gray&down_message=down&up_color=yellow&up_message=up'
`/website.svg?down_color=gray&down_message=down&up_color=yellow&up_message=up&url=${encodeURIComponent(
'https://www.google.com'
)}`
)
t.create('Website to queryParam with custom messages and colors')
.get(
'/website/https/www.google.com.svg?down_color=gray&down_message=down&up_color=yellow&up_message=up',
{
followRedirect: false,
}
)
.expectStatus(301)
.expectHeader(
'Location',
`/website.svg?down_color=gray&down_message=down&up_color=yellow&up_message=up&url=${encodeURIComponent(
'https://www.google.com'
)}`
)

View File

@@ -1,5 +1,7 @@
'use strict'
const Joi = require('@hapi/joi')
const { optionalUrl } = require('../validators')
const {
queryParamSchema,
exampleQueryParams,
@@ -27,6 +29,10 @@ const documentation = `
</p>
`
const urlQueryParamSchema = Joi.object({
url: optionalUrl.required(),
}).required()
module.exports = class Website extends BaseService {
static get category() {
return 'monitoring'
@@ -34,11 +40,9 @@ module.exports = class Website extends BaseService {
static get route() {
return {
base: 'website',
// Do not base new services on this route pattern.
// See https://github.com/badges/shields/issues/3714
pattern: ':protocol(https|http)/:hostAndPath+',
queryParamSchema,
base: '',
pattern: 'website',
queryParamSchema: queryParamSchema.concat(urlQueryParamSchema),
}
}
@@ -46,11 +50,11 @@ module.exports = class Website extends BaseService {
return [
{
title: 'Website',
namedParams: {
protocol: 'https',
hostAndPath: 'shields.io',
namedParams: {},
queryParams: {
...exampleQueryParams,
...{ url: 'https://shields.io' },
},
queryParams: exampleQueryParams,
staticPreview: renderWebsiteStatus({ isUp: true }),
documentation,
},
@@ -64,12 +68,13 @@ module.exports = class Website extends BaseService {
}
async handle(
{ protocol, hostAndPath },
_routeParams,
{
up_message: upMessage,
down_message: downMessage,
up_color: upColor,
down_color: downColor,
url,
}
) {
let isUp
@@ -77,7 +82,7 @@ module.exports = class Website extends BaseService {
const {
res: { statusCode },
} = await this._request({
url: `${protocol}://${hostAndPath}`,
url,
options: {
method: 'HEAD',
},

View File

@@ -3,25 +3,25 @@
const t = (module.exports = require('../tester').createServiceTester())
t.create('status of http://shields.io')
.get('/http/shields.io.json')
.get('/website.json?url=http://shields.io')
.expectBadge({ label: 'website', message: 'up', color: 'brightgreen' })
t.create('status of https://shields.io')
.get('/https/shields.io.json')
.get('/website.json?url=https://shields.io')
.expectBadge({ label: 'website', message: 'up', color: 'brightgreen' })
t.create('status of nonexistent domain')
.get('/https/shields-io.io.json')
.get('/website.json?url=http://shields.io.io')
.expectBadge({ label: 'website', message: 'down', color: 'red' })
t.create('status when network is off')
.get('/http/shields.io.json')
.get('/website.json?url=http://shields.io')
.networkOff()
.expectBadge({ label: 'website', message: 'down', color: 'red' })
t.create('custom online label, online message and online color')
.get(
'/http/online.com.json?up_message=up&down_message=down&up_color=green&down_color=grey'
'/website.json?url=http://online.com&up_message=up&down_message=down&up_color=green&down_color=grey'
)
.intercept(nock =>
nock('http://online.com')
@@ -32,7 +32,7 @@ t.create('custom online label, online message and online color')
t.create('custom offline message and offline color')
.get(
'/http/offline.com.json?up_message=up&down_message=down&up_color=green&down_color=grey'
'/website.json?url=http://offline.com&up_message=up&down_message=down&up_color=green&down_color=grey'
)
.intercept(nock =>
nock('http://offline.com')