Mobrovac has submitted this change and it was merged.

Change subject: Change route to include the format as well
......................................................................


Change route to include the format as well

This patch alters the exposed route to comprise the sought format as
well. The new route is /:format/:title/:revid/:id , where :id is allowed
to contain the trailing file extension as well. If it is provided, it
must match the given format. A few tests checking this as well as the
format itself have been added.

Bug: T97123
Change-Id: I093f1e0a4aad07477f1bfe0cc2b5cd8d7b9efe57
---
M routes/graphoid-v1.js
M test/features/v1/graph.js
2 files changed, 37 insertions(+), 4 deletions(-)

Approvals:
  Mobrovac: Verified; Looks good to me, approved



diff --git a/routes/graphoid-v1.js b/routes/graphoid-v1.js
index 47f595b..cfa1419 100644
--- a/routes/graphoid-v1.js
+++ b/routes/graphoid-v1.js
@@ -149,10 +149,13 @@
     var start = Date.now();
 
     var p = state.request.params,
+        format = p.format,
         domain = p.domain,
         title = p.title,
         revid = p.revid,
-        id = p.id;
+        id_ext = p.id.split('.', 2),
+        id = id_ext[0],
+        ext = id_ext[1];
 
     state.log = p; // log all parameters of the request
 
@@ -163,6 +166,14 @@
         ppprop: 'graph_specs',
         continue: ''
     };
+
+    // check the format / extension
+    if (ext && ext !== format) {
+        throw new Err('info/param-ext', 'req.id');
+    }
+    if (format !== 'png') {
+        throw new Err('info/param-format', 'req.format');
+    }
 
     if (revid) {
         if (!/^[0-9]+$/.test(revid)) {
@@ -331,7 +342,7 @@
 /**
  * Main entry point for graphoid
  */
-router.get('/:title/:revid/:id.png', function(req, res) {
+router.get('/:format/:title/:revid/:id', function(req, res) {
 
     var start = Date.now();
     var state = {request: req, response: res};
diff --git a/test/features/v1/graph.js b/test/features/v1/graph.js
index 150d196..1379049 100644
--- a/test/features/v1/graph.js
+++ b/test/features/v1/graph.js
@@ -19,10 +19,10 @@
     // common URI prefix for v1
     var uri = function(domain, title, revId, graphId) {
         return server.config.uri +
-            ( domain !== null ? domain : 'mediawiki.org' ) + '/v1/' +
+            ( domain !== null ? domain : 'mediawiki.org' ) + '/v1/png/' +
             ( title !== null ? title : 'Extension:Graph%2FDemo' ) + '/' +
             ( revId !== null ? revId : '1508976' ) + '/' +
-            ( graphId !== null ? graphId : 
'597fd63eb884b45edcd7f71a2788bf01ce52ce9b' ) + '.png';
+            ( graphId !== null ? graphId : 
'597fd63eb884b45edcd7f71a2788bf01ce52ce9b' );
     };
 
     it('should get a PNG image from the Extension:Graph/Demo page without 
revision ID', function() {
@@ -100,5 +100,27 @@
         });
     });
 
+    it('format - extension mismatch', function() {
+        return preq.get({
+            uri: uri(null, null, null, null) + '.jpg'
+        }).then(function(res) {
+            throw new Error('Expected an error to be thrown, got status: ' + 
res.status);
+        }, function(err) {
+            assert.deepEqual(err.status, 400);
+            assert.deepEqual(err.body, 'info/param-ext');
+        });
+    });
+
+    it('wrong format', function() {
+        return preq.get({
+            uri: server.config.uri + 'bla/v1/foo/bar/1234/5678'
+        }).then(function(res) {
+            throw new Error('Expected an error to be thrown, got status: ' + 
res.status);
+        }, function(err) {
+            assert.deepEqual(err.status, 400);
+            assert.deepEqual(err.body, 'info/param-format');
+        });
+    });
+
 });
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I093f1e0a4aad07477f1bfe0cc2b5cd8d7b9efe57
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/services/graphoid
Gerrit-Branch: master
Gerrit-Owner: Mobrovac <mobro...@wikimedia.org>
Gerrit-Reviewer: Mobrovac <mobro...@wikimedia.org>
Gerrit-Reviewer: Yurik <yu...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to