GithubHelp home page GithubHelp logo

kaizer / fakerest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from katta/fakerest

0.0 3.0 0.0 99 KB

Fakerest is a simple tool based on sinatra which starts a http server (webrick) and exposes restful services based on the configuration specified in a YAML format.

fakerest's Introduction

Fakerest

Fakerest is a simple tool based on sinatra which starts a http server (webrick) and exposes restful services based on the configuration specified in a YAML format.

Features

You can :

  • Stub any of the restful servies be it any method GET, POST, HEAD, PUT etc.
  • Configure multiple URLs based on your need
  • Define the response code for each of the URL
  • Define the response content type for each of the URL
  • Define the response content based on erb templates for each of the URL
  • Look at the requests made by your application for verifying what the application request look like. Quite handly for testing.
  • Upload files and view the files uploaded in your stub.

Prerequisites

These are required libraries to build and install fakerest:

  • sinatra - webrick based library to host rest services
  • rake - build tool for ruby (required only if you are building from code)
  • mocha - mocha is a mocking and stubbing library for Ruby (this is required only for running tests)

Installing

gem install fakerest

Building from code

Follow these simple steps to create a gem and install it

git clone git://github.com/katta/fakerest.git
cd fakerest
rake package
gem install pkg/fakerest-<version>.gem

Once you install this gem it creates an executable fakerest in the gems default executable directory. To find the gems executable directory use the command gem environment and look out for EXECUTABLE DIRECTORY:

For easy access update your PATH environment variable to point to a EXECUTABLE DIRECTORY:

Usage

You can run fakerest using a command

$ fakerest

this will display the instructions on all the command line arguments to use fakerest which looks like

Usage: fakerest.rb [options]
    -c, --config CONFIG_FILE         Confilg file to load request mappings (required)
    -p, --port PORT                  Port on which the fakerest to be run. Default = 1111
    -w, --views VIEWS_FOLDER         Folder path that contains the views. Default = <WORKING_DIR>/views
    -u, --uploads UPLOADS_FOLDER     Folder to which any file uploads to be stored. Default = <WORKING_DIR>/uploads
    -h, --help                       Displays this help message

Examples

Specifying config file

$ fakerest -c sample.yaml

sample.yaml is a config file which contains the configuration of all rest services you would like to host. It looks like

---
method : get
path : /customer/:id
response:
  content_file : customer
  content_type : json
  status_code : 200
---
method : post
path : /customer
response:
  content_file : customer_created
  content_type : text/plain
  status_code : 200

Changing the default port

Use -p option to change the port on which the fakerest runs. By default it runs on 1111 port.

$ fakerest -p 2222 -c sample.yaml

Views

Views are the erb template files from which the content is read and is served as a response to a http request based on the view file specified in the configuration.

For e.g. In the following configuration, notice the value for content_file. Fakerest looks for a template customer.erb in the views folder.

method : get
path : /customer/:id
response:
  content_file : customer # view file
  content_type : json 
  status_code : 200

Option -w can be used to tell Fakerest the folder in which the view files are held. A samile view file will look like this

{
  "id" : "<%= params["id"] %>",
  "name" : "John"
}

Notice the expression <%= params["id"] %> in the above template code, this will get evaluated before the response is served to the client by Fakerest using the parameters passed in the request.

In the above case if a request is made to a url http://localhost:1111/customer/20 the response will be

{
  "id" : "20",
  "name" : "John"
}

File uploads

You can do a post with file attachments to fakerest server and verify the content of the file using the browser (see next section)

There is a known issue with this, the file attachment parameter name should be "file"

Verifying the requests made to fakerest

Just go to your favorite browser and hit http://localhost:1111/requests/10 where 10 is a number of recent requests you would like to verify. You could change that number 10 to number of requests you are interested in.

Released versions

  • 0.0.4 - minor bug fixes
  • 0.0.3 - with fix for file uploads
  • 0.0.2 - initial version (with know issue of file uploads not working)

Credits

  • Aravind for his help in brainstorming this idea
  • Vignesh for his contribution for the first version of this library

fakerest's People

Contributors

katta avatar

Watchers

Kaizer Poonawala avatar James Cloos avatar  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.