From 389e6e8df67191037f9e73c0414aa2023be713f5 Mon Sep 17 00:00:00 2001 From: Evan Cordell Date: Wed, 5 Mar 2014 17:55:06 -0600 Subject: [PATCH] Add fixes from #45 and #49 back to transaction-runner --- src/transaction-runner.coffee | 28 +++++++++++++++++++----- test/integration/cli-test.coffee | 8 ++++--- test/unit/add-hooks-test.coffee | 1 + test/unit/transaction-runner-test.coffee | 1 + 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/transaction-runner.coffee b/src/transaction-runner.coffee index a62f5eaa8..e51333538 100644 --- a/src/transaction-runner.coffee +++ b/src/transaction-runner.coffee @@ -2,6 +2,7 @@ http = require 'http' https = require 'https' html = require 'html' url = require 'url' +path = require 'path' os = require 'os' gavel = require 'gavel' @@ -40,10 +41,18 @@ class TransactionRunner parsedUrl = url.parse configuration['server'] + # joins paths regardless of slashes + # there may be a nice way in the future: https://github.com/joyent/node/issues/2216 + # note that path.join will fail on windows, and url.resolve can have undesirable behavior depending on slashes + if parsedUrl['path'] is "/" + fullPath = request['uri'] + else + fullPath = '/' + [parsedUrl['path'].replace(/^\/|\/$/g,""), request['uri'].replace(/^\/|\/$/g,"")].join("/") + flatHeaders = flattenHeaders request['headers'] # Add Dredd user agent if no User-Agent present - if flatHeaders['User-Agent'] == undefined + if not flatHeaders['User-Agent'] system = os.type() + ' ' + os.release() + '; ' + os.arch() flatHeaders['User-Agent'] = "Dredd/" + \ packageConfig['version'] + \ @@ -54,7 +63,7 @@ class TransactionRunner for key, value of flatHeaders caseInsensitiveMap[key.toLowerCase()] = key - if caseInsensitiveMap['content-length'] == undefined and request['body'] != '' + if not caseInsensitiveMap['content-length'] and request['body'] != '' flatHeaders['Content-Length'] = request['body'].length if configuration.options.header.length > 0 @@ -72,11 +81,13 @@ class TransactionRunner id = request['method'] + ' ' + request['uri'] + # The data models as used here must conform to Gavel.js + # as defined in `http-response.coffee` expected = headers: flattenHeaders response['headers'] body: response['body'] - status: response['status'] - expected['schema'] = response['schema'] if response['schema'] + statusCode: response['status'] + expected['bodySchema'] = response['schema'] if response['schema'] configuredTransaction = name: name @@ -86,6 +97,7 @@ class TransactionRunner request: request expected: expected origin: origin + fullPath: fullPath return callback(null, configuredTransaction) @@ -95,7 +107,7 @@ class TransactionRunner requestOptions = host: transaction.host port: transaction.port - path: transaction.request['uri'] + path: transaction.fullPath method: transaction.request['method'] headers: transaction.request.headers @@ -127,10 +139,14 @@ class TransactionRunner configuration.emitter.emit 'test error', error, test if error res.on 'end', () -> + + # The data models as used here must conform to Gavel.js + # as defined in `http-response.coffee` real = + statusCode: res.statusCode headers: res.headers body: buffer - status: res.statusCode + transaction['real'] = real gavel.isValid real, transaction.expected, 'response', (error, isValid) -> diff --git a/test/integration/cli-test.coffee b/test/integration/cli-test.coffee index 39e1bd9ea..c7cd8f6da 100644 --- a/test/integration/cli-test.coffee +++ b/test/integration/cli-test.coffee @@ -73,7 +73,7 @@ describe "Command line interface", () -> describe "when executing the command and the server is responding as specified in the blueprint, endpoint with path", () -> before (done) -> - cmd = "./bin/dredd ./test/fixtures/single-get.apib http://localhost:#{PORT}/v2" + cmd = "./bin/dredd ./test/fixtures/single-get.apib http://localhost:#{PORT}/v2/" app = express() @@ -466,7 +466,7 @@ describe "Command line interface", () -> recievedRequest = {} before (done) -> - cmd = "./bin/dredd ./test/fixtures/single_get.md http://localhost:#{PORT} --hookfiles=./test/fixtures/*_hooks.*" + cmd = "./bin/dredd ./test/fixtures/single-get.apib http://localhost:#{PORT} --hookfiles=./test/fixtures/*_hooks.*" app = express() @@ -488,6 +488,8 @@ describe "Command line interface", () -> it 'should modify the transaction with hooks', () -> assert.equal recievedRequest.headers['header'], '123232323' + describe "tests a blueprint containing an endpoint with schema", () -> + describe "and server is responding in accordance with the schema", () -> before (done) -> cmd = "./bin/dredd ./test/fixtures/schema.apib http://localhost:#{PORT}" @@ -535,4 +537,4 @@ describe "Command line interface", () -> server.on 'close', done it 'exit status should be 1 (failure)', () -> - assert.equal exitStatus, 1 + assert.equal exitStatus, 1 diff --git a/test/unit/add-hooks-test.coffee b/test/unit/add-hooks-test.coffee index bf2ee5706..50a27563a 100644 --- a/test/unit/add-hooks-test.coffee +++ b/test/unit/add-hooks-test.coffee @@ -147,6 +147,7 @@ describe 'addHooks(runner)', () -> resourceName: 'Machine' actionName: 'Delete Message' exampleName: 'Bogus example name' + fullPath: '/machines' beforeEach () -> server = nock('http://localhost:3000'). diff --git a/test/unit/transaction-runner-test.coffee b/test/unit/transaction-runner-test.coffee index cf8cd7820..9466f96b7 100644 --- a/test/unit/transaction-runner-test.coffee +++ b/test/unit/transaction-runner-test.coffee @@ -148,6 +148,7 @@ describe 'TransactionRunner', ()-> resourceName: 'Machine' actionName: 'Delete Message' exampleName: 'Bogus example name' + fullPath: '/machines' describe 'when printing the names', () ->