Repository: opencomponents/oc
Pull request #852 (2018-03-26 15:56:26)

[DX-412] Empty response setter on context
Depends on https://github.com/opencomponents/base-templates/pull/279 and https://github.com/opencomponents/oc-client-node/pull/73 and https://github.com/opencomponents/oc-client-browser/pull/44


Patch:
diff --git a/.gitignore b/.gitignore
index a9767a5f..d034cdd1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,6 +22,7 @@ results
 npm-debug.log
 node_modules
 package-lock.json
+yarn.lock
 
 src/components/oc.json
 src/components/base-component-handlebars/_package
@@ -31,6 +32,7 @@ test/fixtures/test.tar.gz
 test/fixtures/targz-test
 test/fixtures/components/container-with-multiple-nested/_package
 test/fixtures/components/container-with-nested/_package
+test/fixtures/components/empty/_package
 test/fixtures/components/hello-world/_package
 test/fixtures/components/hello-world-custom-headers/_package
 test/fixtures/components/jade-filters/_package
diff --git a/package.json b/package.json
index 4444e5af..057673c4 100644
--- a/package.json
+++ b/package.json
@@ -10,9 +10,12 @@
     "build": "node tasks/build.js",
     "git-stage-and-push": "node tasks/git-stage-and-push.js",
     "npm-publish": "node tasks/npm-publish.js",
-    "publish-major": "npm run version-major && npm run test-silent && npm run git-stage-and-push && npm run npm-publish",
-    "publish-minor": "npm run version-minor && npm run test-silent && npm run git-stage-and-push && npm run npm-publish",
-    "publish-patch": "npm run version-patch && npm run test-silent && npm run git-stage-and-push && npm run npm-publish",
+    "publish-major":
+      "npm run version-major && npm run test-silent && npm run git-stage-and-push && npm run npm-publish",
+    "publish-minor":
+      "npm run version-minor && npm run test-silent && npm run git-stage-and-push && npm run npm-publish",
+    "publish-patch":
+      "npm run version-patch && npm run test-silent && npm run git-stage-and-push && npm run npm-publish",
     "precommit": "lint-staged",
     "test": "npm run build && node tasks/mochaTest.js",
     "test-silent": "npm run build && node tasks/mochaTest.js --silent",
@@ -21,10 +24,7 @@
     "version-patch": "node tasks/version.js --type=\"patch\""
   },
   "lint-staged": {
-    "*.js": [
-      "prettier-eslint --single-quote --write",
-      "git add"
-    ]
+    "*.js": ["prettier-eslint --single-quote --write", "git add"]
   },
   "engines": {
     "node": ">=6"
@@ -39,11 +39,7 @@
     "url": "https://github.com/opencomponents/oc/issues"
   },
   "homepage": "https://github.com/opencomponents/oc",
