Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Support for form data
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgreen committed Jul 4, 2013
1 parent 31cd02a commit 86f6423
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
19 changes: 13 additions & 6 deletions lib/elevate/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ def initialize(method, url, options={})
raise ArgumentError, "invalid URL" unless url.start_with? "http"
raise ArgumentError, "invalid body type; must be NSData" if options[:body] && ! options[:body].is_a?(NSData)

unless options.fetch(:query, {}).empty?
url += "?" + URI.encode_query(options[:query])
end

options[:headers] ||= {}

if root = options.delete(:json)
options[:body] = NSJSONSerialization.dataWithJSONObject(root, options: 0, error: nil)

options[:headers] ||= {}
options[:headers]["Content-Type"] = "application/json"
end

unless options.fetch(:query, {}).empty?
url += "?" + URI.encode_query(options[:query])
elsif root = options.delete(:form)
options[:body] = URI.encode_www_form(root).dataUsingEncoding(NSASCIIStringEncoding)
options[:headers]["Content-Type"] ||= "application/x-www-form-urlencoded"
end

@request = NSMutableURLRequest.alloc.init
Expand Down Expand Up @@ -109,6 +112,10 @@ def connection(connection, willSendRequest: request, redirectResponse: response)
def get_authorization_header(credentials)
"Basic " + Base64.encode("#{credentials[:username]}:#{credentials[:password]}")
end

def get_body

end
end
end
end
29 changes: 29 additions & 0 deletions lib/elevate/http/uri.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
module Elevate
module HTTP
module URI
def self.encode_www_form(enum)
enum.map do |k,v|
if v.nil?
encode_www_form_component(k)
elsif v.respond_to?(:to_ary)
v.to_ary.map do |w|
str = encode_www_form_component(k)

if w.nil?
str
else
str + "=" + encode_www_form_component(w)
end
end.join('&')
else
encode_www_form_component(k) + "=" + encode_www_form_component(v)
end
end.join('&')
end

def self.encode_www_form_component(str)
# From AFNetworking :)
CFURLCreateStringByAddingPercentEscapes(nil,
str,
"[].",
":/?&=;+!@\#$()~',*",
CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding))
end

def self.encode_query(hash)
return "" if hash.nil? || hash.empty?

Expand Down
24 changes: 22 additions & 2 deletions spec/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,34 @@
stub.should.be.requested
end

it "sets the Content-Type automatically" do
stub = stub_request(:post, @url).with(body: '{"test":"secret"}', headers: { "Content-Type" => "application/json" })
it "sets the correct Content-Type" do
stub = stub_request(:post, @url).
with(body: '{"test":"secret"}', headers: { "Content-Type" => "application/json" })

Elevate::HTTP.post(@url, json: { "test" => "secret" })

stub.should.be.requested
end
end

describe "with a form body" do
it "encodes form data specified by :form" do
stub = stub_request(:post, @url).with(body: { "test" => "secret", "user" => "matt" })

Elevate::HTTP.post(@url, form: { "test" => "secret", "user" => "matt" })

stub.should.be.requested
end

it "sets the correct Content-Type" do
stub = stub_request(:post, @url).
with(body: { "test" => "secret", "user" => "matt" }, headers: { "Content-Type" => "application/x-www-form-urlencoded" })

Elevate::HTTP.post(@url, form: { "test" => "secret", "user" => "matt" })

stub.should.be.requested
end
end
end

describe "Response" do
Expand Down

0 comments on commit 86f6423

Please sign in to comment.