Permalink
Browse files

Remove first line beak on multipart posts

fixes 'bad content body' exception for Rails 2.3 applications
  • Loading branch information...
1 parent 72bae93 commit c352ff61a6bc23b26789278702cda2dbbb21eb10 @mordaroso mordaroso committed with Marin Usalj Jul 23, 2012
Showing with 8 additions and 4 deletions.
  1. +6 −2 motion/http.rb
  2. +2 −2 spec/motion/http_spec.rb
View
@@ -272,7 +272,9 @@ def append_form_params(body)
list = process_payload_hash(@payload)
list.each do |key, value|
form_data = NSMutableData.new
- s = "\r\n--#{@boundary}\r\n"
+ s = ""
+ s += "\r\n" unless body.length == 0
+ s += "--#{@boundary}\r\n"
s += "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n"
s += value.to_s
form_data.appendData(s.dataUsingEncoding NSUTF8StringEncoding)
@@ -286,7 +288,9 @@ def append_form_params(body)
def append_files(body)
@files.each do |key, value|
file_data = NSMutableData.new
- s = "\r\n--#{@boundary}\r\n"
+ s = ""
+ s += "\r\n" unless body.length == 0
+ s += "--#{@boundary}\r\n"
s += "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{key}\"\r\n"
s += "Content-Type: application/octet-stream\r\n\r\n"
file_data.appendData(s.dataUsingEncoding NSUTF8StringEncoding)
View
@@ -232,7 +232,7 @@ def sample_data
query = BubbleWrap::HTTP::Query.new( 'nil' , method, { payload: payload, files: files } )
uuid = query.instance_variable_get(:@boundary)
real_payload = NSString.alloc.initWithData(query.request.HTTPBody, encoding:NSUTF8StringEncoding)
- real_payload.should.equal "\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\napple\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"model\"\r\n\r\nmacbook\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"twitter\"; filename=\"twitter\"\r\nContent-Type: application/octet-stream\r\n\r\ntwitter:@mneorr\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"site\"; filename=\"site\"\r\nContent-Type: application/octet-stream\r\n\r\nmneorr.com\r\n--#{uuid}--\r\n"
+ real_payload.should.equal "--#{uuid}\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\napple\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"model\"\r\n\r\nmacbook\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"twitter\"; filename=\"twitter\"\r\nContent-Type: application/octet-stream\r\n\r\ntwitter:@mneorr\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"site\"; filename=\"site\"\r\nContent-Type: application/octet-stream\r\n\r\nmneorr.com\r\n--#{uuid}--\r\n"
end
[:get, :head].each do |method|
@@ -264,7 +264,7 @@ def sample_data
query = BubbleWrap::HTTP::Query.new( 'nil', :post, { payload: payload } )
uuid = query.instance_variable_get(:@boundary)
real_payload = NSString.alloc.initWithData(query.request.HTTPBody, encoding:NSUTF8StringEncoding)
- real_payload.should.equal "\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"computer[name]\"\r\n\r\napple\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"computer[model]\"\r\n\r\nmacbook\r\n--#{uuid}--\r\n"
+ real_payload.should.equal "--#{uuid}\r\nContent-Disposition: form-data; name=\"computer[name]\"\r\n\r\napple\r\n--#{uuid}\r\nContent-Disposition: form-data; name=\"computer[model]\"\r\n\r\nmacbook\r\n--#{uuid}--\r\n"
end
end

0 comments on commit c352ff6

Please sign in to comment.