-  "keywords": [
-    "open components",
-    "components",
-    "oc"
-  ],
+  "keywords": ["open components", "components", "oc"],
   "devDependencies": {
     "chai": "3.5.0",
     "chalk": "^2.1.0",
@@ -83,6 +79,7 @@
     "nice-cache": "0.0.5",
     "oc-client": "3.0.11",
     "oc-client-browser": "1.3.1",
+    "oc-empty-response-handler": "1.0.0",
     "oc-get-unix-utc-timestamp": "1.0.2",
     "oc-s3-storage-adapter": "1.1.2",
     "oc-storage-adapters-utils": "1.0.2",
diff --git a/src/registry/routes/helpers/get-component.js b/src/registry/routes/helpers/get-component.js
index 9476a3af..7ea727b9 100644
--- a/src/registry/routes/helpers/get-component.js
+++ b/src/registry/routes/helpers/get-component.js
@@ -5,6 +5,7 @@ const Cache = require('nice-cache');
 const Client = require('oc-client');
 const Domain = require('domain');
 const format = require('stringformat');
+const emptyResponseHandler = require('oc-empty-response-handler');
 const vm = require('vm');
 const _ = require('lodash');
 
@@ -363,36 +364,38 @@ module.exports = function(conf, repository) {
         if (!component.oc.files.dataProvider) {
           returnComponent(null, {});
         } else {
-          const cacheKey = `${component.name}/${component.version}/server.js`,
-            cached = cache.get('file-contents', cacheKey),
-            domain = Domain.create(),
-            contextObj = {
-              acceptLanguage: acceptLanguageParser.parse(acceptLanguage),
-              baseUrl: conf.baseUrl,
-              env: conf.env,
-              params,
-              plugins: conf.plugins,
-              renderComponent: nestedRenderer.renderComponent,
-              renderComponents: nestedRenderer.renderComponents,
-              requestHeaders: options.headers,
-              staticPath: repository
-                .getStaticFilePath(component.name, component.version, '')
-                .replace('https:', ''),
-              setHeader: (header, value) => {
-                if (
-                  !(typeof header === 'string' && typeof value === 'string')
-                ) {
-                  throw strings.errors.registry
-                    .COMPONENT_SET_HEADER_PARAMETERS_NOT_VALID;
-                }
+          const cacheKey = `${component.name}/${component.version}/server.js`;
+          const cached = cache.get('file-contents', cacheKey);
+          const domain = Domain.create();
+          const setEmptyResponse = emptyResponseHandler.contextDecorator(
+            returnComponent
+          );
+          const contextObj = {
+            acceptLanguage: acceptLanguageParser.parse(acceptLanguage),
+            baseUrl: conf.baseUrl,
+            env: conf.env,
+            params,
+            plugins: conf.plugins,
+            renderComponent: nestedRenderer.renderComponent,
+            renderComponents: nestedRenderer.renderComponents,
+            requestHeaders: options.headers,
+            setEmptyResponse,
+            staticPath: repository
+              .getStaticFilePath(component.name, component.version, '')
+              .replace('https:', ''),
+            setHeader: (header, value) => {
+              if (!(typeof header === 'string' && typeof value === 'string')) {
+                throw strings.errors.registry
+                  .COMPONENT_SET_HEADER_PARAMETERS_NOT_VALID;
+              }
 
-                if (header && value) {
-                  responseHeaders = responseHeaders || {};
-                  responseHeaders[header.toLowerCase()] = value;
-                }
-              },
-              templates: repository.getTemplatesInfo()
-            };
+              if (header && value) {
+                responseHeaders = responseHeaders || {};
+                responseHeaders[header.toLowerCase()] = value;
+              }
+            },
+            templates: repository.getTemplatesInfo()
+          };
 
           const setCallbackTimeout = () => {
             if (conf.executionTimeout) {


Test patch:
diff --git a/test/acceptance/registry.js b/test/acceptance/registry.js
index 01baca7f..7e7f80a7 100644
--- a/test/acceptance/registry.js
+++ b/test/acceptance/registry.js
@@ -1,6 +1,7 @@
 'use strict';
 
 const expect = require('chai').expect;
+const emptyResponseHandler = require('oc-empty-response-handler');
 const path = require('path');
 const request = require('minimal-request');
 const _ = require('lodash');
@@ -346,6 +347,7 @@ describe('registry', () => {
       expect(result.components).to.eql([
         'http://localhost:3030/container-with-multiple-nested',
         'http://localhost:3030/container-with-nested',
+        'http://localhost:3030/empty',
         'http://localhost:3030/handlebars3-component',
         'http://localhost:3030/hello-world',
         'http://localhost:3030/hello-world-custom-headers',
@@ -642,6 +644,51 @@ describe('registry', () => {
     });
   });
 
+  describe('GET /empty', () => {
+    describe('rendered', () => {
+      before(done => {
+        request(
+          {
+            url: 'http://localhost:3030/empty',
+            json: true
+          },
+          next(done)
+        );
+      });
+
+      it('should respond with the correct href', () => {
+        expect(result.href).to.eql('http://localhost:3030/empty');
+      });
+
+      it('should respond with an empty response', () => {
+        expect(result.html).to.equal('');
+      });
+    });
+
+    describe('unrendered', () => {
+      before(done => {
+        request(
+          {
+            url: 'http://localhost:3030/empty',
+            headers: { Accept: 'application/vnd.oc.unrendered+json' },
+            json: true
+          },
+          next(done)
+        );
+      });
+
+      it('should respond with the correct href', () => {
+        expect(result.href).to.eql('http://localhost:3030/empty');
+      });
+
+      it('should respond with a minimal empty view-model', () => {
+        expect(result.data).to.eql({
+          [emptyResponseHandler.viewModelEmptyKey]: true
+        });
+      });
+    });
+  });
+
   describe('POST /', () => {
     describe('when body is malformed', () => {
       before(done => {
diff --git a/test/fixtures/components/empty/.npmignore b/test/fixtures/components/empty/.npmignore
new file mode 100644
index 00000000..db378d5d
--- /dev/null
+++ b/test/fixtures/components/empty/.npmignore
@@ -0,0 +1,3 @@
+_package
+package.tar.gz
+node_modules
diff --git a/test/fixtures/components/empty/package.json b/test/fixtures/components/empty/package.json
new file mode 100644
index 00000000..aa758874
--- /dev/null
+++ b/test/fixtures/components/empty/package.json
@@ -0,0 +1,17 @@
+{
+  "name": "empty",
+  "description": "",
+  "version": "1.0.0",
+  "repository": "",
+  "oc": {
+    "renderInfo": true,
+    "container": true,
+    "files": {
+      "data": "server.js",
+      "template": {
+        "src": "template.jade",
+        "type": "jade"
+      }
+    }
+  }
+}
diff --git a/test/fixtures/components/empty/server.js b/test/fixtures/components/empty/server.js
new file mode 100644
index 00000000..67321c18
--- /dev/null
+++ b/test/fixtures/components/empty/server.js
@@ -0,0 +1,3 @@
+module.exports.data = (context, callback) => {
+  return context.setEmptyResponse();
+};
diff --git a/test/fixtures/components/empty/template.jade b/test/fixtures/components/empty/template.jade
new file mode 100644
index 00000000..9e4ecdf5
--- /dev/null
+++ b/test/fixtures/components/empty/template.jade
@@ -0,0 +1,1 @@
+div This is some text
diff --git a/test/unit/registry-domain-repository.js b/test/unit/registry-domain-repository.js
index e5224ece..81ad2fd7 100644
--- a/test/unit/registry-domain-repository.js
+++ b/test/unit/registry-domain-repository.js
@@ -415,6 +415,7 @@ describe('registry : domain : repository', () => {
         expect(response.result).to.eql([
           'container-with-multiple-nested',
           'container-with-nested',
+          'empty',
           'handlebars3-component',
           'hello-world',
           'hello-world-custom-headers',