spec/motion/http_spec.rb in bubble-wrap-1.0.0 vs spec/motion/http_spec.rb in bubble-wrap-1.1.0

- old
+ new

@@ -37,23 +37,23 @@ query.connectionDidFinishLoading(query.connection) called.should.equal true 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 - @watched_object = WatchedObj.new - @name = 'Matt' - query = BubbleWrap::HTTP.send(verb, @localhost_url) do |response| - @watched_object.test_value = @name - end - wait_for_change(@watched_object, 'test_value') do - @watched_object.test_value.should == 'Matt' - 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 + # @watched_object = WatchedObj.new + # @name = 'Matt' + # query = BubbleWrap::HTTP.send(verb, @localhost_url) do |response| + # @watched_object.test_value = @name + # end + # wait_for_change(@watched_object, 'test_value') do + # @watched_object.test_value.should == 'Matt' + # end + # end + # end end describe "HTTP::Response" do @@ -67,23 +67,23 @@ end it "says OK status code 20x" do @response.ok?.should.equal true (200..209).each do |code| - BubbleWrap::HTTP::Response.new({status_code: code}).ok?.should.be.true + BubbleWrap::HTTP::Response.new(status_code: code).ok?.should.be.true end [100..101, 300..307, 400..417, 500..505].inject([]){|codes, rg| codes += rg.to_a}.each do |code| - BubbleWrap::HTTP::Response.new({status_code: code}).ok?.should.be.false + BubbleWrap::HTTP::Response.new(status_code: code).ok?.should.be.false end end it "updates ivars when calling update" do - @response.update( { one: 'one', two: 'two' } ) + @response.update(one: 'one', two: 'two') @response.instance_variable_get(:@one).should.equal 'one' @response.instance_variable_get(:@two).should.equal 'two' - @response.update( { one: 'three', two: 'four' } ) + @response.update(one: 'three', two: 'four') @response.instance_variable_get(:@one).should.equal 'three' @response.instance_variable_get(:@two).should.equal 'four' end it "has appropriate attributes" do @@ -109,11 +109,17 @@ } @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" } - @options = { action: @action, + @files = { + fake_file: NSJSONSerialization.dataWithJSONObject({ fake: 'json' }, options:0, error:nil), + empty_file: NSMutableData.data + } + @options = { + action: @action, + files: @files, payload: @payload, credentials: @credentials, headers: @headers, cache_policy: @cache_policy, leftover_option: @leftover_option @@ -144,10 +150,15 @@ 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 it "should set self as the delegator if action was not passed in" do new_query = BubbleWrap::HTTP::Query.new( 'http://localhost', :get, {}) new_query.instance_variable_get(:@delegator).should.equal new_query end @@ -161,14 +172,74 @@ generated_credentials = {:username => '', :password => ''} new_query.credentials.should.equal generated_credentials options.should.be.empty end - 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 + + describe "PAYLOAD / UPLOAD FILES" do + + def create_query(payload, files) + BubbleWrap::HTTP::Query.new( '', :post, { payload: payload, files: files } ) + end + + def sample_data + "twitter:@mneorr".dataUsingEncoding NSUTF8StringEncoding + end + + 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 + + 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 + + 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 + + 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) } + + [:post, :put, :delete, :patch].each do |method| + 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| + 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 + + 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 + end it "should set default timeout to 30s or the one from hash" do @query.instance_variable_get(:@timeout).should == 30 @@ -204,77 +275,41 @@ it "should call initiate_request with the URL passed in" do processed_url = "http://localhost?user%5Bname%5D=marin&user%5Bsurname%5D=usalj&twitter=@mneorr&website=mneorr.com&values%5B%5D=apple&values%5B%5D=orange&values%5B%5D=peach&credentials%5Busername%5D=mneorr&credentials%5Bpassword%5D=123456xx!@crazy" @query.instance_variable_get(:@url).description.should.equal processed_url end + it "should pass the new request in the new connection" do + @query.connection.request.URL.description.should.equal @query.request.URL.description + end + it "should start the connection" do @query.connection.was_started.should.equal true end it "should turn on the network indicator" do UIApplication.sharedApplication.isNetworkActivityIndicatorVisible.should.equal true end end - describe "initiate request" do + describe "create request" do before do @url_string = 'http://initiated-request.dev/to convert' - @payload = { name: 'apple', model: 'macbook'} @headers = { fake: 'headers' } - @get_query = BubbleWrap::HTTP::Query.new( @url_string , :get, { headers: @headers, payload: @payload } ) - @get_query.initiate_request @url_string + @get_query = BubbleWrap::HTTP::Query.new( @url_string , :get, { headers: @headers } ) end - it "should check if @payload is a hash before generating params" do - @get_query.instance_variable_get(:@payload).should.equal 'name=apple&model=macbook' - - 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 - nil_payload = BubbleWrap::HTTP::Query.new( 'nil' , :post, {} ) - lambda{ nil_payload.initiate_request('fake') }.should.not.raise NoMethodError - end - - it "should set the payload in URL only for GET request" do - [:post, :put, :delete, :head, :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 - - it "sets the HTTPBody DATA to @request for all methods except GET" do - - [:post, :put, :delete, :head, :patch].each do |method| - query = BubbleWrap::HTTP::Query.new( 'nil' , method, { payload: @payload } ) - real_payload = NSString.alloc.initWithData(query.request.HTTPBody, encoding:NSUTF8StringEncoding) - - real_payload.should.equal 'name=apple&model=macbook' - end - - end - - it "should add UTF8 escaping on the URL string" do - @get_query.instance_variable_get(:@url).description.should.equal 'http://initiated-request.dev/to%20convert?name=apple&model=macbook' - end - it "should create a new request with HTTP method & header fields" do @query.request.HTTPMethod.should.equal @query.method @get_query.request.allHTTPHeaderFields.should.equal @headers end it "creates a new NSURLConnection and sets itself as a delegate" do @query.connection.delegate.should.equal @query end - it "should pass the new request in the new connection" do - @query.connection.request.URL.description.should.equal @query.request.URL.description - 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 @@ -301,10 +336,10 @@ 'values[]=orange', 'values[]=peach', "credentials[username]=mneorr", "credentials[password]=123456xx!@crazy" ] - @query.generate_params(@payload).should.equal expected_params + @query.send(:generate_get_params, @payload).should.equal expected_params end end describe "when didReceiveResponse:" do