Class: MockServer::ForwardChainExpectation
- Inherits:
-
Object
- Object
- MockServer::ForwardChainExpectation
- Defined in:
- lib/mockserver/forward_chain_expectation.rb
Overview
Fluent API for building expectations via the when method.
Returned by Client#when to allow chaining:
client.when(request).respond(response)
client.when(request).forward(forward)
client.when(request).error(error)
Instance Method Summary collapse
-
#error(error) ⇒ Array<Expectation>
Set the error action.
-
#forward(forward_or_callback, response_callback = nil) ⇒ Array<Expectation>
Set the forward action.
-
#forward_with_delay(forward, delay) ⇒ Array<Expectation>
Set the forward action with a delay.
-
#initialize(client, expectation) ⇒ ForwardChainExpectation
constructor
A new instance of ForwardChainExpectation.
-
#respond(response_or_callback) ⇒ Array<Expectation>
Set the response action.
-
#respond_with_delay(response, delay) ⇒ Array<Expectation>
Set the response action with a delay.
- #respond_with_sse(sse_response) ⇒ Object
- #respond_with_websocket(websocket_response) ⇒ Object
-
#with_id(id) ⇒ self
Set the expectation ID.
-
#with_priority(priority) ⇒ self
Set the expectation priority.
Constructor Details
#initialize(client, expectation) ⇒ ForwardChainExpectation
Returns a new instance of ForwardChainExpectation.
11 12 13 14 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 11 def initialize(client, expectation) @client = client @expectation = expectation end |
Instance Method Details
#error(error) ⇒ Array<Expectation>
Set the error action.
102 103 104 105 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 102 def error(error) @expectation.http_error = error @client.upsert(@expectation) end |
#forward(forward_or_callback, response_callback = nil) ⇒ Array<Expectation>
Set the forward action. Accepts an HttpForward, HttpOverrideForwardedRequest, HttpTemplate, or a Proc/lambda callback.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 66 def forward(forward_or_callback, response_callback = nil) if forward_or_callback.respond_to?(:call) client_id = @client.send( :register_websocket_callback, 'forward', forward_or_callback, response_callback ) obj_callback = HttpObjectCallback.new(client_id: client_id) obj_callback.response_callback = true if response_callback @expectation.http_forward_object_callback = obj_callback elsif forward_or_callback.is_a?(HttpForward) @expectation.http_forward = forward_or_callback elsif forward_or_callback.is_a?(HttpOverrideForwardedRequest) @expectation.http_override_forwarded_request = forward_or_callback elsif forward_or_callback.is_a?(HttpTemplate) @expectation.http_forward_template = forward_or_callback else raise TypeError, "Expected HttpForward, HttpOverrideForwardedRequest, HttpTemplate, or callable, " \ "got #{forward_or_callback.class.name}" end @client.upsert(@expectation) end |
#forward_with_delay(forward, delay) ⇒ Array<Expectation>
Set the forward action with a delay.
93 94 95 96 97 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 93 def forward_with_delay(forward, delay) forward.delay = delay @expectation.http_forward = forward @client.upsert(@expectation) end |
#respond(response_or_callback) ⇒ Array<Expectation>
Set the response action. Accepts an HttpResponse, HttpTemplate, or a Proc/lambda callback.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 36 def respond(response_or_callback) if response_or_callback.respond_to?(:call) client_id = @client.send(:register_websocket_callback, 'response', response_or_callback) @expectation.http_response_object_callback = HttpObjectCallback.new(client_id: client_id) elsif response_or_callback.is_a?(HttpResponse) @expectation.http_response = response_or_callback elsif response_or_callback.is_a?(HttpTemplate) @expectation.http_response_template = response_or_callback else raise TypeError, "Expected HttpResponse, HttpTemplate, or callable, got #{response_or_callback.class.name}" end @client.upsert(@expectation) end |
#respond_with_delay(response, delay) ⇒ Array<Expectation>
Set the response action with a delay.
55 56 57 58 59 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 55 def respond_with_delay(response, delay) response.delay = delay @expectation.http_response = response @client.upsert(@expectation) end |
#respond_with_sse(sse_response) ⇒ Object
107 108 109 110 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 107 def respond_with_sse(sse_response) @expectation.http_sse_response = sse_response @client.upsert(@expectation) end |
#respond_with_websocket(websocket_response) ⇒ Object
112 113 114 115 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 112 def respond_with_websocket(websocket_response) @expectation.http_websocket_response = websocket_response @client.upsert(@expectation) end |
#with_id(id) ⇒ self
Set the expectation ID.
19 20 21 22 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 19 def with_id(id) @expectation.id = id self end |
#with_priority(priority) ⇒ self
Set the expectation priority.
27 28 29 30 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 27 def with_priority(priority) @expectation.priority = priority self end |