Sha256: a592b1ba2ac9093617a33491d3c62b592026791ccf5c97d9b7c87dd55d7c8853

Contents?: true

Size: 1.74 KB

Versions: 18

Compression:

Stored size: 1.74 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")
      allow(response).to receive(:body).and_return(data)

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

      expect(HTTParty::Request).to receive(:new).and_return(http_request)
    end

    def stub_chunked_http_response_with(chunks, options = {format: "html"})
      response = Net::HTTPResponse.new("1.1", 200, nil)
      allow(response).to receive(: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', options)
      allow(http_request).to receive_message_chain(:http, :request).and_yield(response).and_return(response)

      expect(HTTParty::Request).to receive(:new).and_return(http_request)
    end

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

      # CODE_TO_OBJ currently missing 308
      if code == '308'
        response = Net::HTTPRedirection.new("1.1", code, body)
      else
        response = Net::HTTPResponse::CODE_TO_OBJ[code].new("1.1", code, body)
      end
      allow(response).to receive(:body).and_return(body)

      allow(@http).to receive(:request).and_return(response)
      response
    end
  end
end

Version data entries

18 entries across 17 versions & 2 rubygems

Version Path
httparty-0.16.4 spec/support/stub_response.rb
httparty-0.16.3 spec/support/stub_response.rb
httparty-0.16.2 spec/support/stub_response.rb
httparty-0.16.1 spec/support/stub_response.rb
httparty-0.16.0 spec/support/stub_response.rb
httparty-0.15.7 spec/support/stub_response.rb
httparty-0.15.6 spec/support/stub_response.rb
httparty-0.15.5 spec/support/stub_response.rb
httparty-0.15.4 spec/support/stub_response.rb
httparty-0.15.3 spec/support/stub_response.rb
httparty-0.15.2 spec/support/stub_response.rb
httparty-0.15.1 spec/support/stub_response.rb
httparty-0.15.0 spec/support/stub_response.rb
httparty-0.14.0 spec/support/stub_response.rb
simplenet-client-0.2.0 ./vendor/bundle/ruby/2.0.0/gems/httparty-0.13.7/spec/support/stub_response.rb
simplenet-client-0.2.0 ./vendor/bundle/ruby/1.9.1/gems/httparty-0.13.7/spec/support/stub_response.rb
httparty-0.13.7 spec/support/stub_response.rb
httparty-0.13.6 spec/support/stub_response.rb