Class: MockServer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mockserver/client.rb

Overview

Synchronous MockServer client.

Provides the full MockServer REST API plus a fluent builder DSL and WebSocket-based object callback support.

Examples:

Basic usage

client = MockServer::Client.new('localhost', 1080)
client.when(
  HttpRequest.request(path: '/hello')
).respond(
  HttpResponse.response(body: 'world')
)
client.close

Block form (auto-close)

MockServer::Client.new('localhost', 1080) do |c|
  c.when(HttpRequest.request(path: '/hello'))
   .respond(HttpResponse.response(body: 'world'))
end

Constant Summary collapse

HTTP_TIMEOUT =

seconds, matching Python client

60

Instance Method Summary collapse

Constructor Details

#initialize(host, port, context_path: '', secure: false, ca_cert_path: nil, tls_verify: true) ⇒ Client

Returns a new instance of Client.

Parameters:

  • host (String)
  • port (Integer)
  • context_path (String) (defaults to: '')
  • secure (Boolean) (defaults to: false)
  • ca_cert_path (String, nil) (defaults to: nil)
  • tls_verify (Boolean) (defaults to: true)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mockserver/client.rb', line 37

def initialize(host, port, context_path: '', secure: false,
               ca_cert_path: nil, tls_verify: true)
  @host = host
  @port = port
  @context_path = context_path
  @secure = secure
  @ca_cert_path = ca_cert_path
  @tls_verify = tls_verify
  @websocket_clients = []
  @websocket_mutex = Mutex.new

  scheme = secure ? 'https' : 'http'
  ctx_path = ''
  if context_path && !context_path.empty?
    ctx_path = context_path.start_with?('/') ? context_path : "/#{context_path}"
  end
  @base_url = "#{scheme}://#{host}:#{port}#{ctx_path}"

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Method Details

#add_breakpoint(matcher, phases, request_handler: nil, response_handler: nil, stream_frame_handler: nil) ⇒ String

Register a breakpoint matcher with callback handlers. The callback WebSocket is opened lazily and reused.

Parameters:

  • matcher (HttpRequest)

    the request definition to match

  • phases (Array<String>)

    e.g. [“REQUEST”, “RESPONSE”]

  • request_handler (Proc, nil) (defaults to: nil)

    handler for REQUEST phase

  • response_handler (Proc, nil) (defaults to: nil)

    handler for RESPONSE phase

  • stream_frame_handler (Proc, nil) (defaults to: nil)

    handler for streaming phases

Returns:

  • (String)

    the server-assigned breakpoint matcher id

Raises:

  • (ArgumentError)


497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/mockserver/client.rb', line 497

def add_breakpoint(matcher, phases,
                   request_handler: nil, response_handler: nil,
                   stream_frame_handler: nil)
  raise ArgumentError, 'add_breakpoint requires a non-nil matcher' if matcher.nil?
  raise ArgumentError, 'add_breakpoint requires a non-empty phases array' if phases.nil? || phases.empty?

  ws_client = ensure_breakpoint_websocket
  client_id = ws_client.client_id

  body = JSON.generate({
    'httpRequest' => matcher.to_h,
    'phases' => phases,
    'clientId' => client_id
  })
  status, response_body = request('PUT', '/mockserver/breakpoint/matcher', body)
  if status >= 400
    raise Error, "Failed to register breakpoint matcher (status=#{status}): #{response_body}"
  end

  parsed = response_body && !response_body.empty? ? JSON.parse(response_body) : {}
  breakpoint_id = parsed['id']
  raise Error, 'Server did not return a breakpoint id' unless breakpoint_id

  # Install per-breakpoint-id handlers
  ws_client.set_breakpoint_request_handler(breakpoint_id, request_handler) if request_handler
  ws_client.set_breakpoint_response_handler(breakpoint_id, response_handler) if response_handler
  ws_client.set_breakpoint_stream_frame_handler(breakpoint_id, stream_frame_handler) if stream_frame_handler

  breakpoint_id
end

#add_request_and_response_breakpoint(matcher, request_handler, response_handler) ⇒ String

