GithubHelp home page GithubHelp logo

yahooarchive / fake-server Goto Github PK

View Code? Open in Web Editor NEW
55.0 13.0 21.0 38 KB

Fake-server is a generic and non-intrusive tool used to mock any server response. It has been designed to address issues when running tests against unstable or slow external servers.

License: Other

JavaScript 100.00%

fake-server's Introduction

#fake-server

** Please Note: ** This repo is no longer maintained and has known security vulnerabilities. Use at your own risk!

Build Status

Fake-server is a generic and non-intrusive tool used to mock any server response. It has been designed to address issues when running tests against unstable or slow external servers.

===========

How it works

The idea is to create a webserver listening in a different port and make your tests bring it up and configure how it should behave against each different request. "Configuration" can be done by posting the parameters and desired response to the server, or through configuration files inside ./default_routes/.

For every request, fake-server will try to match against the configured URIs and return the expected response.

Advantages

  • No need to instrument your code (as long as the external server endpoint is configurable :P)
  • Generic enough to work with blackbox or whitebox tests.
  • No database required

Quickstart (two really basic scenarios)

Clone this repository (npm package coming soon)

git clone [email protected]:yahoo/fake-server.git

Start (it will start a server on port 3012)

node server.js

Let's say you want "/test" to always return "hello" and "/foo" to return a 404.

All you have to do is POST to http://localhost:3012/add/ the following data:

Configure /test by posting:

{ route: '/test',
responseCode: 200,
responseBody: "hello" }

one of the many ways to do this is using cURL:

curl http://localhost:3012/add -X POST -H "Content-Type:application/json" -H "Accept:application/json"  \ 
 -d '{"route":"/test","responseCode":200,"responseBody":"hello"}' 

now let's configure our 404 example by sending this to the server:

{ route: '/foo',
responseCode: 404,
responseBody: "Not found" }

using cURL:

curl http://localhost:3012/add -X POST -H "Content-Type:application/json" -H "Accept:application/json" \  
 -d '{"route":"/foo","responseCode":404,"responseBody":"Not found"}' 

now, in your browser you can see the results:
http://localhost:3012/foo
http://localhost:3012/test

What else can fake-server do?

Configuration is done by sending a POST request to /add or by placing a json file containing configurations inside a "routes" object (see default_routes/sample.json for reference). Here are the supported features for this version:

Routes can be RegEx

This will match http://localhost:3012/news/007 as well as http://localhost:3012/news/1231293871293827:

{ route: '/news/[0-9]'
responseCode: 200,
responseBody: 'whatever you want' }

Fake-server supports "POST" calls and uses payload for matching. Regexs are supported for payload matching, and paths can be used to specify inner properties of JSON payloads:

{ route: '/news'
payload: {
id: [\d+], requests[1].user.login: 'jdoe', month: "february" },
responseCode: 200,
responseBody: 'yay! it matches'
}

Support for query string matching. All query params are evaluated as a RegEx.

{ route: '/news', queryParams: { id: "[\d+]", location: "Hawaii" }
responseCode: 200,
responseBody: 'Regex matching rocks'
}

... can also use the request Headers. So you can check if specific cookies are present, for instance

{ route: '/secure',
requiredHeaders: {
X-Auth: "secret",
},
responseCode: 200,
responseBody: 'header is there'
}

Response can be a file. In this case, fake-server will respond with the output of that file.

The following configuration example will return the output of ./mock_data/sample.json (notice the parameter is called responseData instead of responseBody)

{ route: '/',
responseCode: 200,
responseData: './mock_data/sample.json' }

Same endpoint can have different responses

This will return '200' in the first two requests to '/' and 403 on the third request

{ route: '/',
responseCode: 200,
responseBody: 'ok' }

note that I will be adding an 'at' parameter to configure the special behavior to the third request:

{ route: '/',
responseCode: 403,
responseBody: 'Thou shall not pass!',
at: 3 }

Delay response

The following will delay server response in one second:

{ route: '/slow/.*',
responseCode: 200,
responseBody: 'OK',
delay: 1000 }

Resetting server configuration

To avoid the need to restart fake-server in order to clear the configuration, we've implemented a special endpoint called /flush. By sending a DELETE request to http://localhost:3012/flush, you will erase all previously configured responses.

Limitations

  • There are two reserved endpoints: POST '/add' and DELETE '/flush'. These cannot be used by your application.

fake-server's People

Contributors

bigodines avatar nishnet2002 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fake-server's Issues

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.