Coder Social home page Coder Social logo

rest-mock's Introduction

rest-mock Build Status

A tiny test-framework to provide stub-responses for REST calls.

Talk is cheap, show me the code!

Developer bob = new Developer("Bob", 25);
  
RestMock.whenGet("/developer/").thenReturnJSON(bob);
RestMock.startServer();

Ready! Now you can access http://localhost:8080/developer/ and get the following JSON:

{ "name": "Bob", "age": 25 }

Alternatives:

Do you preferer XML? What about:

RestMock.whenGet("/developer/").thenReturnXML(bob);

Ready!

	<?xml version="1.0" ?>
	<developer>
	<name>Bob</name>
	<age>25</age>
	</developer>

And more, you can define your return as a literal using:

RestMock.whenGet("/developer/").thenReturnJSON("yourJSON");
RestMock.whenGet("/developer/").thenReturnXML("yourXML");
RestMock.whenGet("/developer/").thenReturnHtml("yourHTML");
RestMock.whenGet("/developer/").thenReturnText("yourTxt");

rest-mock will set the correct content type in http response.

External files responses

Well, sometimes you have complex answers. So, you don't want create an Object. And paste all the file inside a String doesn't look good as well.

Ok my friend. What about save the files inside your resources test folder and keep your code clean?

Just use the fromResource methods:

RestMock.whenGet("/reallyComplexEnterprise/").thenReturnXMLFromResource("enterprise-answer.xml");
RestMock.whenGet("/hugeHTML/").thenReturnHtml("my-super-index.html");

Testing error handling

Want to test a Forbidden error request?

RestMock.whenGet("/developer/")
	.thenReturnErrorCodeWithMessage(HttpServletResponse.SC_FORBIDDEN, "Forbidden GET");

When you send a GET request do this address you get:

Forbidden GET

... and a HTTP 403 status.

You can use any HTTP status to mock different behaviour.

Dynamic Response

Sometimes you expect a result based on your request parameters. For those purpose you can use the wildtag ${parameter}.

RestMock.whenGet("/hello").thenReturnText("Hello ${name}!");
RestMock.startServer();

When you access /test?name=Bob, you got:

Hello Bob!

Stub Headers

Do you need some data in your header? Don't worry.

RestMock.whenGet("/hello").thenReturnText("Hello ${name}!").withHeader("Cache-Control", "no-cache");
RestMock.whenGet("/hi").thenReturnText("Hi").withHeader("a", "1").withHeader("b", "2").withHeader("c", "3");

rest-mock's People

Contributors

francisrangel avatar gb avatar

Watchers

 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.