org.expedientframework.facilemock:facile-mock-http-proxy

The parent module for mocking framework.

License

License

GroupId

GroupId

org.expedientframework.facilemock
ArtifactId

ArtifactId

facile-mock-http-proxy
Last Version

Last Version

1.0.0-M1
Release Date

Release Date

Type

Type

jar
Description

Description

The parent module for mocking framework.
Source Code Management

Source Code Management

https://github.com/ajeydudhe/facile-mock/tree/master/facile-mock-http-proxy

Download facile-mock-http-proxy

How to add to project

<!-- https://jarcasting.com/artifacts/org.expedientframework.facilemock/facile-mock-http-proxy/ -->
<dependency>
    <groupId>org.expedientframework.facilemock</groupId>
    <artifactId>facile-mock-http-proxy</artifactId>
    <version>1.0.0-M1</version>
</dependency>
// https://jarcasting.com/artifacts/org.expedientframework.facilemock/facile-mock-http-proxy/
implementation 'org.expedientframework.facilemock:facile-mock-http-proxy:1.0.0-M1'
// https://jarcasting.com/artifacts/org.expedientframework.facilemock/facile-mock-http-proxy/
implementation ("org.expedientframework.facilemock:facile-mock-http-proxy:1.0.0-M1")
'org.expedientframework.facilemock:facile-mock-http-proxy:jar:1.0.0-M1'
<dependency org="org.expedientframework.facilemock" name="facile-mock-http-proxy" rev="1.0.0-M1">
  <artifact name="facile-mock-http-proxy" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.expedientframework.facilemock', module='facile-mock-http-proxy', version='1.0.0-M1')
)
libraryDependencies += "org.expedientframework.facilemock" % "facile-mock-http-proxy" % "1.0.0-M1"
[org.expedientframework.facilemock/facile-mock-http-proxy "1.0.0-M1"]

Dependencies

compile (5)

Group / Artifact Type Version
org.expedientframework.facilemock : facile-mock-core jar 1.0.0-M1
net.lightbody.bmp : browsermob-core jar 2.1.5
com.fasterxml.jackson.core : jackson-databind jar 2.8.11.2
org.apache.logging.log4j : log4j-core jar 2.11.1
org.apache.logging.log4j : log4j-slf4j-impl jar 2.11.1

test (4)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.3.1
org.apache.httpcomponents : httpclient jar 4.5.6
commons-io : commons-io jar 2.6
org.assertj : assertj-core jar 3.11.1

Project Modules

There are no modules declared in this project.

Build Status MIT licensed

facile-mock - Simple mocking library for BrowserMobProxy.

WORK IN PROGRESS Work in progress.

facile-mock is a simple mocking library for BrowserMobProxy to allow mocking the http requests in the tests. It allows running a given test with given scope as unit test or integration test. In case of the scope as integration test the mocking will be skipped altogether and run against actual server.

Adding the library reference

Add the maven dependency to your pom.xml as follows:

<dependency>
    <groupId>org.expedientframework.facilemock</groupId>
    <artifactId>facile-mock-http-proxy</artifactId>
    <version>1.0.0-M1</version>
</dependency>

Usage

try(HttpMockContext mock = HttpProxyManagerFactory.createMockContext(unitTest()))
{
  final int port = mock.getHttpProxyManager().getPort();
  mock.when(post(urlEquals("/dummy"))).then(respondWith("Hello from mock for post !!!"));
  final response = postAndGetResponseBody(port, "/dummy");
  assertThat(response).as("Response").isEqualTo("Hello from mock for post !!!");
}
  • HttpProxyManagerFactory.createMockContext() creates the mocking context indicating that we are executing unit tests so that the mocking should be honoured. If you mark it as integration tests then no mocking will be done and the http calls will be made to live server as per the url. So you can run same tests as unit tests with mocking and integration tests without mocking. You just need to passing in proper parameter to createMockContext() at runtime.
  • mock.when() takes the mocking condition which in this case says that for post request with url equal to /dummy we should return a mock response.
  • then() specifies that we should respond with a dummy string value.
  • The postAndGetResponseBody() method makes a http call with the given port on which the http proxy is listening as follows:
public int postAndGetResponseStatus(final int port, final String endpoint)
{
    try(CloseableHttpClient httpClient = HttpClientBuilder.create().setProxy(new HttpHost("localhost", port)))
    {
      final HttpPost post = new HttpPost("http://sampleHostDoesNotExistsBlah.com" + endpoint);
      post.setEntity(new StringEntity("Dummy request body."));
      try(CloseableHttpResponse httpResponse = httpClient.execute(post))
      {
        return httpResponse.getStatusLine().getStatusCode();
      }
    }
    catch (IOException e)
    {
      throw new RuntimeException(e);
    }
}

Versions

Version
1.0.0-M1