Sha256: 04b1e8fe17f5374c74e336f245d289a46a5d4444105d110058b6b9b185e3480e

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

require "rack"

module ShamRack

  # A simple Rack app that stubs out a web service, for testing.
  class StubWebService

    def initialize
      @handlers = []
    end

    def last_request
      @request
    end

    def call(env)
      @request = Rack::Request.new(env)
      @handlers.each do |handler|
        response = handler.call(@request)
        return response if response
      end
      return default_response
    end

    def handle(&block)
      @handlers.unshift(block)
    end

    def register_resource(path, content, content_type = "application/xml", status = 200)
      handle do |request|
        request_path = request.path_info
        unless request.query_string.to_s.empty?
          request_path += "?" + request.query_string
        end
        [status, {"Content-Type" => content_type}, content] if request_path == path
      end
    end

    def reset
      @handlers.clear
    end

    protected

    def default_response
      [404, {"Content-Type" => "text/plain"}, "Not found"]
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sham_rack-1.3.1 lib/sham_rack/stub_web_service.rb
sham_rack-1.3.0 lib/sham_rack/stub_web_service.rb