Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the request library instead of custom implementation #734

Merged
merged 1 commit into from
Mar 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"optimist": "^0.6.1",
"pitboss-ng": "^0.3.2",
"proxyquire": "^1.7.10",
"request": "^2.74.0",
"request": "^2.79.0",
"spawn-args": "^0.2.0",
"sync-exec": "^0.6.2",
"uuid": "^3.0.0",
Expand Down
86 changes: 37 additions & 49 deletions src/transaction-runner.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
http = require 'http'
https = require 'https'
requestLib = require 'request'
url = require 'url'
path = require 'path'
os = require 'os'
Expand Down Expand Up @@ -438,13 +437,17 @@ class TransactionRunner
@error = @error or error

getRequestOptionsFromTransaction: (transaction) ->
requestOptions =
host: transaction.host
urlObject =
protocol: transaction.protocol
hostname: transaction.host
port: transaction.port
path: transaction.fullPath
method: transaction.request['method']

return {
uri: url.format(urlObject) + transaction.fullPath
method: transaction.request.method
headers: transaction.request.headers
return requestOptions
body: transaction.request.body
}

# Add length of body if no Content-Length present
setContentLength: (transaction) ->
Expand Down Expand Up @@ -524,65 +527,47 @@ class TransactionRunner
# and the response validation is invoked here
performRequestAndValidate: (test, transaction, hooks, callback) ->
requestOptions = @getRequestOptionsFromTransaction(transaction)
buffer = ""

handleRequest = (res) =>
logger.verbose('Handling HTTP response from tested server')

res.on 'data', (chunk) ->
logger.silly('Recieving some data from tested server')
buffer += chunk

res.on 'error', (error) =>
logger.debug('Recieving response from tested server errored:', error)
handleRequest = (err, res, body) =>
if err
logger.debug('Requesting tested server errored:', err)
test.title = transaction.id
test.expected = transaction.expected
test.request = transaction.request
@emitError(error, test)

@emitError(err, test)
return callback()

res.once 'end', =>
logger.debug('Response from tested server was recieved')
logger.verbose('Handling HTTP response from tested server')

# 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
# 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: body

transaction['real'] = real
transaction['real'] = real

logger.verbose('Running \'beforeEachValidation\' hooks')
@runHooksForData hooks?.beforeEachValidationHooks, transaction, false, () =>
return callback(@hookHandlerError) if @hookHandlerError
logger.verbose('Running \'beforeEachValidation\' hooks')
@runHooksForData hooks?.beforeEachValidationHooks, transaction, false, () =>
return callback(@hookHandlerError) if @hookHandlerError

logger.verbose('Running \'beforeValidation\' hooks')
@runHooksForData hooks?.beforeValidationHooks[transaction.name], transaction, false, () =>
return callback(@hookHandlerError) if @hookHandlerError
logger.verbose('Running \'beforeValidation\' hooks')
@runHooksForData hooks?.beforeValidationHooks[transaction.name], transaction, false, () =>
return callback(@hookHandlerError) if @hookHandlerError

@validateTransaction test, transaction, callback
@validateTransaction test, transaction, callback


transport = if transaction.protocol is 'https:' then https else http
if transaction.request['body'] and @isMultipart requestOptions
@replaceLineFeedInBody transaction, requestOptions

logger.verbose("""\
About to perform #{transaction.protocol.slice(0, -1).toUpperCase()} \
request to tested server: #{requestOptions.method} #{requestOptions.uri}
""")
try
logger.verbose('About to perform an HTTP request to tested server')
req = transport.request requestOptions, handleRequest

req.on 'error', (error) =>
logger.debug('Requesting tested server errored:', error)
test.title = transaction.id
test.expected = transaction.expected
test.request = transaction.request
@emitError(error, test)
return callback()

req.write(transaction.request.body) if transaction.request.body
req.end()
@performRequest(requestOptions, handleRequest)
catch error
logger.debug('Requesting tested server errored:', error)
test.title = transaction.id
Expand All @@ -591,6 +576,9 @@ class TransactionRunner
@emitError(error, test)
return callback()

performRequest: (options, callback) ->
requestLib(options, callback)

validateTransaction: (test, transaction, callback) ->
configuration = @configuration

Expand Down
13 changes: 6 additions & 7 deletions test/unit/transaction-runner-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ clone = require 'clone'
htmlStub = require 'html'
loggerStub = require '../../src/logger'
addHooks = require '../../src/add-hooks'
httpStub = require 'http'
httpsStub = require 'https'

Runner = proxyquire '../../src/transaction-runner', {
'html': htmlStub,
'./logger': loggerStub
'http': httpStub
'https': httpsStub
}
CliReporter = require '../../src/reporters/cli-reporter'
Hooks = require '../../src/hooks'
Expand Down Expand Up @@ -441,16 +437,16 @@ describe 'TransactionRunner', ->
beforeEach ->
configuration.options['dry-run'] = true
runner = new Runner(configuration)
sinon.stub httpStub, 'request'
sinon.spy runner, 'performRequest'


afterEach ->
configuration.options['dry-run'] = false
httpStub.request.restore()
runner.performRequest.restore()

it 'should skip the tests', (done) ->
runner.executeTransaction transaction, ->
assert.ok httpStub.request.notCalled
assert.ok runner.performRequest.notCalled
done()

describe 'when only certain methods are allowed by the configuration', ->
Expand Down Expand Up @@ -691,6 +687,7 @@ describe 'TransactionRunner', ->
actionName: 'Delete Message'
exampleName: 'Bogus example name'
fullPath: '/machines' + name
protocol: 'http:'
}

transactions.push transaction
Expand Down Expand Up @@ -1303,6 +1300,7 @@ describe 'TransactionRunner', ->
actionName: 'Delete Message'
exampleName: 'Bogus example name'
fullPath: '/machines'
protocol: 'http:'
}

server = nock('http://localhost:3000').
Expand Down Expand Up @@ -1667,6 +1665,7 @@ describe 'TransactionRunner', ->
actionName: 'Delete Message'
exampleName: 'Bogus example name'
fullPath: '/machines'
protocol: 'http:'

for i in [1, 2]
clonedTransaction = clone transaction
Expand Down