Convenience: register a REQUEST+RESPONSE breakpoint.

Parameters:

  • matcher (HttpRequest)
  • request_handler (Proc)
  • response_handler (Proc)

Returns:

  • (String)


541
542
543
544
545
# File 'lib/mockserver/client.rb', line 541

def add_request_and_response_breakpoint(matcher, request_handler, response_handler)
  add_breakpoint(matcher, %w[REQUEST RESPONSE],
                 request_handler: request_handler,
                 response_handler: response_handler)
end

#add_request_breakpoint(matcher, request_handler) ⇒ String

Convenience: register a REQUEST-only breakpoint.

Parameters:

Returns:

  • (String)


532
533
534
# File 'lib/mockserver/client.rb', line 532

def add_request_breakpoint(matcher, request_handler)
  add_breakpoint(matcher, ['REQUEST'], request_handler: request_handler)
end

#advance_clock(duration_millis) ⇒ Hash

Advance the frozen clock by duration_millis milliseconds.

Parameters:

  • duration_millis (Integer)

Returns:

  • (Hash)

    response with status, currentInstant, currentEpochMillis



174
175
176
177
178
179
180
181
182
# File 'lib/mockserver/client.rb', line 174

def advance_clock(duration_millis)
  body = JSON.generate({ 'action' => 'advance', 'durationMillis' => duration_millis })
  status, response_body = request('PUT', '/mockserver/clock', body)
  if status >= 400
    raise Error, "Failed to advance clock (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#bind(*ports) ⇒ Array<Integer>

Bind additional ports.

Parameters:

  • ports (Array<Integer>)

Returns:

  • (Array<Integer>)


420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/mockserver/client.rb', line 420

def bind(*ports)
  body = JSON.generate(Ports.new(ports: ports.flatten).to_h)
  status, response_body = request('PUT', '/mockserver/bind', body)
  if status >= 400
    raise Error, "Failed to bind ports (status=#{status}): #{response_body}"
  end

  if response_body && !response_body.empty?
    parsed = JSON.parse(response_body)
    return Ports.from_hash(parsed).ports
  end
  []
end

#clear(request = nil, type: nil) ⇒ nil

Clear expectations and/or logs.

Parameters:

  • request (HttpRequest, nil) (defaults to: nil)
  • type (String, nil) (defaults to: nil)

    “EXPECTATIONS”, “LOG”, or “ALL”

Returns:

  • (nil)


106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mockserver/client.rb', line 106

def clear(request = nil, type: nil)
  query_params = {}
  query_params['type'] = type if type
  body = request ? JSON.generate(request.to_h) : ''
  status, response_body = do_request(
    'PUT', '/mockserver/clear', body, query_params.empty? ? nil : query_params
  )
  if status >= 400
    raise Error, "Failed to clear (status=#{status}): #{response_body}"
  end

  nil
end

#clear_breakpoint_matchersHash

Clear all registered breakpoint matchers.

Returns:

  • (Hash)


582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/mockserver/client.rb', line 582

def clear_breakpoint_matchers
  status, response_body = request('PUT', '/mockserver/breakpoint/matcher/clear')
  if status >= 400
    raise Error, "Failed to clear breakpoint matchers (status=#{status}): #{response_body}"
  end

  # Clear client-side handlers
  @websocket_mutex.synchronize do
    @websocket_clients.each do |ws|
      ws.clear_breakpoint_handlers if ws.respond_to?(:clear_breakpoint_handlers)
    end
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#clear_by_id(expectation_id, type: nil) ⇒ nil

Clear by expectation ID.

Parameters:

  • expectation_id (String)
  • type (String, nil) (defaults to: nil)

Returns:

  • (nil)


124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mockserver/client.rb', line 124

def clear_by_id(expectation_id, type: nil)
  query_params = {}
  query_params['type'] = type if type
  body = JSON.generate({ 'id' => expectation_id })
  status, response_body = do_request(
    'PUT', '/mockserver/clear', body, query_params.empty? ? nil : query_params
  )
  if status >= 400
    raise Error, "Failed to clear by id (status=#{status}): #{response_body}"
  end

  nil
