Sha256: e5bea96999e8f8874cf3a637b8a2d6882159e6c7bd096fc7593ca72a975eb3e5

Contents?: true

Size: 1.45 KB

Versions: 54

Compression:

Stored size: 1.45 KB

Contents

module HTTParty
  module StubResponse
    def stub_http_response_with(filename)
      format = filename.split('.').last.intern
      data = file_fixture(filename)

      response = Net::HTTPOK.new("1.1", 200, "Content for you")
      response.stub!(:body).and_return(data)

      http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => format)
      http_request.stub_chain(:http, :request).and_return(response)

      HTTParty::Request.should_receive(:new).and_return(http_request)
    end

    def stub_chunked_http_response_with(chunks)
      response = Net::HTTPResponse.new("1.1", 200, nil)
      response.stub(:chunked_data).and_return(chunks)
      def response.read_body(&block)
        @body || chunked_data.each(&block)
      end

      http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => "html")
      http_request.stub_chain(:http, :request).and_yield(response).and_return(response)

      HTTParty::Request.should_receive(:new).and_return(http_request)
    end

    def stub_response(body, code = 200)
      @request.options[:base_uri] ||= 'http://localhost'
      unless defined?(@http) && @http
        @http = Net::HTTP.new('localhost', 80)
        @request.stub!(:http).and_return(@http)
      end

      response = Net::HTTPResponse::CODE_TO_OBJ[code.to_s].new("1.1", code, body)
      response.stub!(:body).and_return(body)

      @http.stub!(:request).and_return(response)
      response
    end
  end
end

Version data entries

54 entries across 53 versions & 7 rubygems

Version Path
alloy_sdk-0.1.0 vendor/bundle/ruby/3.0.0/gems/httparty-0.13.1/spec/support/stub_response.rb
alloy_sdk-0.1.0 vendor/bundle/ruby/2.6.0/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.9 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.8 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.7 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.6 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.5 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.4 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.3 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.2 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
allegro_api_client-0.0.1 gems/ruby/2.1.0/gems/httparty-0.13.1/spec/support/stub_response.rb
httparty-0.13.1 spec/support/stub_response.rb
yoyle439587298-0.13.0 spec/support/stub_response.rb
plyom_user-0.3.1 vendor/bundle/ruby/2.0.0/gems/httparty-0.11.0/spec/support/stub_response.rb
plyom_user-0.3.0 vendor/bundle/ruby/2.0.0/gems/httparty-0.11.0/spec/support/stub_response.rb
plyom_user-0.2.9 vendor/bundle/ruby/2.0.0/gems/httparty-0.11.0/spec/support/stub_response.rb
plyom_user-0.2.8 vendor/bundle/ruby/2.0.0/gems/httparty-0.11.0/spec/support/stub_response.rb
httparty-0.13.0 spec/support/stub_response.rb
plyom_user-0.2.7 vendor/bundle/ruby/2.0.0/gems/httparty-0.11.0/spec/support/stub_response.rb