Flipp - Modified Mockserver Client
A Ruby client for MockServer project. This client follows the Java client's fluent style closely by using Ruby blocks.
Installation
Add this line to your application's Gemfile:
gem 'mockserver-client'
Create and install the gem:
gem build [gem_name].gemspec
gem install [gem_name-version].gem
Usage
The Ruby code here assumes you have included MockServer
and MockServer::Model::DSL
modules. So these lines should typically be at the top of your code:
include MockServer
include MockServer::Model::DSL
Create Expectations
Ruby
client = MockServerClient.new('localhost', 9999)
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'}")
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' }")
response.delay = delay_by(:SECONDS, 1)
end
end
client.register(expectation)
Clear & Reset Server
Ruby
client.clear(request('POST', '/login'))
client.reset
Verifying Behavior
Ruby
ProxyClient.new('localhost', 9090).verify(request(:POST, '/login') do |request|
request.body = exact("{username: 'foo', password: 'bar'}")
request.cookies = [cookie("sessionId", ".*")]
end)
Analyzing Behavior
Ruby
ProxyClient.new('localhost', 9090).dump_log(request(:POST, '/login'), false)
Complete Ruby DSL
The DSL is provided via the MockServer::Model::DSL
module. Include this module in your code to make the DSL available.
Request
- request: Used to build a request object. If a block is passed, will configure request first and then return the configured request. Example:
request(:POST, '/login') {|r| r.headers << header("Content-Type", "application/json")}
. - http_request: Alias for
request
.
Body
- body: Create a string body object (use in a response). Example:
body("unaccepted")
. - exact: Create a body of type
STRING
. Example: exact('{"reason": "unauthorized"}')
. - regex: Create a body of type
REGEX
. Example: regex('username[a-z]{4}')
. - xpath: Used to create a body of type
XPATH
. Example: xpath("/element[key = 'some_key' and value = 'some_value']")
. - parameterized; Create a body to type
PARAMETERS
. Example parameterized(parameter('someValue', 1, 2), parameter('otherValue', 4, 5))
.
Parameters
- parameter: Create a generic parameter. Example:
parameter('key', 'value1' , 'value2')
. - cookie: Create a cookie (same as
parameter
above but exists as syntactic sugar). Example: cookie('sessionId', 'sessionid1ldj')
. - header: Create a header (same as
parameter
above but exists as syntactic sugar). Example: header('Content-Type', 'application/json')
.
Forward
- forward: Create a forwarding response. If a block is passed, will configure forward response first and then return the configured object. Example:
forward {|f| f.scheme = 'HTTPS' }
. - http_forward: Alias for
forward
.
Response
- response: Create a response object. If a block is passed, will configure response first and then return the configured response. Example:
response {|r| r.status_code = 201 }
. - http_response: Alias for
response
.
Delay
- delay_by. Create a delay object in a response. Example :
delay_by(:MICROSECONDS, 20)
.
Times (used in Expectation)
- times: Create an 'times' object. If a block is passed, will configure the object first before returning it. Example:
times {|t| t.unlimited = false }
. - unlimited: Create an object with unlimited repeats. Example:
unlimited()
. (No parameters). - once. Create an object that repeats only once. Example:
once()
. (No parameters). - exactly. Create an object that repeats exactly the number of times specified. Example:
exactly(2)
. - at_least: Create an object that repeats at least the given number of times. (Use in verify). Example:
at_least(2)
.
Expectation (use in register)
- expectation: Create an expectation object. If a block is passed, will configure the object first before returning it. Example:
expectation {|e| e.request {|r| r.path = "index.html} }
.
Getter methods for request
, response
and forward
methods will optionally accept a block. If block is passed object is configured before it is returned. The attribute times
has conventional getter and setter methods.
CLI
This gem comes with a command line interface which allow you to run the Mock Server calls from the command line. When this gem is installed, you will have the mockserver
executable on your PATH. Type mockserver --help
and you will get this output:
mockserver --help
Commands:
mockserver clear
mockserver dump_log
mockserver help [COMMAND]
mockserver register
mockserver reset
mockserver retrieve
mockserver verify
Options:
-h, --host=HOST
-p, --port=N
-d, [--data=DATA]
To get help for an individual command, e.g. dump_log
, you would do:
mockserver --help dump_log
Usage:
mockserver dump_log
Options:
-j, [--java], [--no-java]
-h, --host=HOST
-p, --port=N
-d, [--data=DATA]
Dumps the matching request to the mock server logs.
Here is an example on how you would run the command:
mockserver dump_log -j true
Running with parameters:
host: localhost
port: 8080
java: true
[2014-06-21 09:23:32] DEBUG [MockServerClient] Sending dump log request to mockserver
[2014-06-21 09:23:32] DEBUG [MockServerClient] URL: /dumpToLog?type=java. Payload: {}
[2014-06-21 09:23:32] DEBUG [MockServerClient] Got dump to log response: 202