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_class_callback(class_callback) ⇒ Array<Expectation>
Set a forward class callback action.
-
#forward_with_delay(forward, delay) ⇒ Array<Expectation>
Set the forward action with a delay.
-
#forward_with_template(template) ⇒ Array<Expectation>
Set a forward template action.
-
#initialize(client, expectation) ⇒ ForwardChainExpectation
constructor
A new instance of ForwardChainExpectation.
-
#respond(response_or_callback) ⇒ Array<Expectation>
Set the response action.
-
#respond_with_binary(binary_response) ⇒ Array<Expectation>
Set a binary response action.
-
#respond_with_delay(response, delay) ⇒ Array<Expectation>
Set the response action with a delay.
-
#respond_with_dns(dns_response) ⇒ Array<Expectation>
Set a DNS response action.
-
#respond_with_grpc_bidi(grpc_bidi_response) ⇒ Array<Expectation>
Set a gRPC bidi streaming response action.
-
#respond_with_grpc_stream(grpc_stream_response) ⇒ Array<Expectation>
Set a gRPC stream response action.
- #respond_with_sse(sse_response) ⇒ Object
- #respond_with_websocket(websocket_response) ⇒ Object
-
#with_chaos(chaos) ⇒ self
Set a declarative HTTP chaos/fault injection profile.
-
#with_id(id) ⇒ self
Set the expectation ID.
-
#with_priority(priority) ⇒ self
Set the expectation priority.
-
#with_steps(steps) ⇒ Array<Expectation>
Set an ordered multi-action pipeline of steps.
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.
110 111 112 113 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 110 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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 74 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_class_callback(class_callback) ⇒ Array<Expectation>
Set a forward class callback action.
188 189 190 191 192 193 194 195 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 188 def forward_with_class_callback(class_callback) unless class_callback.is_a?(HttpClassCallback) raise TypeError, "Expected HttpClassCallback, got #{class_callback.class.name}" end @expectation.http_forward_class_callback = class_callback @client.upsert(@expectation) end |
#forward_with_delay(forward, delay) ⇒ Array<Expectation>
Set the forward action with a delay.
101 102 103 104 105 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 101 def forward_with_delay(forward, delay) forward.delay = delay @expectation.http_forward = forward @client.upsert(@expectation) end |
#forward_with_template(template) ⇒ Array<Expectation>
Set a forward template action.
176 177 178 179 180 181 182 183 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 176 def forward_with_template(template) unless template.is_a?(HttpTemplate) raise TypeError, "Expected HttpTemplate, got #{template.class.name}" end @expectation.http_forward_template = template @client.upsert(@expectation) end |
#respond(response_or_callback) ⇒ Array<Expectation>
Set the response action. Accepts an HttpResponse, HttpTemplate, or a Proc/lambda callback.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 44 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_binary(binary_response) ⇒ Array<Expectation>
Set a binary response action.
152 153 154 155 156 157 158 159 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 152 def respond_with_binary(binary_response) unless binary_response.is_a?(BinaryResponse) raise TypeError, "Expected BinaryResponse, got #{binary_response.class.name}" end @expectation.binary_response = binary_response @client.upsert(@expectation) end |
#respond_with_delay(response, delay) ⇒ Array<Expectation>
Set the response action with a delay.
63 64 65 66 67 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 63 def respond_with_delay(response, delay) response.delay = delay @expectation.http_response = response @client.upsert(@expectation) end |
#respond_with_dns(dns_response) ⇒ Array<Expectation>
Set a DNS response action.
164 165 166 167 168 169 170 171 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 164 def respond_with_dns(dns_response) unless dns_response.is_a?(DnsResponse) raise TypeError, "Expected DnsResponse, got #{dns_response.class.name}" end @expectation.dns_response = dns_response @client.upsert(@expectation) end |
#respond_with_grpc_bidi(grpc_bidi_response) ⇒ Array<Expectation>
Set a gRPC bidi streaming response action.
140 141 142 143 144 145 146 147 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 140 def respond_with_grpc_bidi(grpc_bidi_response) unless grpc_bidi_response.is_a?(GrpcBidiResponse) raise TypeError, "Expected GrpcBidiResponse, got #{grpc_bidi_response.class.name}" end @expectation.grpc_bidi_response = grpc_bidi_response @client.upsert(@expectation) end |
#respond_with_grpc_stream(grpc_stream_response) ⇒ Array<Expectation>
Set a gRPC stream response action.
128 129 130 131 132 133 134 135 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 128 def respond_with_grpc_stream(grpc_stream_response) unless grpc_stream_response.is_a?(GrpcStreamResponse) raise TypeError, "Expected GrpcStreamResponse, got #{grpc_stream_response.class.name}" end @expectation.grpc_stream_response = grpc_stream_response @client.upsert(@expectation) end |
#respond_with_sse(sse_response) ⇒ Object
115 116 117 118 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 115 def respond_with_sse(sse_response) @expectation.http_sse_response = sse_response @client.upsert(@expectation) end |
#respond_with_websocket(websocket_response) ⇒ Object
120 121 122 123 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 120 def respond_with_websocket(websocket_response) @expectation.http_websocket_response = websocket_response @client.upsert(@expectation) end |
#with_chaos(chaos) ⇒ self
Set a declarative HTTP chaos/fault injection profile.
35 36 37 38 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 35 def with_chaos(chaos) @expectation.chaos = chaos self 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 |
#with_steps(steps) ⇒ Array<Expectation>
Set an ordered multi-action pipeline of steps.
Exactly one step must have responder: true; that step produces the HTTP response. All other steps are side-effects executed in order.
203 204 205 206 207 208 209 210 |
# File 'lib/mockserver/forward_chain_expectation.rb', line 203 def with_steps(steps) unless steps.is_a?(Array) && steps.all? { |s| s.is_a?(ExpectationStep) } raise TypeError, 'Expected an Array of ExpectationStep objects' end @expectation.steps = steps @client.upsert(@expectation) end |