Jdlrobson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/366947 )

Change subject: Define response behaviour for a variety of endpoints
......................................................................

Define response behaviour for a variety of endpoints

Per the spec [1]

Define responses for:
* For a page outside of the wiki's content namespaces
* For a page in the wiki's content namespace(s)
* For a page that doesn't use the wikitext, wikibase-item, or wikibase-property 
content model
* For a page that doesn't exist
* For a page that redirects to another page

[1] 
https://www.mediawiki.org/wiki/User:Phuedx_(WMF)/Reading/Web/Page_Preview_API

Change-Id: I9264e1b55c1742c3aa5a021ef1154007af83b76f
---
M routes/mobile-sections.js
A test/features/mobile-sections-lead/previewHtml.js
2 files changed, 80 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps 
refs/changes/47/366947/1

diff --git a/routes/mobile-sections.js b/routes/mobile-sections.js
index 8fee414..a8b9ae1 100644
--- a/routes/mobile-sections.js
+++ b/routes/mobile-sections.js
@@ -371,15 +371,26 @@
  */
 function buildSummary(req) {
     return buildLeadObject(req, false).then((lead) => {
-        const intro = lead && lead.intro;
-        if (intro) {
-            return {
-                html: transforms.summarize(intro),
-                revision: lead.revision
-            };
-        } else {
+        let summary = '';
+        let code = 200;
+        let title = lead.displaytitle;
+
+        if (!lead) {
             return false;
+        } else if (lead.redirected) {
+            code = 302;
+            title = lead.redirected;
+        } else if (lead.contentmodel !== 'wikitext' || lead.ns !== 0) {
+            code = 204;
+        } else if (lead.intro) {
+            summary = transforms.summarize(lead.intro);
         }
+        return {
+            code,
+            title,
+            html: summary,
+            revision: lead.revision
+        };
     });
 }
 
@@ -436,9 +447,14 @@
 router.get('/preview-html/:title', (req, res) => {
     return buildSummary(req).then((summary) => {
         if (summary) {
-            res.status(200);
-            mUtil.setETag(res, summary.revision);
-            res.send(summary.html).end();
+            res.status(summary.code);
+            if (summary.code === 200) {
+                mUtil.setETag(res, summary.revision);
+                res.send(summary.html);
+            } else if (summary.code === 302) {
+                res.header('Location', 
`${req.protocol}://${req.headers.host}${req.baseUrl}/preview-html/${summary.title}`);
+            }
+            res.end();
         } else {
             res.status(404);
         }
diff --git a/test/features/mobile-sections-lead/previewHtml.js 
b/test/features/mobile-sections-lead/previewHtml.js
new file mode 100644
index 0000000..a4ddff4
--- /dev/null
+++ b/test/features/mobile-sections-lead/previewHtml.js
@@ -0,0 +1,54 @@
+'use strict';
+
+const assert = require('../../utils/assert.js');
+const preq   = require('preq');
+const server = require('../../utils/server.js');
+
+describe('preview-html', function() {
+
+    this.timeout(20000); // eslint-disable-line no-invalid-this
+
+    before(() => { return server.start(); });
+
+    it('200 For a page that does exist', () => {
+        const uri = 
`${server.config.uri}en.wikipedia.org/v1/page/preview-html/Barack Obama`;
+        return preq.get({ uri })
+            .then((res) => {
+                assert.ok(res.status === 200);
+            });
+    });
+
+    it('404 For a page that doesn\'t exist', () => {
+        const uri = 
`${server.config.uri}en.wikipedia.org/v1/page/preview-html/ashsahahash`;
+        return preq.get({ uri })
+            .catch((res) => {
+                assert.ok(res.status === 404, 'Pages that do not exist 404');
+            });
+    });
+
+    it('204 for pages that are not wikitext', () => {
+        const title = 'Schema:RelatedArticles';
+        const uri = 
`${server.config.uri}meta.wikimedia.org/v1/page/preview-html/${title}`;
+        return preq.get({ uri })
+            .then((res) => {
+                assert.ok(res.status === 204, 'If not wikitext we send 204');
+            });
+    });
+
+    it('204 for pages outside content namespace', () => {
+        const title = 'Talk:Barack Obama';
+        const uri = 
`${server.config.uri}en.wikipedia.org/v1/page/preview-html/${title}`;
+        return preq.get({ uri })
+            .then((res) => {
+                assert.ok(res.status === 204, 'If not in main namespace we 
send 204');
+            });
+    });
+
+    it('302 for pages that are redirects', () => {
+        const uri = 
`${server.config.uri}en.wikipedia.org/v1/page/preview-html/Barack`;
+        return preq.get({ uri })
+            .then((res) => {
+                assert.ok(res.status === 302, 'Redirect pages respond with 
302');
+            });
+    });
+});

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9264e1b55c1742c3aa5a021ef1154007af83b76f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org>

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

Reply via email to