Skip to content

Commit

Permalink
Add fixes from apiaryio#45 and apiaryio#49 back to transaction-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Cordell committed Mar 6, 2014
1 parent c5216af commit 389e6e8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
28 changes: 22 additions & 6 deletions src/transaction-runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'] + \
Expand All @@ -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
Expand All @@ -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
Expand All @@ -86,6 +97,7 @@ class TransactionRunner
request: request
expected: expected
origin: origin
fullPath: fullPath

return callback(null, configuredTransaction)

Expand All @@ -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

Expand Down Expand Up @@ -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) ->
Expand Down
8 changes: 5 additions & 3 deletions test/integration/cli-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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}"
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions test/unit/add-hooks-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ describe 'addHooks(runner)', () ->
resourceName: 'Machine'
actionName: 'Delete Message'
exampleName: 'Bogus example name'
fullPath: '/machines'

beforeEach () ->
server = nock('http://localhost:3000').
Expand Down
1 change: 1 addition & 0 deletions test/unit/transaction-runner-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ describe 'TransactionRunner', ()->
resourceName: 'Machine'
actionName: 'Delete Message'
exampleName: 'Bogus example name'
fullPath: '/machines'


describe 'when printing the names', () ->
Expand Down

0 comments on commit 389e6e8

Please sign in to comment.