Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

de.hanbei:mock-httpserver

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

de.hanbei:mock-httpserver

A MockHttp Server for unit tests that allows to send poredefined responses.

  • 1.0.2
  • Source
  • Maven
  • Socket score

Version published
Maintainers
1
Source

Build Status

mock-httpserver

A Mock Http Server where you can define the answers received on requests optionally based on predicates the request has to fulfill. Its main purpose is to be used in JUnit-Tests.

Starting and stopping the mock-httpserver

Assuming we start the mock-httpserver in the setUp method and stop it in the tearDown method the code would be:

@Before
public void setUp() {
  mockHttpServer = new MockHttpServer(8888);
  mockHttpServer.start();
}

This starts a mock-httpserver on port 8888. To shut it down call the stop method of the mock-httpserver.

@After
public void tearDown() {
  mockHttpServer.stop();
}

How to define responses

By default the mock httpserver just returns a 404 Not Found status code on any request. To define another response that should be returned on a request you can use the addResponse method of the class MockHttpServer. This method takes the HTTP-Method, a relative URI the response should answer to and the Response that should be sent. A response can be built using the builder pattern and the ResponseBuilder class. The Response class provides some static methods to create a ResponseBuilder.

For example to create a response that returns a HTTP status code of 200 and some json string as content use the ResponseBuilder as follows:

Response response = Response.ok().type("application/json").content("{\"some\":\"json\"}").build();

To tell the mock httpserver to return this response one a GET request to http://localhost:8888/some/json do it like this.

mockHttpServer.addResponse(Method.GET, URI.create("/some/json"), response);

For details how to construct a response see the javadoc of the class ResponseBuilder.

How to process requests

If you need to make sure that a request contains a specific header, query parameter or any other request property you can also process the request sent to the server. For this you have to implement process method of the RequestProcessor interface and tell the server to process the request sent to a uri. As for normal responses you can bind request processors to a method and a uri as below.

mockHttpServer.addRequestProcessor(Method.POST, URI.create("some/post"), new RequestProcessor() {
  @Override
  public Response process(Request request) {
    List<String> headers = request.getHeader().getHeaderValues(Header.Fields.USER_AGENT);
    if(headers.contains("Mozilla...")) {
      return Response.ok().build();
    } else {
      return Response.status(Status.UNAUTHORIZED).build();
    }
  }
});

Be careful to always send a response back. If you need to assert certain request parameters please do it outside of the process method. Otherwise the client will wait for a response that will not be sent.

...
List<String> headerValues;

mockHttpServer.addRequestProcessor(Method.GET, URI.create("some/post"), new RequestProcessor() {
  @Override
  public Response process(Request request) {
    headerValues = request.getHeader().getHeaderValues(Header.Fields.USER_AGENT);
    Response.ok().build();
  }
});

assertTrue(headerValues.contains("Mozilla..."));

Maven

As github removed the raw access to files I can no longer provide a Maven repository. Please clone the repo and us either a local install or your own nexus. I'm working on getting it on maven central.

FAQs

Package last updated on 21 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc