Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Debaking parses endpoint.

  • Loading branch information...
commit 5390c342247faf0ad194c5540cde042d4d33f202 1 parent 46c0b21
@brianloveswords brianloveswords authored
Showing with 76 additions and 3 deletions.
  1. +53 −1 lib/bakery.js
  2. +23 −2 test/bakery.test.js
View
54 lib/bakery.js
@@ -9,6 +9,7 @@
var util = require('util');
var streampng = require('streampng');
var request = require('request');
+var urlutil = require('url');
var KEYWORD = 'openbadges';
@@ -70,13 +71,64 @@ exports.debake = function debake(image, callback) {
exports.getBakedData(image, function (err, url) {
if (err)
return callback(err);
+
request(url, function (err, response, body) {
if (err)
return callback(err);
- callback(null, body);
+
+ var status = response.statusCode;
+ var type = response.headers['content-type']
+
+ if (status == 200 && type == 'application/json')
+ return exports.parseResponse(response, body, callback);
+
+ if (status != 200) {
+ var err = (errors[status] || errors.generic)(response);
+ return callback(err);
+ }
});
});
};
+exports.parseResponse = function parseResponse(response, body, callback) {
+ if (typeof body !== 'string')
+ return callback(null, body);
+
+ var obj;
+ try {
+ obj = JSON.parse(body);
+ }
+ catch (err) {
+ var original = err;
+ var err = new Error('Could not parse JSON at endpoint');
+ err.code = 'JSON_PARSE_ERROR';
+ err.url = urlutil.format(response.request.uri);
+ err.original = original;
+ return callback(err)
+ }
+ return callback(null, obj);
+};
+
+var errors = {
+ generic: function (response) {
+ var status = response.statusCode;
+ var msg = util.format('There was a problem retrieving the remote resource (%s)', status);
+ var err = new Error(msg);
+ err.code = 'RESOURCE_ERROR';
+ err.url = urlutil.format(response.request.uri);
+ err.httpStatusCode = status;
+ return err;
+ },
+
+ 404: function (response) {
+ var err = new Error('Could not get resource (404)');
+ err.code = 'RESOURCE_NOT_FOUND';
+ err.url = urlutil.format(response.request.uri);
+ err.httpStatusCode = 404;
+ return err;
+ }
+}
+function error404() { }
+
exports.createChunk = createChunk;
exports.KEYWORD = KEYWORD;
View
25 test/bakery.test.js
@@ -92,12 +92,33 @@ test('bakery.bake: do not bake something twice', function (t) {
});
test('bakery.debake: should work', function (t) {
- var opts = { body: '{"band": "grapeful dread"}'};
+ var expect = {band: 'grapeful dread'};
+ var opts = {
+ body: JSON.stringify(expect),
+ type: 'application/json'
+ };
broil(opts, function (baked) {
bakery.debake(baked, function (err, contents) {
t.notOk(err, 'should not have an error');
- t.same(contents, opts.body);
+ t.same(contents, expect);
+ t.end();
+ });
+ });
+});
+
+test('bakery.debake: 404 should return error', function (t) {
+ var opts = { body: 'x', statusCode: 404 };
+ broil(opts, function (baked) {
+ bakery.debake(baked, function (err, contents) {
+ t.ok(err, 'should have an error');
+ t.same(err.code, 'RESOURCE_NOT_FOUND', 'should have a resource not found error');
+ t.same(err.httpStatusCode, 404, 'should have a 404');
t.end();
});
});
});
+
+test('bakery.debake:', function (t) {
+
+ t.end();
+});
Please sign in to comment.
Something went wrong with that request. Please try again.