Thursday, July 7, 2016

Mock Server

Sometimes you need to mock out a web endpoint and Mock Server is a good tool to do this. There are many common uses cases for this including:
  • Working in conjunction with a 3rd party and the endpoint isn’t ready yet
  • Endpoint may not be reliable
  • Testing purposes
The advantages I saw of Mock Server versus others I looked at were:
  • Ability to register endpoints dynamically
  • Ruby library - Relatively simple to setup

Setup

Mock Server has two parts:
  • The Server - which can be downloaded from here as a jar
  • The client, which is available in Java, JavaScript, and Ruby. For this post, I’m going to focus on the Ruby gem.

Starting up Server

Run the following from the command line:
java -jar <path to mockserver-netty-3.10.4-jar-with-dependencies.jar> --serverPort 8080

Setup the client

Create a Gemfile and add the following:

gem ruby source: 'http://rubygems.org'
gem 'mockserver-client' 

Run bundle install

Create Expectations

Now that we are setup, in order to setup a mocked endpoint, an expectation needs to be created and registered with the Mock Server. Let’s setup a simple ‘GET’ endpoint for illustration.

Get Expectation

First, we’ll setup a class called ExpecationGenerator:
require 'mockserver-client'

class ExpectationGenerator
  include MockServer
  include MockServer::Model::DSL

  def initialize(server_port)
    @client = MockServerClient.new('localhost', server_port)
  end

  def simple_get_expectation
    expectation = expectation do |expectation|
      expectation.request do |request|
        request.method = 'GET'
        request.path = '/simple-get'
      end

      expectation.response do |response|
        response.status_code = 200
        response.body = "Success"
      end
    end

    expectation.times = unlimited() #once()
    @client.register(expectation)
  end
end

mock_server = ExpectationGenerator.new(8080)
mock_server.simple_get_expectation

Code highlights:
  • Setup a class that includes MockServer and the MockServer::Model::DSL
  • Initialize a MockServerClient with the server and port of where your mock server is running
  • Setup the expectation request and response for a ‘GET’ method
    • request.method can be set to ‘GET’, ‘POST’, etc.
    • request.path is the path to mock out
    • response.status_code returns the HTTP status code of the mocked method
    • response.body returns the body to be returned by the endpoint
      • expectation.times can be set to unlimited, as in our example. It can also be set to once(), twice(), etc. Once this endpoint gets hit n number of times, then it becomes invalid and will result in a 404.
  • last piece is registering the endpoint with the client
  • Now that we have the class setup, we need to instantiate it with the mock_server variable

Running the example

  • Start the mock server:
    • java -jar <path to mockserver-netty-3.10.4-jar-with-dependencies.jar> --serverPort 8080
  • Call the expectations.rb file
    • ruby expectations.rb

Prove that it works

  • Open up a browser, type in http://localhost:8080/simple-get and it should return “Success”
This post serves as an introduction to mock server. It’s capable of much more. I would encourage checking out Mock Server