Skip to content

Commit

Permalink
Multipart form requests need to use \r\n instead of only \n #74
Browse files Browse the repository at this point in the history
Changed isMultipart added test
  • Loading branch information
Rikard Qvarforth committed Nov 7, 2014
1 parent 8f400bf commit 3e1f78a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/transaction-runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,24 @@ class TransactionRunner
return callback()

transport = if transaction.protocol is 'https:' then https else http
try

if transaction.request['body'] != ''
@replaceLineFeedInBody transaction, requestOptions
if transaction.request['body'] and @isMultipart requestOptions
@replaceLineFeedInBody transaction, requestOptions

try
req = transport.request requestOptions, handleRequest
req.write transaction.request['body'] if transaction.request['body'] != ''
req.end()
catch error
configuration.emitter.emit 'test error', error, test if error
return callback()

isMultipart: (requestOptions) =>
requestOptions['headers']?['Content-Type']?.indexOf("multipart") > -1

replaceLineFeedInBody: (transaction, requestOptions) =>
logger.debug 'Content-Length before: ' + requestOptions['headers']['Content-Length']
transaction.request['body'] = transaction.request['body'].replace(/\n/g, '\r\n')
transaction.request['headers']['Content-Length'] = transaction.request['body'].length
requestOptions.headers = transaction.request['headers']
logger.debug 'Content-Length after: ' + requestOptions['headers']['Content-Length']


module.exports = TransactionRunner
47 changes: 47 additions & 0 deletions test/unit/transaction-runner-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,50 @@ describe 'TransactionRunner', ()->
runner.executeTransaction transaction, () ->
assert.ok server.isDone()
done()

describe 'executeTransaction(transaction, callback) multipart', () ->

beforeEach () ->
multiPartTransaction =
name: 'Group Machine > Machine > Post Message> Bogus example name'
id: 'POST /machines/message'
host: 'localhost'
port: '3000'
request:
body: '\n--BOUNDARY \ncontent-disposition: form-data; name="mess12"\n\n{"message":"mess1"}\n--BOUNDARY\n\nContent-Disposition: form-data; name="mess2"\n\n{"message":"mess1"}\n--BOUNDARY--'
headers:
'Content-Type': 'multipart/form-data; boundary=BOUNDARY'
'User-Agent': 'Dredd/0.2.1 (Darwin 13.0.0; x64)'
'Content-Length': 180
uri: '/machines/message'
method: 'POST'
expected:
headers:
'content-type': 'text/htm'
body: ''
status: '204'
origin:
resourceGroupName: 'Group Machine'
resourceName: 'Machine'
actionName: 'Post Message'
exampleName: 'Bogus example name'
fullPath: '/machines/message'
protocol: 'http:'

describe 'when multipart body request', () ->

parsedBody = '\r\n--BOUNDARY \r\ncontent-disposition: form-data; name="mess12"\r\n\r\n{"message":"mess1"}\r\n--BOUNDARY\r\n\r\nContent-Disposition: form-data; name="mess2"\r\n\r\n{"message":"mess1"}\r\n--BOUNDARY--'
beforeEach () ->
server = nock('http://localhost:3000').
post('/machines/message').
reply 204
configuration.server = 'http://localhost:3000'

afterEach () ->
nock.cleanAll()

it 'should replace line feed in body', (done) ->
runner.executeTransaction multiPartTransaction, () ->
assert.ok server.isDone()
assert.equal multiPartTransaction['request']['body'], parsedBody, 'Body'
done()

0 comments on commit 3e1f78a

Please sign in to comment.