Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Case insensitive conten-type for multipart
  • Loading branch information
Adam Kliment committed Feb 16, 2015
1 parent 65a2ff6 commit 106edc6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/transaction-runner.coffee
Expand Up @@ -113,11 +113,11 @@ class TransactionRunner
# Add length of body if no Content-Length present
# Doing here instead of in configureTransaction, because request body can be edited in before hook

caseInsensitiveMap = {}
caseInsensitiveRequestHeadersMap = {}
for key, value of transaction.request.headers
caseInsensitiveMap[key.toLowerCase()] = key
caseInsensitiveRequestHeadersMap[key.toLowerCase()] = key

if not caseInsensitiveMap['content-length'] and transaction.request['body'] != ''
if not caseInsensitiveRequestHeadersMap['content-length'] and transaction.request['body'] != ''
transaction.request.headers['Content-Length'] = Buffer.byteLength(transaction.request['body'], 'utf8')

requestOptions =
Expand Down Expand Up @@ -221,7 +221,10 @@ class TransactionRunner
return callback()

isMultipart: (requestOptions) =>
requestOptions['headers']?['Content-Type']?.indexOf("multipart") > -1
caseInsensitiveRequestHeaders = {}
for key, value of requestOptions.headers
caseInsensitiveRequestHeaders[key.toLowerCase()] = value
caseInsensitiveRequestHeaders['content-type']?.indexOf("multipart") > -1

replaceLineFeedInBody: (transaction, requestOptions) =>
transaction.request['body'] = transaction.request['body'].replace(/\n/g, '\r\n')
Expand Down
22 changes: 22 additions & 0 deletions test/unit/transaction-runner-test.coffee
Expand Up @@ -483,6 +483,28 @@ describe 'TransactionRunner', ()->
assert.include multiPartTransaction['request']['body'], "\r\n"
done()

describe 'when multipart header in request is with lowercase key', () ->

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'

delete multiPartTransaction['request']['headers']['Content-Type']
multiPartTransaction['request']['headers']['content-type'] = 'multipart/form-data; boundary=BOUNDARY'

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'
assert.include multiPartTransaction['request']['body'], "\r\n"
done()

describe 'when multipart header is not in request', () ->
beforeEach () ->
server = nock('http://localhost:3000').
Expand Down

0 comments on commit 106edc6

Please sign in to comment.