File: webmock_helper.rb

package info (click to toggle)
ruby-openid-connect 2.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 528 kB
  • sloc: ruby: 3,002; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,188 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'webmock/rspec'

module WebMockHelper
  def mock_json(method, endpoint, response_file, options = {})
    stub_request(method, endpoint).with(
      request_for(method, options)
    ).to_return(
      response_for(response_file, options)
    )
    result = yield
    a_request(method, endpoint).with(
      request_for(method, options)
    ).should have_been_made.once
    result
  end

  private

  def request_for(method, options = {})
    request = {}
    case method
    when :post, :put
      request[:body] = options[:params]
    else
      request[:query] = options[: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!