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

Multipart form requests need to use \r\n instead of only \n #74 #118

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"scripts": {
"test": "scripts/test",
"prepublish": "scripts/prepublish"
"prepublish": "scripts/prepublish",
"postinstall": "scripts/build"
},
"repository": {
"type": "git",
Expand Down
14 changes: 13 additions & 1 deletion src/transaction-runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ logger = require './logger'


String::startsWith = (str) ->
return this.slice(0, str.length) is str
return this.slice(0, str.length) is str

class TransactionRunner
constructor: (@configuration) ->
Expand Down Expand Up @@ -192,6 +192,9 @@ class TransactionRunner
return callback()

transport = if transaction.protocol is 'https:' then https else http
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'] != ''
Expand All @@ -200,4 +203,13 @@ class TransactionRunner
configuration.emitter.emit 'test error', error, test if error
return callback()

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

replaceLineFeedInBody: (transaction, requestOptions) =>
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']


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()