Sha256: 472f0a8e95abe186abef7f5b990c33354714c50138b02049606803b328ec3a11

Contents?: true

Size: 1.8 KB

Versions: 21

Compression:

Stored size: 1.8 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(request, 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"

        # 5.0.0 had a breaking change to require request instead of uri.
        if HTTP::VERSION < '5.0.0'
          return new({
            status: status,
            version: "1.1",
            headers: headers,
            body: body,
            uri: uri
          })
        end

        new({
          status: status,
          version: "1.1",
          headers: headers,
          body: body,
          request: request,
        })
      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

21 entries across 21 versions & 2 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
webmock-3.14.0 lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/lib/webmock/http_lib_adapters/http_rb/response.rb