spec/motion/http_spec.rb in bubble-wrap-1.1.0 vs spec/motion/http_spec.rb in bubble-wrap-1.1.1
- old
+ new
@@ -20,25 +20,25 @@
query.response.should.be.same_as @the_response
called.should.equal true
end
it ".get .post .put .delete .head .patch should properly generate the HTTP::Query" do
- [:get, :post, :put, :delete, :head, :patch].each do |method|
+ [:get, :post, :put, :delete, :head, :patch].each do |method|
test_http_method method
end
end
it "uses the block instead of action passed in " do
- [:get, :post, :put, :delete, :head, :patch].each do |method|
+ [:get, :post, :put, :delete, :head, :patch].each do |method|
called = false
expected_delegator = Proc.new {|response| called = true }
query = BubbleWrap::HTTP.send(method, @localhost_url, { action: 'not_valid' }, &expected_delegator)
query.instance_variable_get(:@delegator).should.equal expected_delegator
query.connectionDidFinishLoading(query.connection)
called.should.equal true
- end
+ end
end
# [:get, :post, :put, :delete, :head, :patch].each do |verb|
# it "has access to the proper response scope for #{verb} request" do
# class WatchedObj; attr_accessor :test_value end
@@ -98,31 +98,31 @@
describe "HTTP::Query" do
before do
@credentials = { username: 'mneorr', password: '123456xx!@crazy' }
- @payload = {
- user: { name: 'marin', surname: 'usalj' },
+ @payload = {
+ user: { name: 'marin', surname: 'usalj' },
twitter: '@mneorr',
website: 'mneorr.com',
values: ['apple', 'orange', 'peach'],
credentials: @credentials
}
@action = lambda{|fa, ke|}
@cache_policy = 24234
@leftover_option = 'trololo'
@headers = { 'User-Agent' => "Mozilla/5.0 (X11; Linux x86_64; rv:12.0) \n Gecko/20100101 Firefox/12.0" }
@files = {
- fake_file: NSJSONSerialization.dataWithJSONObject({ fake: 'json' }, options:0, error:nil),
+ fake_file: NSJSONSerialization.dataWithJSONObject({ fake: 'json' }, options:0, error:nil),
empty_file: NSMutableData.data
}
- @options = {
- action: @action,
+ @options = {
+ action: @action,
files: @files,
- payload: @payload,
- credentials: @credentials,
- headers: @headers,
+ payload: @payload,
+ credentials: @credentials,
+ headers: @headers,
cache_policy: @cache_policy,
leftover_option: @leftover_option
}
@query = BubbleWrap::HTTP::Query.new( @localhost_url , :get, @options )
end
@@ -150,11 +150,11 @@
it "should set the deleted delegator from options" do
@query.instance_variable_get(:@delegator).should.equal @action
@options.should.not.has_key? :action
end
-
+
it "sets the files to instance variable" do
@query.instance_variable_get(:@files).should.equal @files
@options.should.not.has_key? :files
end
@@ -188,47 +188,50 @@
it "should set payload from options{} to @payload" do
payload = "user[name]=marin&user[surname]=usalj&twitter=@mneorr&website=mneorr.com&values[]=apple&values[]=orange&values[]=peach&credentials[username]=mneorr&credentials[password]=123456xx!@crazy"
@query.instance_variable_get(:@payload).should.equal payload
@options.should.not.has_key? :payload
end
-
+
it "should check if @payload is a hash before generating GET params" do
query_string_payload = BubbleWrap::HTTP::Query.new( 'nil' , :get, { payload: "name=apple&model=macbook"} )
query_string_payload.instance_variable_get(:@payload).should.equal 'name=apple&model=macbook'
end
it "should check if payload is nil" do
- lambda{
- BubbleWrap::HTTP::Query.new( 'nil' , :post, {} )
- }.should.not.raise NoMethodError
- end
+ lambda{
+ BubbleWrap::HTTP::Query.new( 'nil' , :post, {} )
+ }.should.not.raise NoMethodError
+ end
it "should set the payload in URL only for GET and HEAD requests" do
[:post, :put, :delete, :patch].each do |method|
query = BubbleWrap::HTTP::Query.new( @localhost_url , method, { payload: @payload } )
query.instance_variable_get(:@url).description.should.equal @localhost_url
- end
+ end
payload = {name: 'marin'}
[:get, :head].each do |method|
query = BubbleWrap::HTTP::Query.new( @localhost_url , method, { payload: payload } )
query.instance_variable_get(:@url).description.should.equal "#{@localhost_url}?name=marin"
- end
+ end
end
it "sets the HTTPBody DATA to @request for all methods except GET and HEAD" do
payload = { name: 'apple', model: 'macbook'}
files = { twitter: sample_data, site: "mneorr.com".dataUsingEncoding(NSUTF8StringEncoding) }
+ puts "\n"
[:post, :put, :delete, :patch].each do |method|
+ puts " - #{method}\n"
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"
end
[:get, :head].each do |method|
+ puts " - #{method}\n"
query = BubbleWrap::HTTP::Query.new( 'nil' , method, { payload: payload } )
real_payload = NSString.alloc.initWithData(query.request.HTTPBody, encoding:NSUTF8StringEncoding)
real_payload.should.be.empty
end
end
@@ -236,10 +239,21 @@
it "sets the payload without conversion to-from NSString if the payload was NSData" do
data = sample_data
lambda { query = create_query(data, nil) }.should.not.raise NoMethodError
end
+ it "sets the payload as a string if JSON" do
+ json = BW::JSON.generate({foo:42, bar:'BubbleWrap'})
+ puts "\n"
+ [:put, :post, :delete, :patch].each do |method|
+ puts " - #{method}\n"
+ query = BubbleWrap::HTTP::Query.new( 'nil' , method, { payload: json } )
+ set_payload = NSString.alloc.initWithData(query.request.HTTPBody, encoding:NSUTF8StringEncoding)
+ set_payload.should.equal json
+ end
+ end
+
end
it "should set default timeout to 30s or the one from hash" do
@query.instance_variable_get(:@timeout).should == 30
@@ -285,11 +299,11 @@
@query.connection.was_started.should.equal true
end
it "should turn on the network indicator" do
UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should.equal true
- end
+ end
end
describe "create request" do
@@ -308,11 +322,11 @@
@query.connection.delegate.should.equal @query
end
it "should patch the NSURLRequest with done_loading and done_loading!" do
@query.request.done_loading.should.equal @query.request.instance_variable_get(:@done_loading)
-
+
@query.request.instance_variable_set(:@done_loading, false)
@query.request.done_loading.should.equal false
@query.request.done_loading!
@query.request.done_loading.should.equal true
end
@@ -322,22 +336,49 @@
@query.request.timeoutInterval.should.equal @query.instance_variable_get(:@timeout)
end
end
+ describe "create POST request" do
+
+ before do
+ @url_string = 'http://initiated-request.dev/post'
+ @headers = { fake: 'headers' }
+ @payload = { key:'abc1234' }
+ @post_query = BubbleWrap::HTTP::Query.new(@url_string, :post, {headers: @headers, payload: @payload})
+ end
+
+ it "should automatically provide Content-Type if a payload is provided" do
+ @post_query.request.allHTTPHeaderFields.should.include?('Content-Type')
+ end
+
+ it "should not add Content-Type if you provide one yourself" do
+ # also ensures check is case insenstive
+ @headers = { fake: 'headers', 'CONTENT-TYPE' => 'x-banana' }
+ @post_query = BubbleWrap::HTTP::Query.new(@url_string, :post, {headers: @headers, payload: @payload})
+ @post_query.request.allHTTPHeaderFields['CONTENT-TYPE'].should.equal @headers['CONTENT-TYPE']
+ end
+
+ it "should not add additional headers if no payload is given" do
+ @post_query = BubbleWrap::HTTP::Query.new(@url_string, :post, {headers: @headers})
+ @post_query.request.allHTTPHeaderFields.should.equal @headers
+ end
+
+ end
+
describe "Generating GET params" do
it "should create params with nested hashes with prefix[key]=value" do
expected_params = [
- 'user[name]=marin',
- 'user[surname]=usalj',
+ 'user[name]=marin',
+ 'user[surname]=usalj',
'twitter=@mneorr',
'website=mneorr.com',
'values[]=apple',
'values[]=orange',
'values[]=peach',
- "credentials[username]=mneorr",
+ "credentials[username]=mneorr",
"credentials[password]=123456xx!@crazy"
]
@query.send(:generate_get_params, @payload).should.equal expected_params
end
@@ -376,11 +417,11 @@
@query.connection(nil, didReceiveData:data)
query_received_data.length.should.equal 24
@query.connection(nil, didReceiveData:data)
query_received_data.length.should.equal 48
- end
+ end
end
@@ -390,11 +431,11 @@
end
it "should turn off network indicator" do
UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == true
@query.connection(nil, didFailWithError:@fake_error)
- UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == false
+ UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should == false
end
it "should set request_done to true" do
@query.request.done_loading.should == false
@query.connection(nil, didFailWithError:@fake_error)
@@ -542,10 +583,10 @@
@fake_sender ||= FakeSender.new
end
end
class BubbleWrap::HTTP::Query
- def create_connection(request, delegate); FakeURLConnection.new(request, delegate); end
+ def create_connection(request, delegate); FakeURLConnection.new(request, delegate); end
end
class FakeURLConnection < NSURLConnection
attr_reader :delegate, :request, :was_started
def initialize(request, delegate)