Sha256: b7d84c49b3d861ecbcca12854de3996a5300a320bc3a364da090510551992ffa

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

# typed: true

require 'ddtrace/transport/response'

module Datadog
  module Transport
    module HTTP
      module Adapters
        # Adapter for testing
        class Test
          attr_reader \
            :buffer,
            :status

          # @param buffer [Array] an optional array that will capture all spans sent to this adapter, defaults to +nil+
          # @deprecated Positional parameters are deprecated. Use named parameters instead.
          def initialize(buffer = nil, **options)
            @buffer = buffer || options[:buffer]
            @mutex = Mutex.new
            @status = 200
          end

          def call(env)
            add_request(env)
            Response.new(status)
          end

          def buffer?
            !@buffer.nil?
          end

          def add_request(env)
            @mutex.synchronize { buffer << env } if buffer?
          end

          def set_status!(status)
            @status = status
          end

          def url; end

          # Response for test adapter
          class Response
            include Datadog::Transport::Response

            attr_reader \
              :body,
              :code

            def initialize(code, body = nil)
              @code = code
              @body = body
            end

            def payload
              @body
            end

            def ok?
              code.between?(200, 299)
            end

            def unsupported?
              code == 415
            end

            def not_found?
              code == 404
            end

            def client_error?
              code.between?(400, 499)
            end

            def server_error?
              code.between?(500, 599)
            end

            def inspect
              "#{super}, code:#{code}"
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ddtrace-1.2.0 lib/ddtrace/transport/http/adapters/test.rb
ddtrace-1.1.0 lib/ddtrace/transport/http/adapters/test.rb
ddtrace-1.0.0 lib/ddtrace/transport/http/adapters/test.rb
ddtrace-1.0.0.beta2 lib/ddtrace/transport/http/adapters/test.rb