npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mockserver-client-builder

v1.2.1

Published

Powerful mockserver client wrapper based on builders with expectation, request matcher and action builders in TypeScript notation

Downloads

25

Readme

Based around mockserver-client-node.

About

The package provides the ability to program the MockServer in design mode.

Configure expectations, scheduling requests and responses through a convenient builder system.

Detailed API documentation of MockServer can be found on the official website.

Basics

npm i mockserver-client-builder
import { client } from 'mockserver-client-builder';

// Config
const config = {
  host: 'mockserver-srv',
  port: 1080,
  tls: false,
};

// Simple plain response mock
client(config)
  .mockSimpleResponse('/simple-mock-request', { music: 'Rock' }, 418);

Running MockServer in the Docker

For running MockServer in Docker for local usage you can apply this docker-compose.yml.

For advanced manual configuration of MockServer in Docker see this article.

Usage examples

To run some scenario use nodejs CLI for compiled js.

$ eslint . --ext .ts
...
$ tcs --build
...
$ nodejs ./lib/path/complex-expectation.js

Now your MockServer is ready to accept requests with expectation responses based on complex-expectation.js scenario.

Example #1. Complex expectation creation

Let's create a new file complex-expectation.ts for mock expectation implementation.

import {
  client, expectation, request, response,
} from 'mockserver-client-builder';

/**
 * Complex expectation building with some advanced params for request matcher, response and expectation.
 * @see {@link https://www.mock-server.com/mock_server/creating_expectations.html}
 */

// Config
const config = {
  host: 'mockserver-srv',
  port: 1080,
  tls: false,
};

// Expectation
const expectationBuilder = expectation()
// When we send request
  .when(
    request()
      .withMethod('GET')
      .withPath('/cities')
      .withQueryStringParameters({
        'filter[id]': ['11', '12'],
        code: ['61'],
      }),
  )
// We expect a respond
  .action(
    response()
      .withStatusCode(200)
      .withBody({
        items: [
          {
            id: '1',
            name: 'Ростов-на-Дону',
          },
          {
            id: '2',
            name: 'Москва',
          },
          {
            id: '3',
            name: 'Таганрог',
          },
        ],
      })
      .withCookies({
        session_id: 'Rftre5638jucg93',
      })
      .withHeaders({
        'Content-Type': [
          'application/json; charset=utf-8',
        ],
        'Cache-Control': [
          'public, max-age=86400',
        ],
        'X-Vendor': [
          'Oleg Chulakov Studio',
        ],
      }),
  )
// Sets priority of expectation
  .withPriority(100)
// After 2 calls the expectation will be cleared
  .withTimes({
    remainingTimes: 2,
  })
// After 30 seconds the expectation will be deleted
  .withTimeToLive({
    timeUnit: 'SECONDS',
    timeToLive: 30,
  })
// Set custom expectation id for simple update (replace)
  .withId('the-on-of-123');

// Send our expectation into mocksever
client(config)
  .mockAnyResponse(expectationBuilder)
  .then((/* value */) => {
    console.log('OK: /cities');
  }, (/* reason */) => {
    console.log('FAIL: /cities');
  });

Example #2. Clear all expecattions data from mockserver

Let's write a small code in file reset-all.ts.

import { client } from 'mockserver-client-builder';

/**
 * Clear & resets all data: logs, expectations.
 *
 * @see {@link https://www.mock-server.com/mock_server/clearing_and_resetting.html}
 */

// Config
const config = {
  host: 'mockserver-srv',
  port: 1080,
  tls: false,
};

// Reset all saved expectations in Mockserver
client(config)
  .reset()
  .then((/* value */) => {
    console.log('OK: Clear All');
  }, (/* reason */) => {
    console.log('FAIL: Clear All');
  });

Other examples

See other examples with creation of requests, responses, expectations and also control it in mockserver.

Run tests

For example, the MockServer started on mockserver-srv, without https and on 1080 port.

SCHEMA=http HOST=mockserver-srv PORT=1080 npm run test