Sha256: 746cb18baa18e774e319ea1a7285d61d8377cbd0e25d2403c2dd332868513550

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'webmock/rspec'

module WebMockHelper
  def mock_response(method, endpoint, response_file, options = {})
    stub_request(method, endpoint).with(
      request_for(method, options)
    ).to_return(
      response_for(response_file, options)
    )
  end

  private

  def request_for(method, options = {})
    request = {}
    params = options&.[](:params) || {}
    case method
    when :post, :put, :delete
      request[:body] = params
    else
      request[:query] = params
    end
    if options[:request_header]
      request[:headers] = options[:request_header]
    end
    request
  end

  def response_for(response_file, options = {})
    response = {}
    format = options[:format] || :json
    if format == :json
      response[:headers] = {
        'Content-Type': 'application/json'
      }
    end
    response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', "#{response_file}.#{format}"))
    if options[:status]
      response[:status] = options[:status]
    end
    response
  end
end

include WebMockHelper
WebMock.disable_net_connect!

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-oauth2-2.2.1 spec/helpers/webmock_helper.rb
rack-oauth2-2.2.0 spec/helpers/webmock_helper.rb