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 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: diff --git a/bin/server.js b/bin/server.js index 28f94b4..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, createReqPayload, isTruthy, join, ref; + var Hapi, MAX_PAYLOAD_BYTES, Server, badRequest, cloudmine, createReqPayload, isTruthy, join, ref; Hapi = require('hapi'); @@ -11,6 +11,8 @@ ref = require('./remote_payload'), isTruthy = ref.isTruthy, createReqPayload = ref.create; + cloudmine = require('cloudmine'); + MAX_PAYLOAD_BYTES = 20000000; Server = (function() { @@ -73,6 +75,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 +102,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 +147,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..2ca960c 100644 --- a/lib/server.coffee +++ b/lib/server.coffee @@ -9,6 +9,7 @@ Hapi = require 'hapi' {badRequest} = require 'boom' join = require('path').join {isTruthy: isTruthy, create: createReqPayload} = require './remote_payload' +cloudmine = require 'cloudmine' MAX_PAYLOAD_BYTES = 20000000 @@ -56,6 +57,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 +70,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 +100,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..8f496f1 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ }, "dependencies": { "boom": "2.10.0", + "cloudmine": ">=0.9.16", "hapi": "9.x.x", "lodash": "4.11.1" }, @@ -37,7 +38,7 @@ "blanket-node": "2.0.0", "chai": "3.5.0", "coffee-script": "1.10.0", - "coffeelint": "1.15.0", + "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 b2accf9..d05a959 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,33 @@ 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 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)-> req = @@ -277,7 +319,6 @@ describe 'Server', -> headers: 'Content-Type': 'application/json' 'X-CloudMine-APIKey': 'notagoodwaytogo' - Server.server.inject req, (res)-> res.result.should.deep.equal expectedPayload done() @@ -300,7 +341,36 @@ 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.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)->