http 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of http might be problematic. Click here for more details.
- data/CHANGES.md +7 -0
- data/README.md +8 -1
- data/http.gemspec +1 -1
- data/lib/http.rb +4 -1
- data/lib/http/chainable.rb +4 -4
- data/lib/http/client.rb +15 -4
- data/lib/http/compat/curb.rb +6 -6
- data/lib/http/mime_type.rb +1 -1
- data/lib/http/mime_types/json.rb +1 -1
- data/lib/http/options.rb +3 -1
- data/lib/http/request.rb +4 -33
- data/lib/http/request_stream.rb +73 -0
- data/lib/http/response.rb +1 -1
- data/lib/http/response_parser.rb +1 -1
- data/lib/http/version.rb +2 -2
- data/spec/http/compat/curb_spec.rb +4 -4
- data/spec/http/options/headers_spec.rb +6 -5
- data/spec/http/options/merge_spec.rb +3 -2
- data/spec/http/options/new_spec.rb +3 -3
- data/spec/http/options/proxy_spec.rb +1 -1
- data/spec/http/options/response_spec.rb +1 -1
- data/spec/http/request_stream_spec.rb +26 -0
- data/spec/http_spec.rb +13 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/support/proxy_server.rb +4 -4
- metadata +93 -59
    
        data/CHANGES.md
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -25,8 +25,15 @@ Http.post "http://example.com/resource", :form => {:foo => "42"} | |
| 25 25 | 
             
            ```
         | 
| 26 26 |  | 
| 27 27 | 
             
            Want to POST with a specific body, JSON for instance?
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            ```ruby
         | 
| 30 | 
            +
            Http.post "http://example.com/resource", :body => JSON.dump(:foo => '42')
         | 
| 31 | 
            +
            ```
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            Or have it serialize JSON for you:
         | 
| 34 | 
            +
             | 
| 28 35 | 
             
            ```ruby
         | 
| 29 | 
            -
            Http.post "http://example.com/resource", : | 
| 36 | 
            +
            Http.post "http://example.com/resource", :json => {:foo => '42'}
         | 
