spec/motion/http_spec.rb in bubble-wrap-1.1.5 vs spec/motion/http_spec.rb in bubble-wrap-1.2.0.pre

- old
+ new

@@ -152,12 +152,12 @@ it "should accept nil header value" do @headers = { 'Authorization' => nil, 'User-Agent' => "Mozilla/5.0 (X11; Linux x86_64; rv:12.0) \n Gecko/20100101 Firefox/12.0" } @options = { headers: @headers, } - @query = BubbleWrap::HTTP::Query.new( @localhost_url , :get, @options ) - @query.should.not.be.nil + query = BubbleWrap::HTTP::Query.new( @localhost_url , :get, @options ) + query.should.not.be.nil end describe "When initialized" do it "should upcase the HTTP method" do @@ -217,11 +217,11 @@ 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" + payload = "user%5Bname%5D=marin&user%5Bsurname%5D=usalj&twitter=%40mneorr&website=mneorr.com&values%5B%5D=apple&values%5B%5D=orange&values%5B%5D=peach&credentials%5Busername%5D=mneorr&credentials%5Bpassword%5D=123456xx%21%40crazy" @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 @@ -246,10 +246,39 @@ 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 "processes filenames from file hashes" do + files = { + upload: {data: sample_data, filename: "test.txt"} + } + query = BubbleWrap::HTTP::Query.new(@fake_url, :post, {files: files}) + uuid = query.instance_variable_get(:@boundary) + real_payload = NSString.alloc.initWithData(query.request.HTTPBody, encoding:NSUTF8StringEncoding) + real_payload.should.equal "--#{uuid}\r\nContent-Disposition: form-data; name=\"upload\"; filename=\"test.txt\"\r\nContent-Type: application/octet-stream\r\n\r\ntwitter:@mneorr\r\n--#{uuid}--\r\n" + end + + it "processes filenames from file hashes, using the name when the filename is missing" do + files = { + upload: {data: sample_data} + } + query = BubbleWrap::HTTP::Query.new(@fake_url, :post, {files: files}) + uuid = query.instance_variable_get(:@boundary) + real_payload = NSString.alloc.initWithData(query.request.HTTPBody, encoding:NSUTF8StringEncoding) + real_payload.should.equal "--#{uuid}\r\nContent-Disposition: form-data; name=\"upload\"; filename=\"upload\"\r\nContent-Type: application/octet-stream\r\n\r\ntwitter:@mneorr\r\n--#{uuid}--\r\n" + end + + it "throws an error for invalid file parameters" do + files = { + twitter: {filename: "test.txt", data: nil} + } + lambda { + BW::HTTP::Query.new("http://example.com", :post, { files: files}) + }.should.raise InvalidFileError + 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" @@ -334,11 +363,11 @@ it "should create a new response before instantiating a new request" do @query.response.should.not.equal nil end 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" + processed_url = "http://localhost?user%5Bname%5D=marin&user%5Bsurname%5D=usalj&twitter=%40mneorr&website=mneorr.com&values%5B%5D=apple&values%5B%5D=orange&values%5B%5D=peach&credentials%5Busername%5D=mneorr&credentials%5Bpassword%5D=123456xx%21%40crazy" @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 @@ -581,10 +610,28 @@ request = @query.connection(nil, willSendRequest:@request, redirectResponse:nil) request.should.equal numbah < 30 ? @request : nil end end + it "should always allow canonical redirects" do + @query.options.update({:no_redirect => 1}) + request = @query.connection(nil, willSendRequest:@request, redirectResponse:nil) + request.should.equal @request + end + + it "should disallow non-canonical redirects if requested not to" do + @query.options.update({:no_redirect => 1}) + request = @query.connection(nil, willSendRequest:@request, redirectResponse:"monkey") + request.should.equal nil + end + + it "should allow non-canonical redirects by default" do + @query.options.delete(:no_redirect) + request = @query.connection(nil, willSendRequest:@request, redirectResponse:"monkey") + request.should.equal @request + end + describe "after 30 redirects" do before do 31.times do @query.connection(nil, willSendRequest:@request, redirectResponse:nil) end @@ -601,10 +648,19 @@ it "calls the delegator block" do @delegator_was_called.should.equal true end end + it "should update the request URL after redirecting by default" do + query = BubbleWrap::HTTP::Query.new( @localhost_url , :get, {} ) + redirected_request = NSMutableURLRequest.requestWithURL NSURL.URLWithString('http://expanded.local/') + query.connection(nil, willSendRequest:redirected_request, redirectResponse:nil) + query.connectionDidFinishLoading(nil) + query.response.url.absoluteString.should.equal redirected_request.URL.absoluteString + query.response.original_url.absoluteString.should.equal @localhost_url + end + end describe "didReceiveAuthenticationChallenge" do before do @challenge = FakeChallenge.new @@ -628,28 +684,46 @@ @challenge.sender.credential.persistence.should.equal @credential_persistence end it 'should continue without credentials when no credentials provided' do @options.delete :credentials - @query = BubbleWrap::HTTP::Query.new( @localhost_url , :get, @options ) - @query.connection(nil, didReceiveAuthenticationChallenge:@challenge) + query = BubbleWrap::HTTP::Query.new( @localhost_url , :get, @options ) + query.connection(nil, didReceiveAuthenticationChallenge:@challenge) @challenge.sender.continue_without_credential.should.equal true end end describe "properly format payload to url get query string" do before do - @payload = {"we love" => '#==Rock&Roll==#', "radio" => "Ga Ga", "qual" => 3.0, "incr" => -1} + @payload = {"we love" => '#==Rock&Roll==#', "radio" => "Ga Ga", "qual" => 3.0, "incr" => -1, "RFC3986" => "!*'();:@&=+$,/?%#[]"} @url_string = 'http://fake.url/method' @get_query = BubbleWrap::HTTP::Query.new( @url_string, :get, :payload => @payload) - @escaped_url = "http://fake.url/method?we%20love=%23%3D%3DRock%26Roll%3D%3D%23&radio=Ga%20Ga&qual=3.0&incr=-1" + @escaped_url = "http://fake.url/method?we%20love=%23%3D%3DRock%26Roll%3D%3D%23&radio=Ga%20Ga&qual=3.0&incr=-1&RFC3986=%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D" end - it "should escape \#=& characters only in keys and values" do + it "should escape !*'();:@&=+$,/?%#[] characters only in keys and values" do @get_query.instance_variable_get(:@url).description.should.equal @escaped_url end + + end + + describe 'properly support cookie-option for nsmutableurlrequest' do + + before do + @no_cookie_query = BubbleWrap::HTTP::Query.new("http://haz-no-cookiez.url", :get, {:payload => {:something => "else"}, :cookies => false}) + @cookie_query = BubbleWrap::HTTP::Query.new("http://haz-cookiez.url", :get, :payload => {:something => "else"}) + end + + it 'should disabled cookie-usage on nsurlrequest' do + @no_cookie_query.instance_variable_get(:@request).HTTPShouldHandleCookies.should.equal false + end + + it 'should keep sane cookie-related defaults on nsurlrequest' do + @cookie_query.instance_variable_get(:@request).HTTPShouldHandleCookies.should.equal true + end + end class FakeSender attr_reader :challenge, :credential, :was_cancelled, :continue_without_credential