end

#clear_service_chaosHash

Clear all service-scoped chaos profiles.

Returns:

  • (Hash)


242
243
244
245
246
247
248
249
250
# File 'lib/mockserver/client.rb', line 242

def clear_service_chaos
  body = JSON.generate({ 'clear' => true })
  status, response_body = request('PUT', '/mockserver/serviceChaos', body)
  if status >= 400
    raise Error, "Failed to clear service chaos (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#clock_statusHash

Query the current clock status.

Returns:

  • (Hash)

    with currentInstant, currentEpochMillis, frozen



198
199
200
201
202
203
204
205
# File 'lib/mockserver/client.rb', line 198

def clock_status
  status, response_body = request('GET', '/mockserver/clock')
  if status >= 400
    raise Error, "Failed to get clock status (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#closenil

Close all WebSocket connections.

Returns:

  • (nil)


642
643
644
645
646
647
648
# File 'lib/mockserver/client.rb', line 642

def close
  @websocket_mutex.synchronize do
    @websocket_clients.each(&:close)
    @websocket_clients.clear
  end
  nil
end

#freeze_clock(instant = nil) ⇒ Hash

Freeze the server clock at the given ISO-8601 instant. If instant is nil, the clock freezes at the current real time.

Parameters:

  • instant (String, nil) (defaults to: nil)

    ISO-8601 instant (e.g. “2025-01-15T09:30:00Z”)

Returns:

  • (Hash)

    response with status, currentInstant, currentEpochMillis



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/mockserver/client.rb', line 159

def freeze_clock(instant = nil)
  payload = { 'action' => 'freeze' }
  payload['instant'] = instant if instant
  body = JSON.generate(payload)
  status, response_body = request('PUT', '/mockserver/clock', body)
  if status >= 400
    raise Error, "Failed to freeze clock (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#has_started?(attempts: 10, timeout: 0.5) ⇒ Boolean Also known as: has_started

Check if MockServer has started.

Parameters:

  • attempts (Integer) (defaults to: 10)
  • timeout (Float) (defaults to: 0.5)

    seconds between attempts

Returns:

  • (Boolean)


449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/mockserver/client.rb', line 449

def has_started?(attempts: 10, timeout: 0.5)
  attempts.times do |i|
    begin
      status, = request('PUT', '/mockserver/status')
      return true if status == 200
    rescue ConnectionError
      # not yet started
    end
    sleep(timeout) if i < attempts - 1
  end
  false
end

#list_breakpoint_matchersHash

List all registered breakpoint matchers.

Returns:

  • (Hash)

    e.g. => [{…, …]}



549
550
551
552
553
554
555
556
# File 'lib/mockserver/client.rb', line 549

def list_breakpoint_matchers
  status, response_body = request('GET', '/mockserver/breakpoint/matchers')
  if status >= 400
    raise Error, "Failed to list breakpoint matchers (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#mock_with_callback(request, callback, times: nil, time_to_live: nil) ⇒ Array<Expectation>

Register a response callback via WebSocket.

Parameters:

Returns:



608
609
610
611
612
613
614
615
616
617
# File 'lib/mockserver/client.rb', line 608

def mock_with_callback(request, callback, times: nil, time_to_live: nil)
  client_id = register_websocket_callback('response', callback)
  expectation = Expectation.new(
    http_request: request,
    http_response_object_callback: HttpObjectCallback.new(client_id: client_id),
    times: times,
    time_to_live: time_to_live
  )
  upsert(expectation)
end

#mock_with_forward_callback(request, forward_callback, response_callback = nil, times: nil, time_to_live: nil) ⇒ Array<Expectation>

Register a forward callback via WebSocket.

Parameters:

  • request (HttpRequest)
  • forward_callback (Proc)
  • response_callback (Proc, nil) (defaults to: nil)
  • times (Times, nil) (defaults to: nil)
  • time_to_live (TimeToLive, nil) (defaults to: nil)

Returns:



626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/mockserver/client.rb', line 626

