Sha256: 8d523ad8a4476f9dbf64b2695b873bdd8d4a2c566b4a70b4478d06b12a0acb22

Contents?: true

Size: 1.5 KB

Versions: 17

Compression:

Stored size: 1.5 KB

Contents

module HTTP
  class Response
    def to_webmock
      webmock_response = ::WebMock::Response.new

      webmock_response.status  = [status.to_i, reason]
      webmock_response.body    = body.to_s
      webmock_response.headers = headers.to_h

      webmock_response
    end

    class << self
      def from_webmock(webmock_response, request_signature = nil)
        status  = Status.new(webmock_response.status.first)
        headers = webmock_response.headers || {}
        uri     = normalize_uri(request_signature && request_signature.uri)

        # HTTP.rb 3.0+ uses a keyword argument to pass the encoding, but 1.x
        # and 2.x use a positional argument, and 0.x don't support supplying
        # the encoding.
        body = if HTTP::VERSION < "1.0.0"
          Body.new(Streamer.new(webmock_response.body))
        elsif HTTP::VERSION < "3.0.0"
          Body.new(Streamer.new(webmock_response.body), webmock_response.body.encoding)
        else
          Body.new(Streamer.new(webmock_response.body), encoding: webmock_response.body.encoding)
        end

        return new(status, "1.1", headers, body, uri) if HTTP::VERSION < "1.0.0"

        new({
          status: status,
          version: "1.1",
          headers: headers,
          body: body,
          uri: uri
        })
      end

      private

      def normalize_uri(uri)
        return unless uri

        uri = Addressable::URI.parse uri
        uri.port = nil if uri.default_port && uri.port == uri.default_port

        uri
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
webmock-3.12.2 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.12.1 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.12.0 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.11.3 lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-newrelic-1.2.0 vendor/bundle/jruby/2.5.0/gems/webmock-3.11.2/lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.11.2 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.11.1 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.11.0 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.10.0 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.9.5 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.9.4 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.9.3 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.9.2 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.9.1 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.9.0 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.8.3 lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.8.2 lib/webmock/http_lib_adapters/http_rb/response.rb