Git Product home page Git Product logo

easy-http-mock's Introduction

Easy Http mock

The goal of this project is to provide a simple way to mock http request and response for specific condition.

How to use

  1. Install

     gem install easy-http-mock
    
  2. Create a yaml file to config the mocked services,

    #settings.yml
    ---
      - 
    	request: 
          	path: /bar
    	response: bar.json
      -
    	request:
          path: /bar
    	  query:
        	param_one: haha
            param_two: blabla
    	response: bar_haha.json
    ...
    
  3. Start mocking server

     ehm settings.yml
    
  4. Test the results

     curl http://localhost:4567/bar
    

    You will get the text that specified in bar.json. As we have defined in the settings.yml, when visit /bar, it will return the bar.json file.

     curl http://localhost:4567/bar?param_one=haha&param_two=blabla
    

    You will get the text that defined in bar_haha.json. So the second route definition will only be return when you request it with specified params with the correct value.

     curl http://localhost:4567/bar?param_one=not_correct_value
    

    This one will return the bar.json, since if any of the conditional definitions(The second one) are not satified, then the default one(The first one in this example) will be returned.

Advance Usages with yaml syntax

Create your settings file with yaml node anchor and reference

---
  - &bar
    request: &bar_request
      path: /bar
    response: bar.json
  -
    request:
      <<: *bar_request
      query:
        param_one: haha
        param_two: blabla
    response: bar_haha.json
  -
    request:
      <<: *bar_request
      query:
        param_three: hehe
    response: bar_hehe.json
...

With this, you can reuse other mock service defination. For example, all the three service in the settings.yml using same path, so we can use yaml anchor and reference to reduce the redundant settings.

Setting the server

Using following command to show help information, you can config the port and bind address for the server.

ehm -h

Future work

Other http verb, for example POST, PUT and DELETE will be added.

Liscense

© 2014 Wang Chao. This code is distributed under the MIT license.

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.