def mock_with_forward_callback(request, forward_callback, response_callback = nil,
                                times: nil, time_to_live: nil)
  client_id = register_websocket_callback('forward', forward_callback, response_callback)
  obj_callback = HttpObjectCallback.new(client_id: client_id)
  obj_callback.response_callback = true if response_callback
  expectation = Expectation.new(
    http_request: request,
    http_forward_object_callback: obj_callback,
    times: times,
    time_to_live: time_to_live
  )
  upsert(expectation)
end

#open_api_expectation(expectation) ⇒ nil

Create an OpenAPI expectation.

Parameters:

Returns:

  • (nil)


92
93
94
95
96
97
98
99
100
# File 'lib/mockserver/client.rb', line 92

def open_api_expectation(expectation)
  body = JSON.generate(expectation.to_h)
  status, response_body = request('PUT', '/mockserver/openapi', body)
  if status >= 400
    raise Error, "Failed to create OpenAPI expectation (status=#{status}): #{response_body}"
  end

  nil
end

#remove_breakpoint_matcher(breakpoint_id) ⇒ Hash

Remove a breakpoint matcher by id.

Parameters:

  • breakpoint_id (String)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/mockserver/client.rb', line 561

def remove_breakpoint_matcher(breakpoint_id)
  raise ArgumentError, 'remove_breakpoint_matcher requires a non-empty id' if breakpoint_id.nil? || breakpoint_id.empty?

  body = JSON.generate({ 'id' => breakpoint_id })
  status, response_body = request('PUT', '/mockserver/breakpoint/matcher/remove', body)
  if status >= 400
    raise Error, "Failed to remove breakpoint matcher (status=#{status}): #{response_body}"
  end

  # Remove client-side handlers
  @websocket_mutex.synchronize do
    @websocket_clients.each do |ws|
      ws.remove_breakpoint_handlers(breakpoint_id) if ws.respond_to?(:remove_breakpoint_handlers)
    end
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#remove_service_chaos(host) ⇒ Hash

Remove the service-scoped chaos profile registered for host.

Parameters:

  • host (String)

Returns:

  • (Hash)


230
231
232
233
234
235
236
237
238
# File 'lib/mockserver/client.rb', line 230

def remove_service_chaos(host)
  body = JSON.generate({ 'host' => host, 'remove' => true })
  status, response_body = request('PUT', '/mockserver/serviceChaos', body)
  if status >= 400
    raise Error, "Failed to remove service chaos (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#resetnil

Reset all expectations and logs.

Returns:

  • (nil)


140
141
142
143
144
145
146
147
148
149
# File 'lib/mockserver/client.rb', line 140

def reset
  status, response_body = request('PUT', '/mockserver/reset')
  if status >= 400
    raise Error, "Failed to reset (status=#{status}): #{response_body}"
  end

  nil
ensure
  close
end

#reset_clockHash

Reset the server clock to real wall-clock time.

Returns:

  • (Hash)

    response with status, currentInstant, currentEpochMillis



186
187
188
189
190
191
192
193
194
# File 'lib/mockserver/client.rb', line 186

def reset_clock
  body = JSON.generate({ 'action' => 'reset' })
  status, response_body = request('PUT', '/mockserver/clock', body)
  if status >= 400
    raise Error, "Failed to reset clock (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#retrieve_active_expectations(request: nil) ⇒ Array<Expectation>

Retrieve active expectations.

Parameters:

Returns:



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/mockserver/client.rb', line 336

def retrieve_active_expectations(request: nil)
  body = request ? JSON.generate(request.to_h) : ''
  status, response_body = do_request(
    'PUT', '/mockserver/retrieve', body,
    { 'type' => 'ACTIVE_EXPECTATIONS', 'format' => 'JSON' }
  )
  if status >= 400
    raise Error, "Failed to retrieve active expectations (status=#{status}): #{response_body}"
  end

  if response_body && !response_body.empty?
    parsed = JSON.parse(response_body)
    return parsed.map { |e| Expectation.from_hash(e) } if parsed.is_a?(Array)
  end
  []
end

#retrieve_log_messages(request: nil) ⇒ Array<String>

