Sha256: ec74d451903f57c6d4950afda8f35deba2c320cd79a54a77a96d7d59df95e6e5

Contents?: true

Size: 1.15 KB

Versions: 20

Compression:

Stored size: 1.15 KB

Contents

require 'billy/handlers/handler'
require 'addressable/uri'

module Billy
  class StubHandler
    include Handler

    def handle_request(method, url, headers, body)
      if handles_request?(method, url, headers, body)
        if (stub = find_stub(method, url))
          query_string = Addressable::URI.parse(url).query || ''
          params = CGI.parse(query_string)
          stub.call(method, url, params, headers, body).tap do |response|
            Billy.log(:info, "puffing-billy: STUB #{method} for '#{url}'")
            return { status: response[0], headers: response[1], content: response[2] }
          end
        end
      end

      nil
    end

    def handles_request?(method, url, _headers, _body)
      !find_stub(method, url).nil?
    end

    def reset
      self.stubs = []
    end

    def stub(url, options = {})
      new_stub = ProxyRequestStub.new(url, options)
      stubs.unshift new_stub
      new_stub
    end

    def unstub(stub)
      stubs.delete stub
    end

    def stubs
      @stubs ||= []
    end

    private

    attr_writer :stubs

    def find_stub(method, url)
      stubs.find { |stub| stub.matches?(method, url) }
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
puffing-billy-4.0.0 lib/billy/handlers/stub_handler.rb
puffing-billy-3.2.0 lib/billy/handlers/stub_handler.rb
puffing-billy-3.1.0 lib/billy/handlers/stub_handler.rb
puffing-billy-3.0.4 lib/billy/handlers/stub_handler.rb
puffing-billy-3.0.3 lib/billy/handlers/stub_handler.rb
puffing-billy-3.0.2 lib/billy/handlers/stub_handler.rb
puffing-billy-3.0.1 lib/billy/handlers/stub_handler.rb
puffing-billy-3.0.0 lib/billy/handlers/stub_handler.rb
puffing-billy-2.4.1 lib/billy/handlers/stub_handler.rb
puffing-billy-2.4.0 lib/billy/handlers/stub_handler.rb
puffing-billy-2.3.1 lib/billy/handlers/stub_handler.rb
puffing-billy-2.3.0 lib/billy/handlers/stub_handler.rb
puffing-billy-2.2.0 lib/billy/handlers/stub_handler.rb
puffing-billy-2.1.1 lib/billy/handlers/stub_handler.rb
puffing-billy-2.1.0 lib/billy/handlers/stub_handler.rb
puffing-billy-2.0.0 lib/billy/handlers/stub_handler.rb
puffing-billy-1.1.3 lib/billy/handlers/stub_handler.rb
puffing-billy-1.1.2 lib/billy/handlers/stub_handler.rb
puffing-billy-1.1.1 lib/billy/handlers/stub_handler.rb
puffing-billy-1.1.0 lib/billy/handlers/stub_handler.rb