Class: CapybaraMock::Stub

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara_mock/stub.rb

Overview

Stub DSL

Instance Method Summary collapse

Constructor Details

#initialize(method, url) ⇒ Stub

Returns a new instance of Stub.



9
10
11
12
13
14
# File 'lib/capybara_mock/stub.rb', line 9

def initialize(method, url)
  @method = method.to_s.upcase
  @url = url
  with
  to_return
end

Instance Method Details

#call(request) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/capybara_mock/stub.rb', line 28

def call(request)
  if @response.respond_to?(:call)
    @response.call(**request)
  else
    @response
  end
end

#match?(request) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/capybara_mock/stub.rb', line 36

def match?(request)
  match_method?(request) &&
    match_url?(request) &&
    match_query?(request) &&
    match_headers?(request) &&
    match_body?(request)
end

#match_body?(request) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/capybara_mock/stub.rb', line 64

def match_body?(request)
  return true unless @body

  decode_body(@body) == decode_body(request[:body])
end

#match_headers?(request) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/capybara_mock/stub.rb', line 60

def match_headers?(request)
  @headers == request[:headers].slice(*@headers.keys)
end

#match_method?(request) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/capybara_mock/stub.rb', line 44

def match_method?(request)
  @method == request[:method]
end

#match_query?(request) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/capybara_mock/stub.rb', line 56

def match_query?(request)
  @query == request[:query].slice(*@query.keys)
end

#match_url?(request) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/capybara_mock/stub.rb', line 48

def match_url?(request)
  if @url.is_a?(Regexp)
    @url.match?(request[:url])
  else
    @url == request[:url]
  end
end

#to_return(status: 200, headers: {}, body: '', &block) ⇒ Object



23
24
25
26
# File 'lib/capybara_mock/stub.rb', line 23

def to_return(status: 200, headers: {}, body: '', &block)
  @response = block || [status, headers, body]
  self
end

#with(query: {}, headers: {}, body: nil) ⇒ Object



16
17
18
19
20
21
# File 'lib/capybara_mock/stub.rb', line 16

def with(query: {}, headers: {}, body: nil)
  @query = query
  @headers = headers
  @body = body
  self
end