Retrieve log messages.

Parameters:

Returns:

  • (Array<String>)


396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/mockserver/client.rb', line 396

def retrieve_log_messages(request: nil)
  body = request ? JSON.generate(request.to_h) : ''
  status, response_body = do_request(
    'PUT', '/mockserver/retrieve', body,
    { 'type' => 'LOGS' }
  )
  if status >= 400
    raise Error, "Failed to retrieve log messages (status=#{status}): #{response_body}"
  end

  if response_body && !response_body.empty?
    begin
      parsed = JSON.parse(response_body)
      return parsed if parsed.is_a?(Array)
    rescue JSON::ParserError
      return response_body.split("------------------------------------\n")
    end
  end
  []
end

#retrieve_recorded_expectations(request: nil) ⇒ Array<Expectation>

Retrieve recorded expectations.

Parameters:

Returns:



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/mockserver/client.rb', line 356

def retrieve_recorded_expectations(request: nil)
  body = request ? JSON.generate(request.to_h) : ''
  status, response_body = do_request(
    'PUT', '/mockserver/retrieve', body,
    { 'type' => 'RECORDED_EXPECTATIONS', 'format' => 'JSON' }
  )
  if status >= 400
    raise Error, "Failed to retrieve recorded expectations (status=#{status}): #{response_body}"
  end

  if response_body && !response_body.empty?
    parsed = JSON.parse(response_body)
    return parsed.map { |e| Expectation.from_hash(e) } if parsed.is_a?(Array)
  end
  []
end

#retrieve_recorded_requests(request: nil) ⇒ Array<HttpRequest>

Retrieve recorded requests.

Parameters:

Returns:



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/mockserver/client.rb', line 316

def retrieve_recorded_requests(request: nil)
  body = request ? JSON.generate(request.to_h) : ''
  status, response_body = do_request(
    'PUT', '/mockserver/retrieve', body,
    { 'type' => 'REQUESTS', 'format' => 'JSON' }
  )
  if status >= 400
    raise Error, "Failed to retrieve recorded requests (status=#{status}): #{response_body}"
  end

  if response_body && !response_body.empty?
    parsed = JSON.parse(response_body)
    return parsed.map { |r| HttpRequest.from_hash(r) } if parsed.is_a?(Array)
  end
  []
end

#retrieve_recorded_requests_and_responses(request: nil) ⇒ Array<HttpRequestAndHttpResponse>

Retrieve recorded requests and responses.

Parameters:

Returns:



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/mockserver/client.rb', line 376

def retrieve_recorded_requests_and_responses(request: nil)
  body = request ? JSON.generate(request.to_h) : ''
  status, response_body = do_request(
    'PUT', '/mockserver/retrieve', body,
    { 'type' => 'REQUEST_RESPONSES', 'format' => 'JSON' }
  )
  if status >= 400
    raise Error, "Failed to retrieve request/responses (status=#{status}): #{response_body}"
  end

  if response_body && !response_body.empty?
    parsed = JSON.parse(response_body)
    return parsed.map { |rr| HttpRequestAndHttpResponse.from_hash(rr) } if parsed.is_a?(Array)
  end
  []
end

#service_chaos_statusHash

Query the current service-scoped chaos registrations.

Returns:

  • (Hash)

    of the form { “services” => { host => profile, … } }



254
255
256
257
258
259
260
261
# File 'lib/mockserver/client.rb', line 254

def service_chaos_status
  status, response_body = request('GET', '/mockserver/serviceChaos')
  if status >= 400
    raise Error, "Failed to get service chaos (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#set_service_chaos(host, chaos, ttl_millis = nil) ⇒ Hash

Register a service-scoped HTTP chaos profile for an upstream host. The profile is applied to every matched forward expectation to that host that does not define its own chaos (an expectation’s own chaos always wins). The host is matched case-insensitively, ignoring any :port.

Parameters:

  • host (String)

    the upstream host to break

  • chaos (HttpChaosProfile)

    the chaos profile to apply

  • ttl_millis (Integer, nil) (defaults to: nil)

    if set, the chaos auto-reverts after this many ms

