Coder Social home page Coder Social logo

restspy's Introduction

RESTSpy Build Status

Mock REST endpoints or proxy them to real endpoints.

Spies

A Spy is a local proxy server that by default redirects all requests to another server. You can then start to mock certain endpoints.

In a test setup you will create a Spy for every REST service that your application depends on and that you want to spy on.

Once a Spy is created, you can start mocking certain endpoints, by providing Doubles. A Double represents a HTTP response as a triplet of a status code, headers and body.

Examples

To get easy access to the RestSpy API you can include RestSpy::Api

include RestSpy::Api

Mock http://localhost:1234/stream endpoint, while requests to all other endpoints are proxied to https://www.facebook.com

facebook = Spy.server_on_local_port('https://www.facebook.com', 1234)
facebook.endpoint('/stream').should(return_response('Hello world!'))

Endpoints can also be matched using a regular expression. The following example shows how to mock all endpoints on localhost:1234 that start with stream.

facebook.endpoint('/stream.*').should(return_response(`Hello world!`, headers={'Token' => 'abcd'}))

Simulate an error code on the http://localhost:1234/search endpoint, while requests to all other endpoints are proxied to http://www.google.com

google = Spy.server_on_local_port('http://www.google.com', 1234)
google.endpoint('/search').should(return_error_code 401)

You can also define series of doubles and proxies for an endpoint:

google.endpoint('/mail').should(
          first(return_error_code 500)
          .and_then(return_error_code 401)
          .and_then(proxy_to(facebook.remote_url)))

restspy's People

Contributors

ybonjour avatar

Stargazers

M. Szalkiewicz avatar Vincent Garrigues avatar Roland Koch avatar Michael England avatar Slavko Krucaj avatar Meghan Kane avatar  avatar Slawomir Smiechura avatar

Watchers

 avatar James Cloos avatar Slavko Krucaj avatar

restspy's Issues

Stub Endpoints only on specific HTTP-Methods

When a double is created for an endpoint it currently stubs the endpoint for all HTTP methods.
It should be possible to create a double only for specific HTTP methods.

Something along the lines of :

spy.endpoint('/foo').should_return_error_code(500) # Stubs all HTTP methods
spy.endpoint('/foo', GET).should_return_error_code(500) # Stubs only GET
spy.endpoint('/foo', [GET, POST]).should_return_error_code(500) # Stubs GET and POST

Add timestamp to requests

When retrieving the executed requests on the /spy endpoint, the timestamps for each request should be returned. This would allow the client to order the requests chronological.

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.