Skip to main content
The mockserver-client gem is a community-maintained Ruby client for MockServer. It provides a DSL for creating expectations, verifying requests, and managing server state.
The Ruby client is a community-maintained project, not an official MockServer artifact. The canonical source and latest release information are available on RubyGems.

Installation

gem 'mockserver-client'
Then run:
bundle install

Usage

The gem exposes two clients:
  • MockServer::MockServerClient — for interacting with MockServer (mocking)
  • MockServer::ProxyClient — for interacting with MockServer acting as a proxy
require 'mockserver-client'

client = MockServer::MockServerClient.new('localhost', 1080)

JSON-based API

The Ruby client mirrors the MockServer REST API. If the Ruby DSL does not cover a particular feature, you can submit raw expectation JSON directly via the REST API using any HTTP library:
require 'net/http'
require 'json'

uri = URI('http://localhost:1080/mockserver/expectation')
http = Net::HTTP.new(uri.host, uri.port)

request = Net::HTTP::Put.new(uri)
request['Content-Type'] = 'application/json'
request.body = JSON.dump({
  "httpRequest" => {
    "method" => "GET",
    "path" => "/view/cart",
    "queryStringParameters" => {
      "cartId" => ["055CA455-1DF7-45BB-8535-4F83E7266092"]
    },
    "cookies" => {
      "session" => "4930456C-C718-476F-971F-CB8E047AB349"
    }
  },
  "httpResponse" => {
    "body" => "some_response_body"
  }
})

response = http.request(request)
puts response.code
To verify a request was received:
uri = URI('http://localhost:1080/mockserver/verify')
http = Net::HTTP.new(uri.host, uri.port)

request = Net::HTTP::Put.new(uri)
request['Content-Type'] = 'application/json'
request.body = JSON.dump({
  "httpRequest" => {
    "path" => "/view/cart"
  },
  "times" => {
    "atLeast" => 1
  }
})

response = http.request(request)
# A 202 response means verification passed.
# A 406 response means verification failed — the body contains the failure message.
puts response.code
To reset all state:
uri = URI('http://localhost:1080/mockserver/reset')
Net::HTTP.new(uri.host, uri.port).request(Net::HTTP::Put.new(uri))

REST API reference

All MockServer operations are available over HTTP. The full schema is documented in the REST API overview. The endpoints relevant to this guide are:
OperationMethodEndpoint
Create expectationPUT/mockserver/expectation
Verify requestPUT/mockserver/verify
Verify sequencePUT/mockserver/verifySequence
Retrieve recorded requestsPUT/mockserver/retrieve?type=REQUESTS
Clear by matcherPUT/mockserver/clear
Reset all statePUT/mockserver/reset
If you need features not yet available in the Ruby gem — such as OpenAPI request matching, response templates, or callback actions — use the REST API directly as shown above. Every feature MockServer supports is accessible via HTTP.