Returns:

  • (Hash)

    response with status and host



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/mockserver/client.rb', line 215

def set_service_chaos(host, chaos, ttl_millis = nil)
  payload = { 'host' => host, 'chaos' => chaos.to_h }
  payload['ttlMillis'] = ttl_millis unless ttl_millis.nil?
  body = JSON.generate(payload)
  status, response_body = request('PUT', '/mockserver/serviceChaos', body)
  if status >= 400
    raise Error, "Failed to set service chaos (status=#{status}): #{response_body}"
  end

  response_body && !response_body.empty? ? JSON.parse(response_body) : {}
end

#stopnil

Stop the MockServer instance.

Returns:

  • (nil)


436
437
438
439
440
441
442
443
# File 'lib/mockserver/client.rb', line 436

def stop
  request('PUT', '/mockserver/stop')
  nil
rescue ConnectionError
  nil
ensure
  close
end

#upsert(*expectations) ⇒ Array<Expectation>

Create or update expectations.

Parameters:

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mockserver/client.rb', line 71

def upsert(*expectations)
  body = JSON.generate(expectations.map(&:to_h))
  status, response_body = request('PUT', '/mockserver/expectation', body)
  if status == 400
    raise Error, "Invalid expectation: #{response_body}"
  end

  if status >= 400
    raise Error, "Failed to upsert expectations (status=#{status}): #{response_body}"
  end

  if response_body && !response_body.empty?
    parsed = JSON.parse(response_body)
    return parsed.map { |e| Expectation.from_hash(e) } if parsed.is_a?(Array)
  end
  expectations.to_a
end

#verify(request = nil, times: nil, response: nil) ⇒ nil

Verify that a request (and optionally a response) was received.

Parameters:

Returns:

  • (nil)

Raises:



269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/mockserver/client.rb', line 269

def verify(request = nil, times: nil, response: nil)
  verification = Verification.new(http_request: request, http_response: response, times: times)
  body = JSON.generate(verification.to_h)
  status, response_body = do_request('PUT', '/mockserver/verify', body)
  if status == 406
    raise VerificationError, response_body
  end

  if status >= 400
    raise Error, "Failed to verify (status=#{status}): #{response_body}"
  end

  nil
end

#verify_sequence(*requests, responses: nil) ⇒ nil

Verify that requests were received in sequence.

Parameters:

  • requests (Array<HttpRequest>)
  • responses (Array<HttpResponse>, nil) (defaults to: nil)

    index-aligned response matchers

Returns:

  • (nil)

Raises:



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/mockserver/client.rb', line 289

def verify_sequence(*requests, responses: nil)
  verification = VerificationSequence.new(
    http_requests: requests.empty? ? nil : requests.to_a,
    http_responses: responses
  )
  body = JSON.generate(verification.to_h)
  status, response_body = request('PUT', '/mockserver/verifySequence', body)
  if status == 406
    raise VerificationError, response_body
  end

  if status >= 400
    raise Error, "Failed to verify sequence (status=#{status}): #{response_body}"
  end

  nil
end

#verify_zero_interactionsnil

Verify zero interactions.

Returns:

  • (nil)


309
310
311
# File 'lib/mockserver/client.rb', line 309

def verify_zero_interactions
  verify(HttpRequest.new, times: VerificationTimes.new(at_most: 0))
end

#when(request, times: nil, time_to_live: nil, priority: nil) ⇒ ForwardChainExpectation

Begin building an expectation via the fluent API.

Parameters:

  • request (HttpRequest)
  • times (Times, nil) (defaults to: nil)
  • time_to_live (TimeToLive, nil) (defaults to: nil)
  • priority (Integer, nil) (defaults to: nil)

Returns:



474
475
476
477
478
479
480
481
482
# File 'lib/mockserver/client.rb', line 474

def when(request, times: nil, time_to_live: nil, priority: nil)
  expectation = Expectation.new(
    http_request: request,
    times: times,
    time_to_live: time_to_live,
    priority: priority
  )
  ForwardChainExpectation.new(self, expectation)
end