Class: MockServer::ForwardChainExpectation

Inherits:
Object
  • Object
show all
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

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.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Parameters:

  • id (String)

Returns:

  • (self)


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.

Parameters:

  • priority (Integer)

Returns:

  • (self)


27
28
29
30
# File 'lib/mockserver/forward_chain_expectation.rb', line 27

def with_priority(priority)
  @expectation.priority = priority
  self
end