Sha256: 7b658b8ac695c923f94b8af3c95cfce6c0c0edc3af7065deba49bd51c1eef0ce

Contents?: true

Size: 1.01 KB

Versions: 7

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

7 entries across 7 versions & 1 rubygems

Version Path
sham_rack-1.4.1 lib/sham_rack/stub_web_service.rb
sham_rack-1.4.0 lib/sham_rack/stub_web_service.rb
sham_rack-1.3.6 lib/sham_rack/stub_web_service.rb
sham_rack-1.3.5 lib/sham_rack/stub_web_service.rb
sham_rack-1.3.4 lib/sham_rack/stub_web_service.rb
sham_rack-1.3.3 lib/sham_rack/stub_web_service.rb
sham_rack-1.3.2 lib/sham_rack/stub_web_service.rb