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

#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>)


415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/mockserver/client.rb', line 415

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_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)


523
524
525
526
527
528
529
# File 'lib/mockserver/client.rb', line 523

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)


444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/mockserver/client.rb', line 444

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

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

Register a response callback via WebSocket.

Parameters:

Returns:



489
490
491
492
493
494
495
496
497
498
# File 'lib/mockserver/client.rb', line 489

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:



507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/mockserver/client.rb', line 507

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_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:



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/mockserver/client.rb', line 331

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>)


391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/mockserver/client.rb', line 391

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:



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/mockserver/client.rb', line 351

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:



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/mockserver/client.rb', line 311

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:



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/mockserver/client.rb', line 371

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)


431
432
433
434
435
436
437
438
# File 'lib/mockserver/client.rb', line 431

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, times: nil) ⇒ nil

Verify that a request was received.

Parameters:

Returns:

  • (nil)

Raises:



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

def verify(request, times: nil)
  verification = Verification.new(http_request: request, 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) ⇒ nil

Verify that requests were received in sequence.

Parameters:

Returns:

  • (nil)

Raises:



287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/mockserver/client.rb', line 287

def verify_sequence(*requests)
  verification = VerificationSequence.new(http_requests: requests.to_a)
  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)


304
305
306
# File 'lib/mockserver/client.rb', line 304

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:



469
470
471
472
473
474
475
476
477
# File 'lib/mockserver/client.rb', line 469

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