move [Discourse] url to query param (#4020)

* refactor: switch Discourse url to query param

* tests: fix e2e test with Discourse example

* refactor(Discourse): change query param name to server

* tests: update e2e test with new discourse param name
This commit is contained in:
Caleb Cartwright
2019-09-15 21:15:19 -05:00
committed by repo-ranger[bot]
parent db0ebf1e6a
commit 51d864efc1
5 changed files with 119 additions and 34 deletions

View File

@@ -27,7 +27,7 @@ describe('Main page', function() {
expectBadgeExample(
'Discourse status',
'http://localhost:8080/badge/discourse-online-brightgreen',
'/discourse/:scheme/:host/status'
'/discourse/status?server=https%3A%2F%2Fmeta.discourse.org'
)
})

View File

@@ -0,0 +1,18 @@
'use strict'
const { redirector } = require('..')
module.exports = [
redirector({
category: 'chat',
route: {
base: 'discourse',
pattern: ':protocol(http|https)/:hostAndPath(.+)/:metric',
},
transformPath: ({ metric }) => `/discourse/${metric}`,
transformQueryParams: ({ protocol, hostAndPath }) => ({
server: `${protocol}://${hostAndPath}`,
}),
dateAdded: new Date('2019-09-15'),
}),
]

View File

@@ -0,0 +1,69 @@
'use strict'
const { ServiceTester } = require('../tester')
const t = (module.exports = new ServiceTester({
id: 'DiscourseRedirect',
title: 'DiscourseRedirect',
pathPrefix: '/discourse',
}))
t.create('discourse status')
.get('/https/meta.discourse.org/status.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/status.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)
t.create('discourse topics')
.get('/https/meta.discourse.org/topics.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/topics.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)
t.create('discourse users')
.get('/https/meta.discourse.org/users.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/users.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)
t.create('discourse likes')
.get('/https/meta.discourse.org/likes.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/likes.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)
t.create('discourse posts')
.get('/https/meta.discourse.org/posts.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/discourse/posts.svg?server=${encodeURIComponent(
'https://meta.discourse.org'
)}`
)

View File

@@ -3,7 +3,7 @@
const camelcase = require('camelcase')
const Joi = require('@hapi/joi')
const { metric } = require('../text-formatters')
const { nonNegativeInteger } = require('../validators')
const { nonNegativeInteger, optionalUrl } = require('../validators')
const { BaseJsonService } = require('..')
const schema = Joi.object({
@@ -13,6 +13,10 @@ const schema = Joi.object({
like_count: nonNegativeInteger,
}).required()
const queryParamSchema = Joi.object({
server: optionalUrl.required(),
}).required()
class DiscourseBase extends BaseJsonService {
static get category() {
return 'chat'
@@ -21,9 +25,8 @@ class DiscourseBase extends BaseJsonService {
static buildRoute(metric) {
return {
base: 'discourse',
// Do not base new services on this route pattern.
// See https://github.com/badges/shields/issues/3714
pattern: `:scheme(http|https)/:host/${metric}`,
pattern: metric,
queryParamSchema,
}
}
@@ -31,10 +34,10 @@ class DiscourseBase extends BaseJsonService {
return { label: 'discourse' }
}
async fetch({ scheme, host }) {
async fetch({ server }) {
return this._requestJson({
schema,
url: `${scheme}://${host}/site/statistics.json`,
url: `${server}/site/statistics.json`,
})
}
}
@@ -55,9 +58,9 @@ function DiscourseMetricIntegrationFactory({ metricName, property }) {
return [
{
title: `Discourse ${metricName}`,
namedParams: {
scheme: 'https',
host: 'meta.discourse.org',
namedParams: {},
queryParams: {
server: 'https://meta.discourse.org',
},
staticPreview: this.render({ stat: 100 }),
},
@@ -71,8 +74,8 @@ function DiscourseMetricIntegrationFactory({ metricName, property }) {
}
}
async handle({ scheme, host }) {
const data = await this.fetch({ scheme, host })
async handle(_routeParams, { server }) {
const data = await this.fetch({ server })
return this.constructor.render({ stat: data[property] })
}
}
@@ -87,9 +90,9 @@ class DiscourseStatus extends DiscourseBase {
return [
{
title: `Discourse status`,
namedParams: {
scheme: 'https',
host: 'meta.discourse.org',
namedParams: {},
queryParams: {
server: 'https://meta.discourse.org',
},
staticPreview: this.render(),
},
@@ -103,8 +106,8 @@ class DiscourseStatus extends DiscourseBase {
}
}
async handle({ scheme, host }) {
await this.fetch({ scheme, host })
async handle(_routeParams, { server }) {
await this.fetch({ server })
// if fetch() worked, the server is up
// if it failed, we'll show an error e.g: 'inaccessible'
return this.constructor.render()

View File

@@ -26,7 +26,7 @@ const data = {
}
t.create('Topics')
.get('/https/meta.discourse.org/topics.json')
.get('/topics.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
@@ -35,7 +35,7 @@ t.create('Topics')
.expectBadge({ label: 'discourse', message: '23k topics' })
t.create('Posts')
.get('/https/meta.discourse.org/posts.json')
.get('/posts.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
@@ -44,7 +44,7 @@ t.create('Posts')
.expectBadge({ label: 'discourse', message: '338k posts' })
t.create('Users')
.get('/https/meta.discourse.org/users.json')
.get('/users.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
@@ -53,7 +53,7 @@ t.create('Users')
.expectBadge({ label: 'discourse', message: '31k users' })
t.create('Likes')
.get('/https/meta.discourse.org/likes.json')
.get('/likes.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
@@ -62,7 +62,7 @@ t.create('Likes')
.expectBadge({ label: 'discourse', message: '309k likes' })
t.create('Status')
.get('/https/meta.discourse.org/status.json')
.get('/status.json?server=https://meta.discourse.org')
.intercept(nock =>
nock('https://meta.discourse.org')
.get('/site/statistics.json')
@@ -71,7 +71,7 @@ t.create('Status')
.expectBadge({ label: 'discourse', message: 'online' })
t.create('Status with http (not https)')
.get('/http/meta.discourse.org/status.json')
.get('/status.json?server=http://meta.discourse.org')
.intercept(nock =>
nock('http://meta.discourse.org')
.get('/site/statistics.json')
@@ -79,13 +79,8 @@ t.create('Status with http (not https)')
)
.expectBadge({ label: 'discourse', message: 'online' })
t.create('Status (offline)')
.get('/https/meta.discourse.org/status.json')
.networkOff()
.expectBadge({ label: 'discourse', message: 'inaccessible' })
t.create('Invalid Host')
.get('/https/some.host/status.json')
.get('/status.json?server=https://some.host')
.intercept(nock =>
nock('https://some.host')
.get('/site/statistics.json')
@@ -94,7 +89,7 @@ t.create('Invalid Host')
.expectBadge({ label: 'discourse', message: 'not found' })
t.create('Topics')
.get('/https/meta.discourse.org/topics.json')
.get('/topics.json?server=https://meta.discourse.org')
.expectBadge({
label: 'discourse',
message: Joi.string().regex(
@@ -103,7 +98,7 @@ t.create('Topics')
})
t.create('Posts')
.get('/https/meta.discourse.org/posts.json')
.get('/posts.json?server=https://meta.discourse.org')
.expectBadge({
label: 'discourse',
message: Joi.string().regex(
@@ -112,7 +107,7 @@ t.create('Posts')
})
t.create('Users')
.get('/https/meta.discourse.org/users.json')
.get('/users.json?server=https://meta.discourse.org')
.expectBadge({
label: 'discourse',
message: Joi.string().regex(
@@ -121,7 +116,7 @@ t.create('Users')
})
t.create('Likes')
.get('/https/meta.discourse.org/likes.json')
.get('/likes.json?server=https://meta.discourse.org')
.expectBadge({
label: 'discourse',
message: Joi.string().regex(
@@ -130,5 +125,5 @@ t.create('Likes')
})
t.create('Status')
.get('/https/meta.discourse.org/status.json')
.get('/status.json?server=https://meta.discourse.org')
.expectBadge({ label: 'discourse', message: 'online' })