Mvolz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/199921

Change subject: [WIP] Restructure requestFromDOI + tests
......................................................................

[WIP] Restructure requestFromDOI + tests

requestFromDOI:

* Follow exactly one direct first before
sending to requestFromURL.

* Return 404s with error message instead
of 520s with citation.

tests:

* Add test for unresolvable DOIs confirming
they return 404s and an error message

Bug: T93876
Change-Id: I2756d11c30c41f88656b9d31ccd7d3e16553c7e2
---
M lib/CitoidService.js
M package.json
M test/features/errors/index.js
M test/utils/assert.js
4 files changed, 47 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/citoid 
refs/changes/21/199921/1

diff --git a/lib/CitoidService.js b/lib/CitoidService.js
index 6fb0875..843f5c0 100644
--- a/lib/CitoidService.js
+++ b/lib/CitoidService.js
@@ -5,7 +5,8 @@
  */
 
 /* Import Modules */
-var urlParse = require('url');
+var http = require('http'),
+       urlParse = require('url');
 
 /* Import Local Modules */
 var unshorten = require('./unshorten.js'),
@@ -16,8 +17,8 @@
 /**
  * Constructor for CitoidService object
  * @param {Object} citoidConfig configuration object
- * @param {Object} logger       logger object, must have a log() method
- * @param {Object} statsd       metrics object
+ * @param {Object} logger         logger object, must have a log() method
+ * @param {Object} statsd         metrics object
  */
 function CitoidService(citoidConfig, logger, statsd) {
        this.logger = logger;
@@ -28,7 +29,7 @@
 
 /**
  * Requests to the citoid service
- * @param  {Object}   opts     options object containing request information
+ * @param  {Object}   opts      options object containing request information
  * @param  {Function} callback callback (error, statusCode, body)
  */
 CitoidService.prototype.request = function(opts, callback){
@@ -48,8 +49,8 @@
 
 /**
  * Request citation metadata from a URL
- * @param  {Object}   opts         options object containing requested url
- * @param  {Function} callback     callback (error, statusCode, body)
+ * @param  {Object}   opts              options object containing requested url
+ * @param  {Function} callback  callback (error, statusCode, body)
  */
 CitoidService.prototype.requestFromURL = function (opts, callback) {
        var self = this,
@@ -114,19 +115,36 @@
 
 /**
  * Request citation metadata from a DOI
- * @param  {Object}   opts         options object containing DOI and format
- * @param  {Function} callback     callback (error, statusCode, body)
+ * @param  {Object}   opts              options object containing DOI and 
format
+ * @param  {Function} callback  callback (error, statusCode, body)
  */
 CitoidService.prototype.requestFromDOI = function (opts, callback){
-       var doiLink = 'http://dx.doi.org/'+ opts.search;
-       opts.search = doiLink;
-       // TODO: optimise this (can skip some steps in requestFromURL)
-       this.requestFromURL(opts, callback);
+       var canonLink, doiLink, error, message,
+               citoidService = this,
+               redirectOpts = {followRedirect : false},
+               zoteroOpts = opts;
+
+       // Set correct url in zotero and redirect options objects
+       zoteroOpts.search = doiLink = 'http://dx.doi.org/'+ opts.search;
+
+       // Follow one redirect here from the DOI to the canonical url
+       http.get(doiLink, function (res) {
+               // Detect a redirect
+               if (res && res.statusCode > 300 && res.statusCode < 400 && 
res.headers.location) {
+                       zoteroOpts.search = res.headers.location;
+                       citoidService.requestFromURL(zoteroOpts, callback);
+               } else {
+                       citoidService.logger.log('debug/DOI', "Unable to 
resolve DOI " + opts.search);
+                       message = 'Unable to resolve DOI';
+                       error = new Error(message);
+                       callback(error, 404, {Error: message});
+               }
+       });
 };
 
 /**
  * Request citation metadata from a PubMed identifier. Supports PMID, PMCID, 
Manuscript ID and versioned identifiers
- * @param  {Object}   opts       options object containing PubMed identifier. 
PMCID identifiers must begin with 'PMC'
+ * @param  {Object}   opts        options object containing PubMed identifier. 
PMCID identifiers must begin with 'PMC'
  * @param  {Function} callback   callback (error, statusCode, body)
  */
 CitoidService.prototype.requestFromPubMedID = function(opts, callback){
@@ -146,7 +164,7 @@
 /**
  * Determine type of string (doi, url) and callback on correct handler
  * @param  {String}   rawSearchInput   what the end user searched for
- * @param  {Function} callback                 callback(extractedValue, 
correctFunction)
+ * @param  {Function} callback                 callback(extractedValue, 
correctFunction)
  */
 CitoidService.prototype.distinguish = function(rawSearchInput, callback){
        var reDOI, rePMID, rePMCID, rePMCID2, reHTTP, reWWW,
diff --git a/package.json b/package.json
index 3477cf1..51bb5b0 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "citoid",
-  "version": "0.2.1",
+  "version": "0.2.2",
   "description": "Converts search terms such as URL or DOI into citations.",
   "scripts": {
     "start": "service-runner",
diff --git a/test/features/errors/index.js b/test/features/errors/index.js
index a2d1792..19450b0 100644
--- a/test/features/errors/index.js
+++ b/test/features/errors/index.js
@@ -12,7 +12,7 @@
 
        before(function () { return server.start(); });
 
-       it('missing format', function() {
+       it('missing format in query', function() {
                return preq.get({
                        uri: server.config.q_uri,
                        query: {
@@ -25,7 +25,7 @@
                });
        });
 
-       it('missing search', function() {
+       it('missing search in query', function() {
                return preq.get({
                        uri: server.config.q_uri,
                        query: {
@@ -38,7 +38,7 @@
                });
        });
 
-       it('erroneous domain', function() {
+       it('bad domain', function() {
                return server.query('example./com', 'mediawiki', 'en')
                .then(function(res) {
                        assert.status(res, 520);
@@ -48,7 +48,7 @@
                });
        });
 
-       it('non-existent URL path', function() {
+       it('link response has http errors', function() {
                var url = 'http://example.com/thisurldoesntexist';
                return server.query(url, 'mediawiki', 'en')
                .then(function(res) {
@@ -59,5 +59,15 @@
                });
        });
 
+       it('bad doi', function() {
+               var doi = '10.1000/thisdoidoesntexist';
+               return server.query(doi, 'mediawiki', 'en').then(function(res) {
+                       assert.status(res, 404);
+               }, function(err) {
+                       assert.status(err, 404);
+               });
+       });
+
+
 });
 
diff --git a/test/utils/assert.js b/test/utils/assert.js
index d65def4..d694915 100644
--- a/test/utils/assert.js
+++ b/test/utils/assert.js
@@ -122,7 +122,6 @@
 
 }
 
-
 module.exports.ok             = assert.ok;
 module.exports.fails          = fails;
 module.exports.deepEqual      = deepEqual;

-- 
To view, visit https://gerrit.wikimedia.org/r/199921
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2756d11c30c41f88656b9d31ccd7d3e16553c7e2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/citoid
Gerrit-Branch: master
Gerrit-Owner: Mvolz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to