Giter Club home page Giter Club logo

robohydra-mock-server's Introduction

RoboHydra Mock Server

This is a HTTP mocking server based on RoboHydra framework.

npm GitHub license Build Status Code Climate Test Coverage

Installation

npm i -g robohydra
npm i --save-dev robohydra-mock-server

Configuration

Create a robohydra.json configuration file:

{
  "plugins": [
    "admin-extras",
    "forward-proxy",
    "swagger-mock",
    "user-agent-summoner"
  ],
  "pluginLoadPaths": [
    "node_modules/robohydra-mock-server/plugins"
  ],
  "summoner": {
    "hydraPickerPlugin": "user-agent-summoner"
  }
}

Then run a RoboHydra server:

robohydra robohydra.json

Connections will be accepted on http://localhost:3000/.

Usage

Command Line Client

Let's add a some rules to our RoboHydra proxy. For example, let's it proxy connections to http://robohydra.org/.

curl -X POST \
  --data-urlencode newHeadType=proxy \
  --data-urlencode newProxyHeadMountPath=/robohydra.org/ \
  --data-urlencode newProxyHeadProxyTo=http://robohydra.org/ \
  --data-urlencode newProxyHeadSetHostHeader=on \
  http://localhost:3000/robohydra-admin/head/create

Check that it works:

curl --proxy http://localhost:3000 http://robohydra.org/

It should output HTML code.

And any other URLs have been rejected:

curl --proxy http://localhost:3000 http://rkn.gov.ru/

It should output a Not Found.

JavaScript Client

import MockServerClient = require('robohydra-mock-server/client');
import request = require('request');


/* create a client */
const roboHydraUrl = 'http://localhost:3000/';
const mockServerClient = new MockServerClient({ host: roboHydraUrl });


/* set expectations */

// proxy connections
const devServerProxy = () =>
  mockServerClient.when({ url: `http://robohydra.org/` }).proxy();

// fake respose
const fakeServerError = () =>
  mockServerClient
    .when({ url: `http://robohydra.org/fake` })
    .respose({ status: 500, body: '(Fake) Internal Server Error' });


const useIt = () =>
   const r = request.defaults({'proxy': roboHydraUrl});

   r.get(`http://robohydra.org/`)
     .on('response', (resp) => console.log(resp.statusCode) ); // 200

   r.get(`http://robohydra.org/fake`)
     .on('response', (resp) => console.log(resp.statusCode) ); // 500


const removeAllExpectations = () => mockServerClient.reset()


Promise.all([devServerProxy(), fakeServerError()])
  .then(useIt)
  .then(removeAllExpectations);

TODO

  • Add support for HTTPS.
  • Add server-side validators.
  • Add Swagger mocking head.
  • Add support for multiple sessions.

robohydra-mock-server's People

Contributors

generalov-epm avatar generalov avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.