Class: WebStub::Stub

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

Constant Summary collapse

METHODS =
["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url) ⇒ Stub

Returns a new instance of Stub.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/webstub/stub.rb', line 5

def initialize(method, url)
  @request_method = canonicalize_method(method)
  raise ArgumentError, "invalid method name" unless METHODS.include? @request_method

  @requests = 0
  
  @request_url = canonicalize_url(url)
  @request_headers = nil
  @request_body = nil

  @response_body = ""
  @response_delay = 0.0
  @response_headers = {}
  @response_status_code = 200
end

Instance Attribute Details

#requestsObject

Returns the value of attribute requests.



49
50
51
# File 'lib/webstub/stub.rb', line 49

def requests
  @requests
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



59
60
61
# File 'lib/webstub/stub.rb', line 59

def response_body
  @response_body
end

#response_delayObject (readonly)

Returns the value of attribute response_delay.



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

def response_delay
  @response_delay
end

#response_headersObject (readonly)

Returns the value of attribute response_headers.



61
62
63
# File 'lib/webstub/stub.rb', line 61

def response_headers
  @response_headers
end

#response_status_codeObject (readonly)

Returns the value of attribute response_status_code.



62
63
64
# File 'lib/webstub/stub.rb', line 62

def response_status_code
  @response_status_code
end

Instance Method Details

#matches?(method, url, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/webstub/stub.rb', line 21

def matches?(method, url, options={})
  if @request_url != canonicalize_url(url)
    return false
  end

  if @request_method != canonicalize_method(method)
    return false
  end

  if @request_headers
    headers = options[:headers] || {}

    @request_headers.each do |key, value|
      if headers[key] != value
        return false
      end
    end
  end

  if @request_body
    if @request_body != options[:body]
      return false
    end
  end

  true
end

#redirects?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/webstub/stub.rb', line 51

def redirects?
  @response_status_code.between?(300, 399) && @response_headers["Location"] != nil
end

#requested?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/webstub/stub.rb', line 55

def requested?
  @requests > 0
end

#to_redirect(options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/webstub/stub.rb', line 95

def to_redirect(options)
  unless url = options.delete(:url)
    raise ArgumentError, "to_redirect requires the :url option"
  end

  options[:headers] ||= {}
  options[:headers]["Location"] = url
  options[:status_code] = 301

  to_return(options)
end

#to_return(options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/webstub/stub.rb', line 64

def to_return(options)
  if status_code = options[:status_code]
    @response_status_code = status_code
  end

  if headers = options[:headers]
    @response_headers.merge!(headers)
  end

  if json = options[:json]
    @response_body = json
    @response_headers["Content-Type"] = "application/json"

    if @response_body.is_a?(Hash) || @response_body.is_a?(Array)
      @response_body = JSON.generate(@response_body)
    end
  else
    @response_body = options[:body] || ""

    if content_type = options[:content_type]
      @response_headers["Content-Type"] = content_type
    end
  end

  if delay = options[:delay]
    @response_delay = delay
  end

  self
end

#with(options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/webstub/stub.rb', line 107

def with(options)
  if body = options[:body]
    @request_body = body

    if @request_body.is_a?(Hash)
      @request_body = @request_body.inject({}) { |h, (k,v)| h[k.to_s] = v; h }
    end
  end

  if headers = options[:headers]
    @request_headers = headers
  end

  self
end