Class: WebStub::Stub
- Inherits:
-
Object
- Object
- WebStub::Stub
- Defined in:
- lib/webstub/stub.rb
Constant Summary collapse
- METHODS =
["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"].freeze
Instance Attribute Summary collapse
-
#requests ⇒ Object
Returns the value of attribute requests.
-
#response_body ⇒ Object
readonly
Returns the value of attribute response_body.
-
#response_delay ⇒ Object
readonly
Returns the value of attribute response_delay.
-
#response_headers ⇒ Object
readonly
Returns the value of attribute response_headers.
-
#response_status_code ⇒ Object
readonly
Returns the value of attribute response_status_code.
Instance Method Summary collapse
-
#initialize(method, url) ⇒ Stub
constructor
A new instance of Stub.
- #matches?(method, url, options = {}) ⇒ Boolean
- #redirects? ⇒ Boolean
- #requested? ⇒ Boolean
- #to_redirect(options) ⇒ Object
- #to_return(options) ⇒ Object
- #with(options) ⇒ Object
Constructor Details
#initialize(method, url) ⇒ Stub
Returns a new instance of Stub.
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
#requests ⇒ Object
Returns the value of attribute requests.
49 50 51 |
# File 'lib/webstub/stub.rb', line 49 def requests @requests end |
#response_body ⇒ Object (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_delay ⇒ Object (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_headers ⇒ Object (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_code ⇒ Object (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
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, ={}) if @request_url != canonicalize_url(url) return false end if @request_method != canonicalize_method(method) return false end if @request_headers headers = [:headers] || {} @request_headers.each do |key, value| if headers[key] != value return false end end end if @request_body if @request_body != [:body] return false end end true end |
#redirects? ⇒ 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
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() unless url = .delete(:url) raise ArgumentError, "to_redirect requires the :url option" end [:headers] ||= {} [:headers]["Location"] = url [:status_code] = 301 to_return() 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() if status_code = [:status_code] @response_status_code = status_code end if headers = [:headers] @response_headers.merge!(headers) end if json = [: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 = [:body] || "" if content_type = [:content_type] @response_headers["Content-Type"] = content_type end end if delay = [: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() if body = [: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 = [:headers] @request_headers = headers end self end |