| 30 37 | 
             
            ```
         | 
| 31 38 |  | 
| 32 39 | 
             
            It's easy!
         | 
    
        data/http.gemspec
    CHANGED
    
    | @@ -13,7 +13,7 @@ Gem::Specification.new do |gem| | |
| 13 13 | 
             
              gem.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         | 
| 14 14 | 
             
              gem.name          = "http"
         | 
| 15 15 | 
             
              gem.require_paths = ["lib"]
         | 
| 16 | 
            -
              gem.version       =  | 
| 16 | 
            +
              gem.version       = HTTP::VERSION
         | 
| 17 17 |  | 
| 18 18 | 
             
              gem.add_runtime_dependency 'http_parser.rb'
         | 
| 19 19 | 
             
              gem.add_runtime_dependency 'certified'
         | 
    
        data/lib/http.rb
    CHANGED
    
    | @@ -8,12 +8,13 @@ require 'http/client' | |
| 8 8 | 
             
            require 'http/mime_type'
         | 
| 9 9 | 
             
            require 'http/options'
         | 
| 10 10 | 
             
            require 'http/request'
         | 
| 11 | 
            +
            require 'http/request_stream'
         | 
| 11 12 | 
             
            require 'http/response'
         | 
| 12 13 | 
             
            require 'http/response_parser'
         | 
| 13 14 | 
             
            require 'http/uri_backport' if RUBY_VERSION < "1.9.0"
         | 
| 14 15 |  | 
| 15 16 | 
             
            # HTTP should be easy
         | 
| 16 | 
            -
            module  | 
| 17 | 
            +
            module HTTP
         | 
| 17 18 | 
             
              extend Chainable
         | 
| 18 19 |  | 
| 19 20 | 
             
              # The method given was not understood
         | 
| @@ -38,3 +39,5 @@ module Http | |
| 38 39 | 
             
                end
         | 
| 39 40 | 
             
              end
         | 
| 40 41 | 
             
            end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            Http = HTTP unless defined?(Http)
         | 
    
        data/lib/http/chainable.rb
    CHANGED
    
    | @@ -1,4 +1,4 @@ | |
| 1 | 
            -
            module  | 
| 1 | 
            +
            module HTTP
         | 
| 2 2 | 
             
              module Chainable
         | 
| 3 3 | 
             
                # Request a get sans response body
         | 
| 4 4 | 
             
                def head(uri, options = {})
         | 
| @@ -54,15 +54,15 @@ module Http | |
| 54 54 | 
             
                def on(event, &block)
         | 
| 55 55 | 
             
                  branch default_options.with_callback(event, block)
         | 
| 56 56 | 
             
                end
         | 
| 57 | 
            -
             | 
| 57 | 
            +
             | 
| 58 58 | 
             
                # Make a request through an HTTP proxy
         | 
| 59 59 | 
             
                def via(*proxy)
         | 
| 60 | 
            -
                  proxy_hash = {} | 
| 60 | 
            +
                  proxy_hash = {}
         | 
| 61 61 | 
             
                  proxy_hash[:proxy_address] = proxy[0] if proxy[0].is_a? String
         | 
| 62 62 | 
             
                  proxy_hash[:proxy_port]    = proxy[1] if proxy[1].is_a? Integer
         | 
| 63 63 | 
             
                  proxy_hash[:proxy_username]= proxy[2] if proxy[2].is_a? String
         | 
| 64 64 | 
             
                  proxy_hash[:proxy_password]= proxy[3] if proxy[3].is_a? String
         | 
| 65 | 
            -
             | 
| 65 | 
            +
             | 
| 66 66 | 
             
                  if proxy_hash.keys.size >=2
         | 
| 67 67 | 
             
                    branch default_options.with_proxy(proxy_hash)
         | 
| 68 68 | 
             
                  else
         | 
    
        data/lib/http/client.rb
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            require 'uri'
         | 
| 2 2 |  | 
| 3 | 
            -
            module  | 
| 3 | 
            +
            module HTTP
         | 
| 4 4 | 
             
              # Clients make requests and receive responses
         | 
| 5 5 | 
             
              class Client
         | 
| 6 6 | 
             
                include Chainable
         | 
| @@ -25,17 +25,23 @@ module Http | |
| 25 25 | 
             
                # Make an HTTP request
         | 
| 26 26 | 
             
                def request(method, uri, options = {})
         | 
| 27 27 | 
             
                  opts = @default_options.merge(options)
         | 
| 28 | 
            +
                  host = URI.parse(uri).host
         | 
| 29 | 
            +
                  opts.headers["Host"] = host
         | 
| 28 30 | 
             
                  headers = opts.headers
         | 
| 29 31 | 
             
                  proxy = opts.proxy
         | 
| 30 32 |  | 
| 31 33 | 
             
                  method_body = body(opts, headers)
         | 
| 32 | 
            -
                  puts method_body
         | 
| 33 34 | 
             
                  request = Request.new method, uri, headers, proxy, method_body
         | 
| 34 35 |  | 
| 35 36 | 
             
                  if opts.follow
         | 
| 36 37 | 
             
                    code = 302
         | 
| 37 38 | 
             
                    while code == 302 or code == 301
         | 
| 38 | 
            -
                       | 
| 39 | 
            +
                      # if the uri isn't fully formed complete it
         | 
| 40 | 
            +
                      if not uri.match /\./
         | 
| 41 | 
            +
                        uri = "http://#{host}#{uri}"
         | 
| 42 | 
            +
                      end
         | 
| 43 | 
            +
                      host = URI.parse(uri).host
         | 
| 44 | 
            +
                      opts.headers["Host"] = host
         | 
| 39 45 | 
             
                      method_body = body(opts, headers)
         | 
| 40 46 | 
             
                      request = Request.new method, uri, headers, proxy, method_body
         | 
| 41 47 | 
             
                      response = perform request, opts
         | 
| @@ -57,7 +63,12 @@ module Http | |
| 57 63 | 
             
                  socket = options[:socket_class].open(uri.host, uri.port) # TODO: proxy support
         | 
| 58 64 |  | 
| 59 65 | 
             
                  if uri.is_a?(URI::HTTPS)
         | 
| 60 | 
            -
                     | 
| 66 | 
            +
                    if options[:ssl_context] == nil
         | 
| 67 | 
            +
                      context = OpenSSL::SSL::SSLContext.new
         | 
| 68 | 
            +
                    else
         | 
| 69 | 
            +
                      context = options[:ssl_context]
         | 
| 70 | 
            +
                    end
         | 
| 71 | 
            +
                    socket = options[:ssl_socket_class].new(socket, context)
         | 
| 61 72 | 
             
                    socket.connect
         | 
| 62 73 | 
             
                  end
         | 
| 63 74 |  | 
    
        data/lib/http/compat/curb.rb
    CHANGED
    
    | @@ -62,21 +62,21 @@ module Curl | |
| 62 62 | 
             
                  perform
         | 
| 63 63 | 
             
                end
         | 
| 64 64 | 
             
              end
         | 
| 65 | 
            -
             | 
| 65 | 
            +
             | 
| 66 66 | 
             
              class Multi
         | 
| 67 67 | 
             
                def initialize
         | 
| 68 68 | 
             
                  @clients = []
         | 
| 69 69 | 
             
                  @done = false
         | 
| 70 70 | 
             
                end
         | 
| 71 | 
            -
             | 
| 71 | 
            +
             | 
| 72 72 | 
             
                def add(client)
         | 
| 73 73 | 
             
                  @clients << client
         | 
| 74 74 | 
             
                end
         | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 75 | 
            +
             | 
| 76 | 
            +
             | 
| 77 77 | 
             
                def perform
         | 
| 78 78 | 
             
                  return if @done
         | 
| 79 | 
            -
             | 
| 79 | 
            +
             | 
| 80 80 | 
             
                  @clients.map do |client|
         | 
| 81 81 | 
             
                    Thread.new { client.perform }
         | 
| 82 82 | 
             
                  end.each(&:join)
         | 
| @@ -84,4 +84,4 @@ module Curl | |
| 84 84 | 
             
                  @done = true
         | 
| 85 85 | 
             
                end
         | 
| 86 86 | 
             
              end
         | 
| 87 | 
            -
            end
         | 
| 87 | 
            +
            end
         | 
    
        data/lib/http/mime_type.rb
    CHANGED
    
    
    
        data/lib/http/mime_types/json.rb
    CHANGED
    
    
    
        data/lib/http/options.rb
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            require 'socket'
         | 
| 2 2 | 
             
            require 'openssl'
         | 
| 3 3 |  | 
| 4 | 
            -
            module  | 
| 4 | 
            +
            module HTTP
         | 
| 5 5 | 
             
              class Options
         | 
| 6 6 |  | 
| 7 7 | 
             
                # How to format the response [:object, :body, :parse_body]
         | 
| @@ -57,6 +57,8 @@ module Http | |
| 57 57 | 
             
                  @socket_class     = options[:socket_class]     || self.class.default_socket_class
         | 
| 58 58 | 
             
                  @ssl_socket_class = options[:ssl_socket_class] || self.class.default_ssl_socket_class
         | 
| 59 59 | 
             
                  @ssl_context      = options[:ssl_context]
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                  @headers["User-Agent"] ||= "RubyHttpGem/#{Http::VERSION}"
         | 
| 60 62 | 
             
                end
         | 
| 61 63 |  | 
| 62 64 | 
             
                def with_response(response)
         | 
    
        data/lib/http/request.rb
    CHANGED
    
    | @@ -1,4 +1,4 @@ | |
| 1 | 
            -
            module  | 
| 1 | 
            +
            module HTTP
         | 
| 2 2 | 
             
              class Request
         | 
| 3 3 | 
             
                # Method is given as a lowercase symbol e.g. :get, :post
         | 
| 4 4 | 
             
                attr_reader :method
         | 
| @@ -33,38 +33,9 @@ module Http | |
| 33 33 |  | 
| 34 34 | 
             
                # Stream the request to a socket
         | 
| 35 35 | 
             
                def stream(socket)
         | 
| 36 | 
            -
                  request_header = "#{method.to_s.upcase} #{uri.path} HTTP/#{version} | 
| 37 | 
            -
                   | 
| 38 | 
            -
             | 
| 39 | 
            -
                  end
         | 
| 40 | 
            -
             | 
| 41 | 
            -
                  case body
         | 
| 42 | 
            -
                  when NilClass
         | 
| 43 | 
            -
                    socket << request_header << CRLF
         | 
| 44 | 
            -
                    return
         | 
| 45 | 
            -
                  when String
         | 
| 46 | 
            -
                    request_header << "Content-Length: #{body.length}#{CRLF}" unless @headers['Content-Length']
         | 
| 47 | 
            -
                    request_header << CRLF
         | 
| 48 | 
            -
             | 
| 49 | 
            -
                    socket << request_header
         | 
| 50 | 
            -
                    socket << body.to_s
         | 
| 51 | 
            -
                  when Enumerable
         | 
| 52 | 
            -
                    if encoding = @headers['Transfer-Encoding']
         | 
| 53 | 
            -
                      raise ArgumentError, "invalid transfer encoding" unless encoding == "chunked"
         | 
| 54 | 
            -
                      request_header << CRLF
         | 
| 55 | 
            -
                    else
         | 
| 56 | 
            -
                      request_header << "Transfer-Encoding: chunked#{CRLF * 2}"
         | 
| 57 | 
            -
                    end
         | 
| 58 | 
            -
             | 
| 59 | 
            -
                    socket << request_header
         | 
| 60 | 
            -
                    body.each do |chunk|
         | 
| 61 | 
            -
                      socket << chunk.bytesize.to_s(16) << CRLF
         | 
| 62 | 
            -
                      socket << chunk
         | 
| 63 | 
            -
                    end
         | 
| 64 | 
            -
             | 
| 65 | 
            -
                    socket << "0" << CRLF * 2
         | 
| 66 | 
            -
                  else raise TypeError, "invalid body type: #{body.class}"
         | 
| 67 | 
            -
                  end
         | 
| 36 | 
            +
                  request_header = "#{method.to_s.upcase} #{uri.path} HTTP/#{version}"
         | 
| 37 | 
            +
                  rs = Http::RequestStream.new socket, body, @headers, request_header
         | 
| 38 | 
            +
                  rs.stream
         | 
| 68 39 | 
             
                end
         | 
| 69 40 | 
             
              end
         | 
| 70 41 | 
             
            end
         | 
| @@ -0,0 +1,73 @@ | |
| 1 | 
            +
            module HTTP
         | 
| 2 | 
            +
              class RequestStream
         | 
| 3 | 
            +
                def initialize(socket, body, headers, headerstart)
         | 
| 4 | 
            +
                  @body           = body
         | 
| 5 | 
            +
                  raise ArgumentError, "body of wrong type" unless valid_body_type
         | 
| 6 | 
            +
                  @socket         = socket
         | 
| 7 | 
            +
                  @headers        = headers
         | 
| 8 | 
            +
                  @request_header = [headerstart]
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def valid_body_type
         | 
| 12 | 
            +
                  valid_types= [String, NilClass, Enumerable]
         | 
| 13 | 
            +
                  checks = valid_types.map {|type| @body.is_a? type}
         | 
| 14 | 
            +
                  checks.any?
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                #Adds headers to the request header from the headers array
         | 
| 18 | 
            +
                def add_headers
         | 
| 19 | 
            +
                  @headers.each do |field, value|
         | 
| 20 | 
            +
                    @request_header << "#{field}: #{value}"
         | 
| 21 | 
            +
                  end
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                # Stream the request to a socket
         | 
| 25 | 
            +
                def stream
         | 
| 26 | 
            +
                  self.send_request_header
         | 
| 27 | 
            +
                  self.send_request_body
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                # Adds the headers to the header array for the given request body we are working
         | 
| 31 | 
            +
                # with
         | 
| 32 | 
            +
                def add_body_type_headers
         | 
| 33 | 
            +
                  if @body.is_a? String and not @headers['Content-Length']
         | 
| 34 | 
            +
                    @request_header << "Content-Length: #{@body.length}"
         | 
| 35 | 
            +
                  elsif @body.is_a? Enumerable
         | 
| 36 | 
            +
                    if encoding = @headers['Transfer-Encoding'] and not encoding == "chunked"
         | 
| 37 | 
            +
                      raise ArgumentError, "invalid transfer encoding"
         | 
| 38 | 
            +
                    else
         | 
| 39 | 
            +
                      @request_header << "Transfer-Encoding: chunked"
         | 
| 40 | 
            +
                    end
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                # Joins the headers specified in the request into a correctly formatted
         | 
| 45 | 
            +
                # http request header string
         | 
| 46 | 
            +
                def join_headers
         | 
| 47 | 
            +
                  # join the headers array with crlfs, stick two on the end because
         | 
| 48 | 
            +
                  # that ends the request header
         | 
| 49 | 
            +
                  @request_header.join(CRLF) + (CRLF)*2
         | 
| 50 | 
            +
                end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                def send_request_header
         | 
| 53 | 
            +
                  self.add_headers
         | 
| 54 | 
            +
                  self.add_body_type_headers
         | 
| 55 | 
            +
                  header = self.join_headers
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                  @socket << header
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                def send_request_body
         | 
| 61 | 
            +
                  if @body.is_a? String
         | 
| 62 | 
            +
                    @socket << @body
         | 
| 63 | 
            +
                  elsif @body.is_a? Enumerable
         | 
| 64 | 
            +
                    @body.each do |chunk|
         | 
| 65 | 
            +
                      @socket << chunk.bytesize.to_s(16) << CRLF
         | 
| 66 | 
            +
                      @socket << chunk
         | 
| 67 | 
            +
                    end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                    @socket << "0" << CRLF * 2
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
                end
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
            end
         | 
    
        data/lib/http/response.rb
    CHANGED
    
    
    
        data/lib/http/response_parser.rb
    CHANGED
    
    
    
        data/lib/http/version.rb
    CHANGED
    
    | @@ -1,3 +1,3 @@ | |
| 1 | 
            -
            module  | 
| 2 | 
            -
              VERSION = "0. | 
| 1 | 
            +
            module HTTP
         | 
| 2 | 
            +
              VERSION = "0.4.0"
         | 
| 3 3 | 
             
            end
         | 
| @@ -19,14 +19,14 @@ describe Curl do | |
| 19 19 | 
             
                  end
         | 
| 20 20 | 
             
                end
         | 
| 21 21 | 
             
              end
         | 
| 22 | 
            -
             | 
| 22 | 
            +
             | 
| 23 23 | 
             
              describe Curl::Multi do
         | 
| 24 24 | 
             
                it "gets resources" do
         | 
| 25 25 | 
             
                  requests  = [test_endpoint]
         | 
| 26 26 | 
             
                  responses = []
         | 
| 27 | 
            -
             | 
| 27 | 
            +
             | 
| 28 28 | 
             
                  multi = Curl::Multi.new
         | 
| 29 | 
            -
             | 
| 29 | 
            +
             | 
| 30 30 | 
             
                  requests.each do |url|
         | 
| 31 31 | 
             
                    response = Curl::Easy.new url, :get
         | 
| 32 32 | 
             
                    multi.add response
         | 
| @@ -37,4 +37,4 @@ describe Curl do | |
| 37 37 | 
             
                  responses.first.body_str.should match(/<!doctype html>/)
         | 
| 38 38 | 
             
                end
         | 
| 39 39 | 
             
              end
         | 
| 40 | 
            -
            end
         | 
| 40 | 
            +
            end
         | 
| @@ -2,16 +2,17 @@ require 'spec_helper' | |
| 2 2 |  | 
| 3 3 | 
             
            describe Http::Options, "headers" do
         | 
| 4 4 |  | 
| 5 | 
            -
              let(:opts){ Http::Options.new }
         | 
| 5 | 
            +
              let(:opts)       { Http::Options.new }
         | 
| 6 | 
            +
              let(:user_agent) { "RubyHttpGem/#{Http::VERSION}" }
         | 
| 6 7 |  | 
| 7 | 
            -
              it 'defaults to  | 
| 8 | 
            -
                opts.headers.should eq( | 
| 8 | 
            +
              it 'defaults to just the user agent' do
         | 
| 9 | 
            +
                opts.headers.should eq("User-Agent" => user_agent)
         | 
| 9 10 | 
             
              end
         | 
| 10 11 |  | 
| 11 12 | 
             
              it 'may be specified with with_headers' do
         | 
| 12 13 | 
             
                opts2 = opts.with_headers("accept" => "json")
         | 
| 13 | 
            -
                opts.headers.should eq( | 
| 14 | 
            -
                opts2.headers.should eq("accept" => "json")
         | 
| 14 | 
            +
                opts.headers.should eq("User-Agent" => user_agent)
         | 
| 15 | 
            +
                opts2.headers.should eq("accept" => "json", "User-Agent" => user_agent)
         | 
| 15 16 | 
             
              end
         | 
| 16 17 |  | 
| 17 18 | 
             
              it 'accepts any object that respond to :to_hash' do
         | 
| @@ -2,7 +2,8 @@ require 'spec_helper' | |
| 2 2 |  | 
| 3 3 | 
             
            describe Http::Options, "merge" do
         | 
| 4 4 |  | 
| 5 | 
            -
              let(:opts){ Http::Options.new }
         | 
| 5 | 
            +
              let(:opts)       { Http::Options.new }
         | 
| 6 | 
            +
              let(:user_agent) { "RubyHttpGem/#{Http::VERSION}" }
         | 
| 6 7 |  | 
| 7 8 | 
             
              it 'supports a Hash' do
         | 
| 8 9 | 
             
                old_response = opts.response
         | 
| @@ -34,7 +35,7 @@ describe Http::Options, "merge" do | |
| 34 35 | 
             
                  :response  => :parsed_body,
         | 
| 35 36 | 
             
                  :form      => {:bar => 'bar'},
         | 
| 36 37 | 
             
                  :body      => "body-bar",
         | 
| 37 | 
            -
                  :headers   => {:accept  => "xml", :foo => "foo", :bar => 'bar'},
         | 
| 38 | 
            +
                  :headers   => {:accept  => "xml", :foo => "foo", :bar => 'bar', "User-Agent" => user_agent},
         | 
| 38 39 | 
             
                  :proxy     => {:proxy_address => "127.0.0.1", :proxy_port => 8080},
         | 
| 39 40 | 
             
                  :callbacks => {:request => ["common"], :response => ["foo", "bar"]},
         | 
| 40 41 | 
             
                  :follow => nil
         | 
| @@ -1,6 +1,7 @@ | |
| 1 1 | 
             
            require 'spec_helper'
         | 
| 2 2 |  | 
| 3 3 | 
             
            describe Http::Options, "new" do
         | 
| 4 | 
            +
              let(:user_agent) { "RubyHttpGem/#{Http::VERSION}" }
         | 
| 4 5 |  | 
| 5 6 | 
             
              it 'supports a Options instance' do
         | 
| 6 7 | 
             
                opts = Http::Options.new
         | 
| @@ -8,7 +9,6 @@ describe Http::Options, "new" do | |
| 8 9 | 
             
              end
         | 
| 9 10 |  | 
| 10 11 | 
             
              context 'with a Hash' do
         | 
| 11 | 
            -
             | 
| 12 12 | 
             
                it 'coerces :response correctly' do
         | 
| 13 13 | 
             
                  opts = Http::Options.new(:response => :object)
         | 
| 14 14 | 
             
                  opts.response.should eq(:object)
         | 
| @@ -16,9 +16,9 @@ describe Http::Options, "new" do | |
| 16 16 |  | 
| 17 17 | 
             
                it 'coerces :headers correctly' do
         | 
| 18 18 | 
             
                  opts = Http::Options.new(:headers => {:accept => "json"})
         | 
| 19 | 
            -
                  opts.headers.should eq(:accept => "json")
         | 
| 19 | 
            +
                  opts.headers.should eq(:accept => "json", "User-Agent" => user_agent)
         | 
| 20 20 | 
             
                end
         | 
| 21 | 
            -
             | 
| 21 | 
            +
             | 
| 22 22 | 
             
                it 'coerces :proxy correctly' do
         | 
| 23 23 | 
             
                  opts = Http::Options.new(:proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080})
         | 
| 24 24 | 
             
                  opts.proxy.should eq(:proxy_address => "127.0.0.1", :proxy_port => 8080)
         | 
| @@ -18,4 +18,4 @@ describe Http::Options, "proxy" do | |
| 18 18 | 
             
                opts2 = opts.with_proxy(:proxy_address => "127.0.0.1", :proxy_port => 8080, :proxy_username => "username", :proxy_password => "password")
         | 
| 19 19 | 
             
                opts2.proxy.should eq(:proxy_address => "127.0.0.1", :proxy_port => 8080, :proxy_username => "username", :proxy_password => "password")
         | 
| 20 20 | 
             
              end
         | 
| 21 | 
            -
            end
         | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe Http::RequestStream do
         | 
| 4 | 
            +
              describe "#initalize" do
         | 
| 5 | 
            +
                def construct(body)
         | 
| 6 | 
            +
                  Http::RequestStream.new(nil, body, [], "")
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                it "doesn't throw on a nil body" do
         | 
| 10 | 
            +
                  expect {construct []}.to_not raise_error(ArgumentError)
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                it "doesn't throw on a String body" do
         | 
| 14 | 
            +
                  expect {construct "string body"}.to_not raise_error(ArgumentError)
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                it "doesn't throw on an Enumerable body" do
         | 
| 18 | 
            +
                  expect {construct ["bees", "cows"]}.to_not raise_error(ArgumentError)
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                it "does throw on a body that isn't string, enumerable or nil" do
         | 
| 22 | 
            +
                  expect {construct true}.to raise_error(ArgumentError)
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
    
        data/spec/http_spec.rb
    CHANGED
    
    | @@ -11,6 +11,19 @@ describe Http do | |
| 11 11 | 
             
                  response.should match(/<!doctype html>/)
         | 
| 12 12 | 
             
                end
         | 
| 13 13 |  | 
| 14 | 
            +
                it "should be easy to get a https resource" do
         | 
| 15 | 
            +
                  response = Http.with_headers(:accept => 'application/json').get "https://api.github.com/users/samphippen"
         | 
| 16 | 
            +
                  response["type"].should == "User"
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                it "can get some real world sites, following redirects if necessary" do
         | 
| 20 | 
            +
                  sites = ["http://github.com/", "http://xkcd.com/", "http://www.spotify.com/"]
         | 
| 21 | 
            +
                  sites.each do |site|
         | 
| 22 | 
            +
                    resp = Http.with_response(:object).with_follow(true).get site
         | 
| 23 | 
            +
                    resp.status.should == 200
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 14 27 | 
             
                context "with_response" do
         | 
| 15 28 | 
             
                  it 'allows specifying :object' do
         | 
| 16 29 | 
             
                    res = Http.with_response(:object).get test_endpoint
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    
| @@ -1,17 +1,17 @@ | |
| 1 1 | 
             
            require 'webrick/httpproxy'
         | 
| 2 | 
            -
             | 
| 2 | 
            +
             | 
| 3 3 | 
             
            ProxyServer     = WEBrick::HTTPProxyServer.new(:Port => 8080, :AccessLog => [])
         | 
| 4 | 
            -
             | 
| 4 | 
            +
             | 
| 5 5 | 
             
            t = Thread.new { ProxyServer.start }
         | 
| 6 6 | 
             
            trap("INT")    { ProxyServer.shutdown; exit }
         | 
| 7 7 |  | 
| 8 | 
            -
            AuthenticatedProxyServer = WEBrick::HTTPProxyServer.new(:Port => 8081, | 
| 8 | 
            +
            AuthenticatedProxyServer = WEBrick::HTTPProxyServer.new(:Port => 8081,
         | 
| 9 9 | 
             
                              :ProxyAuthProc => Proc.new do | req, res |
         | 
| 10 10 | 
             
                                WEBrick::HTTPAuth.proxy_basic_auth(req, res, 'proxy') do | user, pass |
         | 
| 11 11 | 
             
                                  user == 'username' and pass == 'password'
         | 
| 12 12 | 
             
                                end
         | 
| 13 13 | 
             
                              end)
         | 
| 14 | 
            -
             | 
| 14 | 
            +
             | 
| 15 15 |  | 
| 16 16 | 
             
            t = Thread.new { AuthenticatedProxyServer.start }
         | 
| 17 17 | 
             
            trap("INT")    { AuthenticatedProxyServer.shutdown; exit }
         | 
    
        metadata
    CHANGED
    
    | @@ -1,79 +1,102 @@ | |
| 1 | 
            -
            --- !ruby/object:Gem::Specification
         | 
| 1 | 
            +
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: http
         | 
| 3 | 
            -
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
               | 
| 3 | 
            +
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            +
              hash: 15
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 | 
            +
              segments: 
         | 
| 7 | 
            +
              - 0
         | 
| 8 | 
            +
              - 4
         | 
| 9 | 
            +
              - 0
         | 
| 10 | 
            +
              version: 0.4.0
         | 
| 6 11 | 
             
            platform: ruby
         | 
| 7 | 
            -
            authors:
         | 
| 12 | 
            +
            authors: 
         | 
| 8 13 | 
             
            - Tony Arcieri
         | 
| 9 14 | 
             
            autorequire: 
         | 
| 10 15 | 
             
            bindir: bin
         | 
| 11 16 | 
             
            cert_chain: []
         | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 17 | 
            +
             | 
| 18 | 
            +
            date: 2012-10-12 00:00:00 Z
         | 
| 19 | 
            +
            dependencies: 
         | 
| 20 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 15 21 | 
             
              name: http_parser.rb
         | 
| 16 | 
            -
               | 
| 22 | 
            +
              version_requirements: &id001 !ruby/object:Gem::Requirement 
         | 
| 17 23 | 
             
                none: false
         | 
| 18 | 
            -
                requirements:
         | 
| 19 | 
            -
                - -  | 
| 20 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 21 | 
            -
                     | 
| 24 | 
            +
                requirements: 
         | 
| 25 | 
            +
                - - ">="
         | 
| 26 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 27 | 
            +
                    hash: 3
         | 
| 28 | 
            +
                    segments: 
         | 
| 29 | 
            +
                    - 0
         | 
| 30 | 
            +
                    version: "0"
         | 
| 22 31 | 
             
              type: :runtime
         | 
| 32 | 
            +
              requirement: *id001
         | 
| 23 33 | 
             
              prerelease: false
         | 
| 24 | 
            -
             | 
| 25 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 34 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 26 35 | 
             
              name: certified
         | 
| 27 | 
            -
               | 
| 36 | 
            +
              version_requirements: &id002 !ruby/object:Gem::Requirement 
         | 
| 28 37 | 
             
                none: false
         | 
| 29 | 
            -
                requirements:
         | 
| 30 | 
            -
                - -  | 
| 31 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 32 | 
            -
                     | 
| 38 | 
            +
                requirements: 
         | 
| 39 | 
            +
                - - ">="
         | 
| 40 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 41 | 
            +
                    hash: 3
         | 
| 42 | 
            +
                    segments: 
         | 
| 43 | 
            +
                    - 0
         | 
| 44 | 
            +
                    version: "0"
         | 
| 33 45 | 
             
              type: :runtime
         | 
| 46 | 
            +
              requirement: *id002
         | 
| 34 47 | 
             
              prerelease: false
         | 
| 35 | 
            -
             | 
| 36 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 48 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 37 49 | 
             
              name: rake
         | 
| 38 | 
            -
               | 
| 50 | 
            +
              version_requirements: &id003 !ruby/object:Gem::Requirement 
         | 
| 39 51 | 
             
                none: false
         | 
| 40 | 
            -
                requirements:
         | 
| 41 | 
            -
                - -  | 
| 42 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 43 | 
            -
                     | 
| 52 | 
            +
                requirements: 
         | 
| 53 | 
            +
                - - ">="
         | 
| 54 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 55 | 
            +
                    hash: 3
         | 
| 56 | 
            +
                    segments: 
         | 
| 57 | 
            +
                    - 0
         | 
| 58 | 
            +
                    version: "0"
         | 
| 44 59 | 
             
              type: :development
         | 
| 60 | 
            +
              requirement: *id003
         | 
| 45 61 | 
             
              prerelease: false
         | 
| 46 | 
            -
             | 
| 47 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 62 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 48 63 | 
             
              name: rspec
         | 
| 49 | 
            -
               | 
| 64 | 
            +
              version_requirements: &id004 !ruby/object:Gem::Requirement 
         | 
| 50 65 | 
             
                none: false
         | 
| 51 | 
            -
                requirements:
         | 
| 52 | 
            -
                - -  | 
| 53 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            -
                     | 
| 66 | 
            +
                requirements: 
         | 
| 67 | 
            +
                - - ">="
         | 
| 68 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 69 | 
            +
                    hash: 3
         | 
| 70 | 
            +
                    segments: 
         | 
| 71 | 
            +
                    - 0
         | 
| 72 | 
            +
                    version: "0"
         | 
| 55 73 | 
             
              type: :development
         | 
| 74 | 
            +
              requirement: *id004
         | 
| 56 75 | 
             
              prerelease: false
         | 
| 57 | 
            -
             | 
| 58 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 76 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 59 77 | 
             
              name: json
         | 
| 60 | 
            -
               | 
| 78 | 
            +
              version_requirements: &id005 !ruby/object:Gem::Requirement 
         | 
| 61 79 | 
             
                none: false
         | 
| 62 | 
            -
                requirements:
         | 
| 63 | 
            -
                - -  | 
| 64 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 65 | 
            -
                     | 
| 80 | 
            +
                requirements: 
         | 
| 81 | 
            +
                - - ">="
         | 
| 82 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 83 | 
            +
                    hash: 3
         | 
| 84 | 
            +
                    segments: 
         | 
| 85 | 
            +
                    - 0
         | 
| 86 | 
            +
                    version: "0"
         | 
| 66 87 | 
             
              type: :development
         | 
| 88 | 
            +
              requirement: *id005
         | 
| 67 89 | 
             
              prerelease: false
         | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
              zoo
         | 
| 71 | 
            -
            email:
         | 
| 90 | 
            +
            description: HTTP so awesome it will lure Catherine Zeta Jones into your unicorn petting zoo
         | 
| 91 | 
            +
            email: 
         | 
| 72 92 | 
             
            - tony.arcieri@gmail.com
         | 
| 73 93 | 
             
            executables: []
         | 
| 94 | 
            +
             | 
| 74 95 | 
             
            extensions: []
         | 
| 96 | 
            +
             | 
| 75 97 | 
             
            extra_rdoc_files: []
         | 
| 76 | 
            -
             | 
| 98 | 
            +
             | 
| 99 | 
            +
            files: 
         | 
| 77 100 | 
             
            - .gitignore
         | 
| 78 101 | 
             
            - .rspec
         | 
| 79 102 | 
             
            - .travis.yml
         | 
| @@ -91,6 +114,7 @@ files: | |
| 91 114 | 
             
            - lib/http/mime_types/json.rb
         | 
| 92 115 | 
             
            - lib/http/options.rb
         | 
| 93 116 | 
             
            - lib/http/request.rb
         | 
| 117 | 
            +
            - lib/http/request_stream.rb
         | 
| 94 118 | 
             
            - lib/http/response.rb
         | 
| 95 119 | 
             
            - lib/http/response_parser.rb
         | 
| 96 120 | 
             
            - lib/http/streaming_body.rb
         | 
| @@ -106,6 +130,7 @@ files: | |
| 106 130 | 
             
            - spec/http/options/proxy_spec.rb
         | 
| 107 131 | 
             
            - spec/http/options/response_spec.rb
         | 
| 108 132 | 
             
            - spec/http/options_spec.rb
         | 
| 133 | 
            +
            - spec/http/request_stream_spec.rb
         | 
| 109 134 | 
             
            - spec/http/response_spec.rb
         | 
| 110 135 | 
             
            - spec/http_spec.rb
         | 
| 111 136 | 
             
            - spec/spec_helper.rb
         | 
| @@ -113,29 +138,38 @@ files: | |
| 113 138 | 
             
            - spec/support/proxy_server.rb
         | 
| 114 139 | 
             
            homepage: https://github.com/tarcieri/http
         | 
| 115 140 | 
             
            licenses: []
         | 
| 141 | 
            +
             | 
| 116 142 | 
             
            post_install_message: 
         | 
| 117 143 | 
             
            rdoc_options: []
         | 
| 118 | 
            -
             | 
| 144 | 
            +
             | 
| 145 | 
            +
            require_paths: 
         | 
| 119 146 | 
             
            - lib
         | 
| 120 | 
            -
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 147 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         | 
| 121 148 | 
             
              none: false
         | 
| 122 | 
            -
              requirements:
         | 
| 123 | 
            -
              - -  | 
| 124 | 
            -
                - !ruby/object:Gem::Version
         | 
| 125 | 
            -
                   | 
| 126 | 
            -
             | 
| 149 | 
            +
              requirements: 
         | 
| 150 | 
            +
              - - ">="
         | 
| 151 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 152 | 
            +
                  hash: 3
         | 
| 153 | 
            +
                  segments: 
         | 
| 154 | 
            +
                  - 0
         | 
| 155 | 
            +
                  version: "0"
         | 
| 156 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 127 157 | 
             
              none: false
         | 
| 128 | 
            -
              requirements:
         | 
| 129 | 
            -
              - -  | 
| 130 | 
            -
                - !ruby/object:Gem::Version
         | 
| 131 | 
            -
                   | 
| 158 | 
            +
              requirements: 
         | 
| 159 | 
            +
              - - ">="
         | 
| 160 | 
            +
                - !ruby/object:Gem::Version 
         | 
| 161 | 
            +
                  hash: 3
         | 
| 162 | 
            +
                  segments: 
         | 
| 163 | 
            +
                  - 0
         | 
| 164 | 
            +
                  version: "0"
         | 
| 132 165 | 
             
            requirements: []
         | 
| 166 | 
            +
             | 
| 133 167 | 
             
            rubyforge_project: 
         | 
| 134 | 
            -
            rubygems_version: 1.8. | 
| 168 | 
            +
            rubygems_version: 1.8.24
         | 
| 135 169 | 
             
            signing_key: 
         | 
| 136 170 | 
             
            specification_version: 3
         | 
| 137 171 | 
             
            summary: HTTP should be easy
         | 
| 138 | 
            -
            test_files:
         | 
| 172 | 
            +
            test_files: 
         | 
| 139 173 | 
             
            - spec/http/compat/curb_spec.rb
         | 
| 140 174 | 
             
            - spec/http/options/body_spec.rb
         | 
| 141 175 | 
             
            - spec/http/options/callbacks_spec.rb
         | 
| @@ -146,9 +180,9 @@ test_files: | |
| 146 180 | 
             
            - spec/http/options/proxy_spec.rb
         | 
| 147 181 | 
             
            - spec/http/options/response_spec.rb
         | 
| 148 182 | 
             
            - spec/http/options_spec.rb
         | 
| 183 | 
            +
            - spec/http/request_stream_spec.rb
         | 
| 149 184 | 
             
            - spec/http/response_spec.rb
         | 
| 150 185 | 
             
            - spec/http_spec.rb
         | 
| 151 186 | 
             
            - spec/spec_helper.rb
         | 
| 152 187 | 
             
            - spec/support/example_server.rb
         | 
| 153 188 | 
             
            - spec/support/proxy_server.rb
         | 
| 154 | 
            -
            has_rdoc: 
         |