service tests for [libscore], improve error handling (#1706)

This commit is contained in:
chris48s
2018-05-29 09:41:42 +01:00
committed by GitHub
parent 8ba33d419f
commit de4e527f57
2 changed files with 50 additions and 0 deletions

View File

@@ -1254,6 +1254,15 @@ cache(function(data, match, sendBadge, request) {
}
try {
var data = JSON.parse(buffer);
if (data.count.length === 0) {
/* Note the 'not found' response from libscore is:
status code = 200,
body = {"github":"","meta":{},"count":[],"sites":[]}
*/
badgeData.text[1] = 'not found';
sendBadge(format, badgeData);
return;
}
badgeData.text[1] = metric(+data.count[data.count.length-1]);
badgeData.colorscheme = 'blue';
sendBadge(format, badgeData);

View File

@@ -0,0 +1,41 @@
'use strict';
const Joi = require('joi');
const ServiceTester = require('../service-tester');
const { isMetric } = require('../test-validators');
const { invalidJSON } = require('../response-fixtures');
const t = new ServiceTester({ id: 'libscore', title: 'libscore' });
module.exports = t;
t.create('libscore (valid)')
.get('/s/jQuery.json')
.expectJSONTypes(Joi.object().keys({
name: 'libscore',
value: isMetric,
}));
t.create('libscore (not found)')
.get('/s/not-a-library.json')
.expectJSON({name: 'libscore', value: 'not found'});
t.create('libscore (connection error)')
.get('/s/jQuery.json')
.networkOff()
.expectJSON({name: 'libscore', value: 'inaccessible'});
t.create('libscore (unexpected response)')
.get('/s/jQuery.json')
.intercept(nock => nock('http://api.libscore.com')
.get('/v1/libraries/jQuery')
.reply(invalidJSON)
)
.expectJSON({name: 'libscore', value: 'invalid'});
t.create('libscore (error response)')
.get('/s/jQuery.json')
.intercept(nock => nock('http://api.libscore.com')
.get('/v1/libraries/jQuery')
.reply(500, '{"error":"oh noes!!"}')
)
.expectJSON({name: 'libscore', value: 'invalid'});