From f7ea19aa4acfb1b4ec5db909bdb2bd00f551fce9 Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 31 Dec 2017 13:38:18 +0000 Subject: [PATCH 1/2] add tests for cdnjs service --- server.js | 12 ++++++++++-- service-tests/cdnjs.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 service-tests/cdnjs.js diff --git a/server.js b/server.js index 5dca33bc93cdd..3bfc8557550ac 100644 --- a/server.js +++ b/server.js @@ -1698,12 +1698,20 @@ cache(function(data, match, sendBadge, request) { return; } try { - var version = JSON.parse(buffer).version || 0; + const json = JSON.parse(buffer); + if (Object.keys(json).length === 0) { + /* Note the 'not found' response from cdnjs is: + status code = 200, body = {} */ + badgeData.text[1] = 'not found'; + sendBadge(format, badgeData); + return; + } + const version = json.version || 0; badgeData.text[1] = versionText(version); badgeData.colorscheme = versionColor(version); sendBadge(format, badgeData); } catch(e) { - badgeData.text[1] = 'not found'; + badgeData.text[1] = 'invalid'; sendBadge(format, badgeData); } }); diff --git a/service-tests/cdnjs.js b/service-tests/cdnjs.js new file mode 100644 index 0000000000000..99a0ef837f7f7 --- /dev/null +++ b/service-tests/cdnjs.js @@ -0,0 +1,41 @@ +'use strict'; + +const Joi = require('joi'); +const ServiceTester = require('./runner/service-tester'); +const { isVPlusTripleDottedVersion } = require('./helpers/validators.js'); + +const t = new ServiceTester({ id: 'cdnjs', title: 'CDNJs' }); +module.exports = t; + + +t.create('cdnjs (valid)') + .get('/v/jquery.json') + .expectJSONTypes(Joi.object().keys({ + name: 'cdnjs', + value: isVPlusTripleDottedVersion, + })); + +t.create('cdnjs (not found)') + .get('/v/not-a-library.json') + .expectJSON({name: 'cdnjs', value: 'not found'}); + +t.create('cdnjs (connection error)') + .get('/v/jquery.json') + .networkOff() + .expectJSON({name: 'cdnjs', value: 'inaccessible'}); + +t.create('cdnjs (unexpected response)') + .get('/v/jquery.json') + .intercept(nock => nock('https://api.cdnjs.com') + .get('/libraries/jquery?fields=version') + .reply(200, "{{{{{invalid json}}") + ) + .expectJSON({name: 'cdnjs', value: 'invalid'}); + +t.create('cdnjs (error response)') + .get('/v/jquery.json') + .intercept(nock => nock('https://api.cdnjs.com') + .get('/libraries/jquery?fields=version') + .reply(500, '{"error":"oh noes!!"}') + ) + .expectJSON({name: 'cdnjs', value: 'invalid'}); From 56850ea608c6b0154dd21bf1186ad5b3b4437fce Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 31 Dec 2017 13:39:25 +0000 Subject: [PATCH 2/2] use es6 declarations --- server.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index 3bfc8557550ac..accf7a7d14fa4 100644 --- a/server.js +++ b/server.js @@ -1687,10 +1687,10 @@ cache(function(data, match, sendBadge, request) { // CDNJS version integration camp.route(/^\/cdnjs\/v\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { - var library = encodeURIComponent(match[1]); // eg, "express" or "@user/express" - var format = match[2]; - var apiUrl = 'https://api.cdnjs.com/libraries/' + library + '?fields=version'; - var badgeData = getBadgeData('cdnjs', data); + const library = encodeURIComponent(match[1]); // eg, "express" or "@user/express" + const format = match[2]; + const apiUrl = 'https://api.cdnjs.com/libraries/' + library + '?fields=version'; + const badgeData = getBadgeData('cdnjs', data); request(apiUrl, function(err, res, buffer) { if (err != null) { badgeData.text[1] = 'inaccessible';