Node Packaged Modules (npm)

npm contains the following modules:

Running MockServer With mockserver-grunt

The node module can be used to start and stop MockServer and the MockServer proxy as a node module or as a Grunt plugin.

You may install this plugin / node module with the following command:

npm install mockserver-grunt --save-dev

Node Module

To start or stop the MockServer from any Node.js code you need to import this module using require('mockserver-grunt') as follows:

var mockserver = require('mockserver-grunt');

Then you can use either the start_mockserver or stop_mockserver functions as follows:

var mockserver = require('mockserver-grunt');
mockserver.start_mockserver({
                serverPort: 1080,
                proxyPort: 1090,
                verbose: true
            });

// do something

mockserver.stop_mockserver();

If you are only using the MockServer then only specify the MockServer port as follows:

mockserver.start_mockserver({serverPort: 1080});

// do something

mockserver.stop_mockserver();

The MockServer and the MockServer proxy use port unification to support HTTP and HTTPS on the same port. A client can then connect to the single port with both HTTP and HTTPS as the socket will automatically detected SSL traffic and decrypt it when required.

Grunt Plugin

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins.

In your project's Gruntfile, add a section named start_mockserver and stop_mockserver to the data object passed into grunt.initConfig().

The following example will result in a both a MockServer and a MockServer proxy being started on ports 1080 and 1090.

grunt.initConfig({
    start_mockserver: {
        start: {
            options: {
                serverPort: 1080,
                proxyPort: 1090
            }
        }
    },
    stop_mockserver: {
        stop: {

        }
    }
});

grunt.loadNpmTasks('mockserver-grunt');

Grunt Plugin & NPM Module Options

options.serverPort

Type: Integer Default: undefined

This value specifies the HTTP and HTTPS port for the MockServer port unification is used to support HTTP and HTTPS on the same port. The MockServer will only be started if a port is provided, if this value is left undefined the MockServer will not be started.

options.proxyPort

Type: Integer Default: undefined

This value specifies the HTTP, HTTPS, SOCKS and HTTP CONNECT port for proxy, port unification is used to support all protocols on the same port. The proxy will only be started if a port is provided, if this value is left undefined the proxy will not be started.

options.verbose

Type: Boolean Default: false

This value indicates whether the MockServer logs should be written to the console. In addition to logging additional output from the grunt task this options also sets the logging level of the MockServer to INFO. At INFO level all interactions with the MockServer including setting up expectations, matching expectations, clearing expectations and verifying requests are written to the log. The MockServer logs are written to mockserver.log in the current directory.

Note: It is also possible to use the --verbose command line switch to enabled verbose level logging from the command line.

options.trace

Type: Boolean Default: false

This value sets the logging level of the MockServer to TRACE. At TRACE level (in addition to INFO level information) all matcher results, including when specific matchers fail (such as HeaderMatcher) are written to the log. The MockServer logs are written to mockserver.log in the current directory.

options.javaDebugPort

Type: Integer Default: undefined

This value indicates whether Java debugging should be enabled and if so which port the debugger should listen on. When this options is provided the following additional option is passed to the JVM:

"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + javaDebugPort

Note: suspend=y is used so the MockServer will pause until the debugger is attached. The grunt task will wait 50 seconds for the debugger to be attached before it exits with a failure status.