Example Code - Ruby
To setup an expectation that responds to a request:
require "mockserver-client"
class SomeClass
include MockServer
include MockServer::Model::DSL
def createExpectation
client = MockServerClient.new('localhost', 1080)
expectation = expectation do |expectation|
expectation.request do |request|
request.method = 'POST'
request.path = '/login'
request.query_string_parameters << parameter('returnUrl', '/account')
request.cookies = [cookie('sessionId', '2By8LOhBmaW5nZXJwcmludCIlMDAzMW')]
request.body = exact({ username: 'foo', password: 'bar' }.to_json)
end
expectation.response do |response|
response.status_code = 401
response.headers << header('Content-Type', 'application/json; charset=utf-8')
response.headers << header('Cache-Control', 'public, max-age=86400')
response.body = body({ message: 'incorrect username and password combination' }.to_json)
response.delay = delay_by(:SECONDS, 1)
end
end
client.register(expectation)
end
end
To setup an expectation that forwards all requests:
client = MockServerClient.new('localhost', 1080)
expectation = expectation do |expectation|
expectation.request do |request|
request.method = 'GET'
request.path = '/somePath'
end
expectation.forward do |forward|
forward.host = 'www.mock-server.com'
forward.port = 80
forward.scheme = :HTTP
end
expectation.times = exactly(2)
end
client.register(expectation)