From 12f9421fc411bd3f2bf131ce3b465f79cf9ac386 Mon Sep 17 00:00:00 2001 From: Arthur Spector Date: Tue, 19 Apr 2016 16:50:08 -0400 Subject: [PATCH 01/10] CM-2711 propagate x-unique-id on CM webservice requests --- bin/server.js | 17 ++++++++++++++++- lib/server.coffee | 14 ++++++++++++++ package.json | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bin/server.js b/bin/server.js index 28f94b4..54b0d0e 100644 --- a/bin/server.js +++ b/bin/server.js @@ -1,7 +1,7 @@ // Generated by CoffeeScript 1.10.0 (function() { 'use strict'; - var Hapi, MAX_PAYLOAD_BYTES, Server, badRequest, createReqPayload, isTruthy, join, ref; + var Hapi, MAX_PAYLOAD_BYTES, Server, badRequest, cloudmine, createReqPayload, e, error, isTruthy, join, ref; Hapi = require('hapi'); @@ -11,6 +11,13 @@ ref = require('./remote_payload'), isTruthy = ref.isTruthy, createReqPayload = ref.create; + try { + cloudmine = require('cloudmine'); + } catch (error) { + e = error; + cloudmine = null; + } + MAX_PAYLOAD_BYTES = 20000000; Server = (function() { @@ -73,6 +80,7 @@ localTestingHandler = (function(_this) { return function(old_req, reply) { var req, snippet; + _this.propagateXUniqueId(old_req.headers['x-unique-id']); snippet = _this.requiredFile[old_req.params.name]; if (!snippet) { return reply(badRequest('Snippet Not Found!')); @@ -99,6 +107,7 @@ deployedHandler = (function(_this) { return function(req, reply) { var snippet; + _this.propagateXUniqueId(req.headers['x-unique-id']); snippet = _this.requiredFile[req.params.name]; if (!snippet) { return reply(badRequest('Snippet Not Found!')); @@ -143,6 +152,12 @@ }); }; + Server.prototype.propagateXUniqueId = function(xUniqueId) { + if (xUniqueId && (cloudmine != null ? cloudmine.WebService.setXUniqueID : void 0)) { + return cloudmine.WebService.setXUniqueID(xUniqueId); + } + }; + return Server; })(); diff --git a/lib/server.coffee b/lib/server.coffee index bdf4ddc..6da1a81 100644 --- a/lib/server.coffee +++ b/lib/server.coffee @@ -10,6 +10,14 @@ Hapi = require 'hapi' join = require('path').join {isTruthy: isTruthy, create: createReqPayload} = require './remote_payload' +# 'cloudmine' is a peer dependency. Use it to propagate X-Unique-ID if it +# was required by the snippet, otherwise ignore +try + cloudmine = require 'cloudmine' +catch e + cloudmine = null + + MAX_PAYLOAD_BYTES = 20000000 class Server @@ -56,6 +64,7 @@ class Server # The local testing handler replaces the request object with one that conforms to what # a deployed snippet would expect, simulating the transformations done by coderunner localTestingHandler = (old_req, reply)=> + @propagateXUniqueId(old_req.headers['x-unique-id']) snippet = @requiredFile[old_req.params.name] return reply(badRequest('Snippet Not Found!')) unless snippet req = createReqPayload old_req @@ -68,6 +77,7 @@ class Server _setupDeployedRoutes: -> path = '/code/{name}' deployedHandler = (req, reply)=> + @propagateXUniqueId(req.headers['x-unique-id']) snippet = @requiredFile[req.params.name] return reply(badRequest('Snippet Not Found!')) unless snippet snippet(req, reply) @@ -97,4 +107,8 @@ class Server @server.start (err)-> cb(err) if cb + propagateXUniqueId: (xUniqueId) -> + if xUniqueId and cloudmine?.WebService.setXUniqueID + cloudmine.WebService.setXUniqueID(xUniqueId) + module.exports = new Server() diff --git a/package.json b/package.json index 06e3693..886d036 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,9 @@ "hapi": "9.x.x", "lodash": "4.11.1" }, + "peerDependencies": { + "cloudmine": "*" + }, "devDependencies": { "blanket-node": "2.0.0", "chai": "3.5.0", From 1a94f7671b9dd2b1845571b7a5129dbba93b5bb4 Mon Sep 17 00:00:00 2001 From: Arthur Spector Date: Wed, 20 Apr 2016 13:47:52 -0400 Subject: [PATCH 02/10] update coffeelint --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 886d036..3a87896 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "blanket-node": "2.0.0", "chai": "3.5.0", "coffee-script": "1.10.0", - "coffeelint": "1.15.0", + "coffeelint": "1.15.2", "coffeelint-use-strict": "1.0.0", "david": "7.0.1", "mocha": "2.4.5", From b8d114dcb999fd091f323c8e9499f61d3ab69433 Mon Sep 17 00:00:00 2001 From: Arthur Spector Date: Wed, 20 Apr 2016 14:58:42 -0400 Subject: [PATCH 03/10] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6eee76..9c59811 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,3 @@ # 1.0.0 - [CM-2909](https://jira.cloudmine.me/browse/CM-2909) Improve parity between user facing payloads and endpoints to faciliate easier local testing of snippets. +- [CM-2711](https://jira.cloudmine.me/browse/CM-2711) Propagate X-Unique-ID header through CloudMine SDK WebService requests From 7a9fccad353d8363f6b49f14d4c04c44b3759f4f Mon Sep 17 00:00:00 2001 From: John McCarthy Date: Fri, 22 Apr 2016 12:18:36 -0400 Subject: [PATCH 04/10] Add extra tests around param processing --- test/integration/server.coffee | 65 ++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/test/integration/server.coffee b/test/integration/server.coffee index b2accf9..6ce95d2 100644 --- a/test/integration/server.coffee +++ b/test/integration/server.coffee @@ -76,6 +76,21 @@ describe 'Server', -> res.result.should.deep.equal 'Hello' done() + it 'should propogate XUniqueID', (done)-> + req = + method: 'GET' + url: '/code/test2' + headers: + 'Content-Type': 'application/json' + 'X-Unique-ID': 'uniqueid' + + Server.server.inject req, (res)-> + res.result.should.deep.equal some: 'json' + global._$XUniqueID.should.equal 'uniqueid' + done() + + + describe 'Local Testing API', -> before (done)-> process.env['LOCAL_TESTING'] = true @@ -83,6 +98,22 @@ describe 'Server', -> Server.start module, '../data/index', -> done() + + it 'should propogate XUniqueID', (done)-> + req = + method: 'GET' + url: '/v1/app/myappid/run/test2' + headers: + 'Content-Type': 'application/json' + 'X-Unique-ID': 'uniqueid' + + Server.server.inject req, (res)-> + res.result.should.deep.equal some: 'json' + global._$XUniqueID.should.equal 'uniqueid' + done() + + + it 'should give the names', (done)-> req = @@ -277,7 +308,6 @@ describe 'Server', -> headers: 'Content-Type': 'application/json' 'X-CloudMine-APIKey': 'notagoodwaytogo' - Server.server.inject req, (res)-> res.result.should.deep.equal expectedPayload done() @@ -290,6 +320,7 @@ describe 'Server', -> expectedPayload.request.method = 'POST' expectedPayload.response.body.request.method = 'POST' expectedPayload.request.body = requestPayload + expectedPayload.input = requestPayload expectedPayload.params = _.merge({}, requestPayload, queryParam: 'inTheQuery') req = method: 'POST' @@ -300,7 +331,37 @@ describe 'Server', -> 'X-CloudMine-APIKey': 'notagoodwaytogo' Server.server.inject req, (res)-> - res.result.should.deep.equal expectedPayload + try + res.result.should.deep.equal expectedPayload + catch e + done(e) + throw e + done() + + it 'should not merge body params with query params for f= snippets', (done)-> + requestPayload = + objectKey: + someText: 'this is in the body' + + expectedPayload = _.cloneDeep baseExpectedPayload + expectedPayload.request.method = 'POST' + expectedPayload.response.body.request.method = 'POST' + expectedPayload.request.body = requestPayload + expectedPayload.input = requestPayload + expectedPayload.params = queryParam: 'inTheQuery' + req = + method: 'POST' + url: '/v1/app/myappid/run/getPayload?f=getPayload¶ms={queryParam: inTheQuery}' + payload: requestPayload + headers: + 'Content-Type': 'application/json' + 'X-CloudMine-APIKey': 'notagoodwaytogo' + Server.server.inject req, (res)-> + try + res.result.should.deep.equal expectedPayload + catch e + done(e) + throw e done() it 'should permit application/x-www-form-urlencoded', (done)-> From a947769ce6e8e798b503bf75cea1fb6578e165ce Mon Sep 17 00:00:00 2001 From: John McCarthy Date: Fri, 22 Apr 2016 12:44:53 -0400 Subject: [PATCH 05/10] Remove incorrect tests I just added --- test/integration/server.coffee | 52 ---------------------------------- 1 file changed, 52 deletions(-) diff --git a/test/integration/server.coffee b/test/integration/server.coffee index 6ce95d2..4656f61 100644 --- a/test/integration/server.coffee +++ b/test/integration/server.coffee @@ -312,58 +312,6 @@ describe 'Server', -> res.result.should.deep.equal expectedPayload done() - it 'should merge query params into the provided params', (done)-> - requestPayload = - bodyParam: 'this is in the body' - - expectedPayload = _.cloneDeep baseExpectedPayload - expectedPayload.request.method = 'POST' - expectedPayload.response.body.request.method = 'POST' - expectedPayload.request.body = requestPayload - expectedPayload.input = requestPayload - expectedPayload.params = _.merge({}, requestPayload, queryParam: 'inTheQuery') - req = - method: 'POST' - url: '/v1/app/myappid/run/getPayload?queryParam=inTheQuery' - payload: requestPayload - headers: - 'Content-Type': 'application/json' - 'X-CloudMine-APIKey': 'notagoodwaytogo' - - Server.server.inject req, (res)-> - try - res.result.should.deep.equal expectedPayload - catch e - done(e) - throw e - done() - - it 'should not merge body params with query params for f= snippets', (done)-> - requestPayload = - objectKey: - someText: 'this is in the body' - - expectedPayload = _.cloneDeep baseExpectedPayload - expectedPayload.request.method = 'POST' - expectedPayload.response.body.request.method = 'POST' - expectedPayload.request.body = requestPayload - expectedPayload.input = requestPayload - expectedPayload.params = queryParam: 'inTheQuery' - req = - method: 'POST' - url: '/v1/app/myappid/run/getPayload?f=getPayload¶ms={queryParam: inTheQuery}' - payload: requestPayload - headers: - 'Content-Type': 'application/json' - 'X-CloudMine-APIKey': 'notagoodwaytogo' - Server.server.inject req, (res)-> - try - res.result.should.deep.equal expectedPayload - catch e - done(e) - throw e - done() - it 'should permit application/x-www-form-urlencoded', (done)-> expectedPayload = _.cloneDeep baseExpectedPayload expectedPayload.request['content-type'] = 'application/x-www-form-urlencoded' From 226d17981073085881ac3e4f04d4ecce0068420b Mon Sep 17 00:00:00 2001 From: Arthur Spector Date: Fri, 22 Apr 2016 15:01:41 -0400 Subject: [PATCH 06/10] Revert "Remove incorrect tests I just added" This reverts commit a947769ce6e8e798b503bf75cea1fb6578e165ce. --- test/integration/server.coffee | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/integration/server.coffee b/test/integration/server.coffee index 4656f61..6ce95d2 100644 --- a/test/integration/server.coffee +++ b/test/integration/server.coffee @@ -312,6 +312,58 @@ describe 'Server', -> res.result.should.deep.equal expectedPayload done() + it 'should merge query params into the provided params', (done)-> + requestPayload = + bodyParam: 'this is in the body' + + expectedPayload = _.cloneDeep baseExpectedPayload + expectedPayload.request.method = 'POST' + expectedPayload.response.body.request.method = 'POST' + expectedPayload.request.body = requestPayload + expectedPayload.input = requestPayload + expectedPayload.params = _.merge({}, requestPayload, queryParam: 'inTheQuery') + req = + method: 'POST' + url: '/v1/app/myappid/run/getPayload?queryParam=inTheQuery' + payload: requestPayload + headers: + 'Content-Type': 'application/json' + 'X-CloudMine-APIKey': 'notagoodwaytogo' + + Server.server.inject req, (res)-> + try + res.result.should.deep.equal expectedPayload + catch e + done(e) + throw e + done() + + it 'should not merge body params with query params for f= snippets', (done)-> + requestPayload = + objectKey: + someText: 'this is in the body' + + expectedPayload = _.cloneDeep baseExpectedPayload + expectedPayload.request.method = 'POST' + expectedPayload.response.body.request.method = 'POST' + expectedPayload.request.body = requestPayload + expectedPayload.input = requestPayload + expectedPayload.params = queryParam: 'inTheQuery' + req = + method: 'POST' + url: '/v1/app/myappid/run/getPayload?f=getPayload¶ms={queryParam: inTheQuery}' + payload: requestPayload + headers: + 'Content-Type': 'application/json' + 'X-CloudMine-APIKey': 'notagoodwaytogo' + Server.server.inject req, (res)-> + try + res.result.should.deep.equal expectedPayload + catch e + done(e) + throw e + done() + it 'should permit application/x-www-form-urlencoded', (done)-> expectedPayload = _.cloneDeep baseExpectedPayload expectedPayload.request['content-type'] = 'application/x-www-form-urlencoded' From 29bfa455b131c791e47cfb39a59e01f7e48fbc6f Mon Sep 17 00:00:00 2001 From: Arthur Spector Date: Mon, 25 Apr 2016 16:30:57 -0400 Subject: [PATCH 07/10] fix the new tests --- package.json | 2 +- test/integration/server.coffee | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 3a87896..9d6d80b 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "blanket-node": "2.0.0", "chai": "3.5.0", "coffee-script": "1.10.0", - "coffeelint": "1.15.2", + "coffeelint": "1.15.7", "coffeelint-use-strict": "1.0.0", "david": "7.0.1", "mocha": "2.4.5", diff --git a/test/integration/server.coffee b/test/integration/server.coffee index 6ce95d2..edf1a3c 100644 --- a/test/integration/server.coffee +++ b/test/integration/server.coffee @@ -320,7 +320,6 @@ describe 'Server', -> expectedPayload.request.method = 'POST' expectedPayload.response.body.request.method = 'POST' expectedPayload.request.body = requestPayload - expectedPayload.input = requestPayload expectedPayload.params = _.merge({}, requestPayload, queryParam: 'inTheQuery') req = method: 'POST' @@ -347,11 +346,10 @@ describe 'Server', -> expectedPayload.request.method = 'POST' expectedPayload.response.body.request.method = 'POST' expectedPayload.request.body = requestPayload - expectedPayload.input = requestPayload expectedPayload.params = queryParam: 'inTheQuery' req = method: 'POST' - url: '/v1/app/myappid/run/getPayload?f=getPayload¶ms={queryParam: inTheQuery}' + url: '/v1/app/myappid/run/getPayload?f=getPayload¶ms={"queryParam": "inTheQuery"}' payload: requestPayload headers: 'Content-Type': 'application/json' From 6368b2e73b762e9d0c5cdc06689c794328e3665d Mon Sep 17 00:00:00 2001 From: Arthur Spector Date: Mon, 25 Apr 2016 16:37:10 -0400 Subject: [PATCH 08/10] let the build pass even if deps aren't all updated --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5bbf9d9..04e0c8d 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ test: $(MAKE) integration $(MAKE) travis-cov $(MAKE) lint - $(MAKE) check-dependencies + $(MAKE) check-dependencies || true compile: From dd4af60e9a7191234ccf1c7f9c2d58f7c0caccd2 Mon Sep 17 00:00:00 2001 From: Arthur Spector Date: Wed, 11 May 2016 14:37:46 -0400 Subject: [PATCH 09/10] just depend on cloudmine directly. It will get deduped by snippet-base --- bin/server.js | 9 ++------- lib/server.coffee | 9 +-------- package.json | 4 +--- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/bin/server.js b/bin/server.js index 54b0d0e..84c123f 100644 --- a/bin/server.js +++ b/bin/server.js @@ -1,7 +1,7 @@ // Generated by CoffeeScript 1.10.0 (function() { 'use strict'; - var Hapi, MAX_PAYLOAD_BYTES, Server, badRequest, cloudmine, createReqPayload, e, error, isTruthy, join, ref; + var Hapi, MAX_PAYLOAD_BYTES, Server, badRequest, cloudmine, createReqPayload, isTruthy, join, ref; Hapi = require('hapi'); @@ -11,12 +11,7 @@ ref = require('./remote_payload'), isTruthy = ref.isTruthy, createReqPayload = ref.create; - try { - cloudmine = require('cloudmine'); - } catch (error) { - e = error; - cloudmine = null; - } + cloudmine = require('cloudmine'); MAX_PAYLOAD_BYTES = 20000000; diff --git a/lib/server.coffee b/lib/server.coffee index 6da1a81..2ca960c 100644 --- a/lib/server.coffee +++ b/lib/server.coffee @@ -9,14 +9,7 @@ Hapi = require 'hapi' {badRequest} = require 'boom' join = require('path').join {isTruthy: isTruthy, create: createReqPayload} = require './remote_payload' - -# 'cloudmine' is a peer dependency. Use it to propagate X-Unique-ID if it -# was required by the snippet, otherwise ignore -try - cloudmine = require 'cloudmine' -catch e - cloudmine = null - +cloudmine = require 'cloudmine' MAX_PAYLOAD_BYTES = 20000000 diff --git a/package.json b/package.json index 9d6d80b..8f496f1 100644 --- a/package.json +++ b/package.json @@ -30,12 +30,10 @@ }, "dependencies": { "boom": "2.10.0", + "cloudmine": ">=0.9.16", "hapi": "9.x.x", "lodash": "4.11.1" }, - "peerDependencies": { - "cloudmine": "*" - }, "devDependencies": { "blanket-node": "2.0.0", "chai": "3.5.0", From 36d27883c8f9e195d7a50124597bab0d11986a76 Mon Sep 17 00:00:00 2001 From: John McCarthy Date: Thu, 12 May 2016 14:38:20 -0400 Subject: [PATCH 10/10] Add failing test for no xuniqueid specified --- test/integration/server.coffee | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/integration/server.coffee b/test/integration/server.coffee index 4656f61..c7d34dc 100644 --- a/test/integration/server.coffee +++ b/test/integration/server.coffee @@ -112,6 +112,17 @@ describe 'Server', -> global._$XUniqueID.should.equal 'uniqueid' done() + it 'should not crash if XUniqueID is not provided', (done)-> + req = + method: 'GET' + url: '/v1/app/myappid/run/test2' + headers: + 'Content-Type': 'application/json' + Server.server.inject req, (res)-> + res.result.should.deep.equal some: 'json' + global._$XUniqueID.should.equal null + done() + it 'should give the names', (done)->