Skip to content

Commit

Permalink
uri -> url
Browse files Browse the repository at this point in the history
kept support for uri, incase of any already existing badges.
  • Loading branch information
RedSparr0w committed Mar 4, 2018
1 parent c6f47d8 commit 094da78
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 55 deletions.
2 changes: 1 addition & 1 deletion frontend/components/dynamic-badge-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class DynamicBadgeMaker extends React.Component {
className="short"
value={this.state.url}
onChange={event => this.setState({ url: event.target.value })}
placeholder="json url" /> {}
placeholder="url" /> {}
<input
className="short"
value={this.state.query}
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class Usage extends React.PureComponent {
<DynamicBadgeMaker baseUri={baseUri} />

<p>
<code>/badge/dynamic/&lt;DATATYPE&gt;.svg?uri=&lt;URI&gt;&amp;label=&lt;LABEL&gt;&amp;query=&lt;<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>|<a href="https://www.npmjs.com/package/xpath" target="_BLANK" title="XPath syntax">//data/subdata</a>&gt;&amp;colorB=&lt;COLOR&gt;&amp;prefix=&lt;PREFIX&gt;&amp;suffix=&lt;SUFFIX&gt;</code>
<code>/badge/dynamic/&lt;DATATYPE&gt;.svg?url=&lt;URL&gt;&amp;label=&lt;LABEL&gt;&amp;query=&lt;<a href="https://www.npmjs.com/package/jsonpath" target="_BLANK" title="JSONdata syntax">$.DATA.SUBDATA</a>|<a href="https://www.npmjs.com/package/xpath" target="_BLANK" title="XPath syntax">//data/subdata</a>&gt;&amp;colorB=&lt;COLOR&gt;&amp;prefix=&lt;PREFIX&gt;&amp;suffix=&lt;SUFFIX&gt;</code>
</p>

<hr className="spacing" />
Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/badge-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export function staticBadgeUrl(baseUrl, subject, status, color, options) {
}

// Options can include: { prefix, suffix, color, longCache, style, queryParams }
export function dynamicBadgeUrl(baseUrl, datatype, label, jsonUrl, query, options = {}) {
export function dynamicBadgeUrl(baseUrl, datatype, label, dataUrl, query, options = {}) {
const { prefix, suffix, color, queryParams = {}, ...rest } = options;

Object.assign(queryParams, {
label,
uri: jsonUrl,
url: dataUrl,
query,
});

Expand Down
10 changes: 5 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7468,7 +7468,7 @@ camp.route(/^\/maven-metadata\/v\/(https?)\/(.+\.xml)\.(svg|png|gif|jpg|json)$/,
// User defined sources - JSON response
camp.route(/^\/badge\/dynamic\/(json|xml)\.(svg|png|gif|jpg|json)$/,
cache({
queryParams: ['uri', 'query', 'prefix', 'suffix'],
queryParams: ['uri', 'url', 'query', 'prefix', 'suffix'],
handler: function(query, match, sendBadge, request) {
var type = match[1];
var format = match[2];
Expand All @@ -7478,15 +7478,15 @@ cache({

var badgeData = getBadgeData('custom badge', query);

if (!query.uri || !query.query){
if (!query.uri && !query.url || !query.query){
setBadgeColor(badgeData, 'red');
badgeData.text[1] = !query.uri ? 'no uri specified' : 'no query specified';
badgeData.text[1] = !query.query ? 'no query specified' : 'no url specified';
sendBadge(format, badgeData);
return;
}
var uri = encodeURI(decodeURIComponent(query.uri));
var url = encodeURI(decodeURIComponent(query.url || query.uri));

request(uri, (err, res, data) => {
request(url, (err, res, data) => {
try {
if (checkErrorResponse(badgeData, err, res, 'resource not found')) {
return;
Expand Down
46 changes: 25 additions & 21 deletions service-tests/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,60 @@ const t = new ServiceTester({ id: 'dynamic-json', title: 'User Defined JSON Sour
module.exports = t;

t.create('Connection error')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.name&label=Package Name&style=_shields_test')
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$.name&label=Package Name&style=_shields_test')
.networkOff()
.expectJSON({ name: 'Package Name', value: 'inaccessible', colorB: colorsB.red });

t.create('No URI specified')
t.create('No URL specified')
.get('.json?query=$.name&label=Package Name&style=_shields_test')
.expectJSON({ name: 'Package Name', value: 'no uri specified', colorB: colorsB.red });
.expectJSON({ name: 'Package Name', value: 'no url specified', colorB: colorsB.red });

t.create('No query specified')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&label=Package Name&style=_shields_test')
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&label=Package Name&style=_shields_test')
.expectJSON({ name: 'Package Name', value: 'no query specified', colorB: colorsB.red });

t.create('JSON from uri')
t.create('JSON from url')
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$.name&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'gh-badges', colorB: colorsB.brightgreen });

t.create('JSON from uri (support uri query paramater)')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.name&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'gh-badges', colorB: colorsB.brightgreen });

t.create('JSON from uri | multiple results')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$..keywords[0:2:1]')
t.create('JSON from url | multiple results')
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$..keywords[0:2:1]')
.expectJSON({ name: 'custom badge', value: 'GitHub, badge' });

t.create('JSON from uri | caching with new query params')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.version')
t.create('JSON from url | caching with new query params')
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$.version')
.expectJSONTypes(Joi.object().keys({
name: 'custom badge',
value: Joi.string().regex(/^\d+(\.\d+)?(\.\d+)?$/)
}));

t.create('JSON from uri | with prefix & suffix & label')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.version&prefix=v&suffix= dev&label=Shields')
t.create('JSON from url | with prefix & suffix & label')
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$.version&prefix=v&suffix= dev&label=Shields')
.expectJSONTypes(Joi.object().keys({
name: 'Shields',
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?\sdev$/)
}));

t.create('JSON from uri | object doesnt exist')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.does_not_exist&style=_shields_test')
t.create('JSON from url | object doesnt exist')
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$.does_not_exist&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'no result', colorB: colorsB.lightgrey });

t.create('JSON from uri | invalid uri')
.get('.json?uri=https://github.com/badges/shields/raw/master/notafile.json&query=$.version&style=_shields_test')
t.create('JSON from url | invalid url')
.get('.json?url=https://github.com/badges/shields/raw/master/notafile.json&query=$.version&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'resource not found', colorB: colorsB.lightgrey });

t.create('JSON from uri | user color overrides default')
.get('.json?uri=https://github.com/badges/shields/raw/master/package.json&query=$.name&colorB=10ADED&style=_shields_test')
t.create('JSON from url | user color overrides default')
.get('.json?url=https://github.com/badges/shields/raw/master/package.json&query=$.name&colorB=10ADED&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'gh-badges', colorB: '#10ADED' });

t.create('JSON from uri | error color overrides default')
.get('.json?uri=https://github.com/badges/shields/raw/master/notafile.json&query=$.version&style=_shields_test')
t.create('JSON from url | error color overrides default')
.get('.json?url=https://github.com/badges/shields/raw/master/notafile.json&query=$.version&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'resource not found', colorB: colorsB.lightgrey });

t.create('JSON from uri | error color overrides user specified')
t.create('JSON from url | error color overrides user specified')
.get('.json?query=$.version&colorB=10ADED&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'no uri specified', colorB: colorsB.red });
.expectJSON({ name: 'custom badge', value: 'no url specified', colorB: colorsB.red });
54 changes: 29 additions & 25 deletions service-tests/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,70 +15,74 @@ const t = new ServiceTester({ id: 'dynamic-xml', title: 'User Defined XML Source
module.exports = t;

t.create('Connection error')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/name&label=Package Name&style=_shields_test')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/name&label=Package Name&style=_shields_test')
.networkOff()
.expectJSON({ name: 'Package Name', value: 'inaccessible', colorB: colorsB.red });

t.create('No URI specified')
t.create('No URL specified')
.get('.json?query=//name&label=Package Name&style=_shields_test')
.expectJSON({ name: 'Package Name', value: 'no uri specified', colorB: colorsB.red });
.expectJSON({ name: 'Package Name', value: 'no url specified', colorB: colorsB.red });

t.create('No query specified')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&label=Package Name&style=_shields_test')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&label=Package Name&style=_shields_test')
.expectJSON({ name: 'Package Name', value: 'no query specified', colorB: colorsB.red });

t.create('XML from uri')
t.create('XML from url')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/name&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'IndieGala Helper', colorB: colorsB.brightgreen });

t.create('XML from uri (support uri query paramater)')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/name&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'IndieGala Helper', colorB: colorsB.brightgreen });

t.create('XML from uri (attribute)')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/reviews/@num')
t.create('XML from url (attribute)')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/reviews/@num')
.expectJSONTypes(Joi.object().keys({
name: 'custom badge',
value: Joi.string().regex(/^\d+$/)
}));

t.create('XML from uri | multiple results')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/compatible_applications/application/name')
t.create('XML from url | multiple results')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/compatible_applications/application/name')
.expectJSONTypes(Joi.object().keys({
name: 'custom badge',
value: Joi.string().regex(/^Firefox( for Android)?,\sFirefox( for Android)?$/)
}));

t.create('XML from uri | caching with new query params')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/version')
t.create('XML from url | caching with new query params')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/version')
.expectJSONTypes(Joi.object().keys({
name: 'custom badge',
value: isSemver
}));

t.create('XML from uri | with prefix & suffix & label')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=//version&prefix=v&suffix= dev&label=IndieGala Helper')
t.create('XML from url | with prefix & suffix & label')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=//version&prefix=v&suffix= dev&label=IndieGala Helper')
.expectJSONTypes(Joi.object().keys({
name: 'IndieGala Helper',
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?\sdev$/)
}));

t.create('XML from uri | query doesnt exist')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/does/not/exist&style=_shields_test')
t.create('XML from url | query doesnt exist')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/does/not/exist&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'no result', colorB: colorsB.lightgrey });

t.create('XML from uri | query doesnt exist (attribute)')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/does/not/@exist&style=_shields_test')
t.create('XML from url | query doesnt exist (attribute)')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/does/not/@exist&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'no result', colorB: colorsB.lightgrey });

t.create('XML from uri | invalid uri')
.get('.json?uri=https://github.com/badges/shields/raw/master/notafile.xml&query=//version&style=_shields_test')
t.create('XML from url | invalid url')
.get('.json?url=https://github.com/badges/shields/raw/master/notafile.xml&query=//version&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'resource not found', colorB: colorsB.lightgrey });

t.create('XML from uri | user color overrides default')
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/name&colorB=10ADED&style=_shields_test')
t.create('XML from url | user color overrides default')
.get('.json?url=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=/addon/name&colorB=10ADED&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'IndieGala Helper', colorB: '#10ADED' });

t.create('XML from uri | error color overrides default')
.get('.json?uri=https://github.com/badges/shields/raw/master/notafile.xml&query=//version&style=_shields_test')
t.create('XML from url | error color overrides default')
.get('.json?url=https://github.com/badges/shields/raw/master/notafile.xml&query=//version&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'resource not found', colorB: colorsB.lightgrey });

t.create('XML from uri | error color overrides user specified')
t.create('XML from url | error color overrides user specified')
.get('.json?query=//version&colorB=10ADED&style=_shields_test')
.expectJSON({ name: 'custom badge', value: 'no uri specified', colorB: colorsB.red });
.expectJSON({ name: 'custom badge', value: 'no url specified', colorB: colorsB.red });

0 comments on commit 094da78

Please sign in to comment.