Skip to content

Implementations of Hoverfly proxy in Spring Boot using Bolt db.

Notifications You must be signed in to change notification settings

bhawanihmh/Hoverfly-Spring-Boot-BoltDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 

Repository files navigation

SpringBoot Hoverfly BoltDB

Implementations of Hoverfly proxy in Spring Boot using Bolt DB.

Install Hoverfly on MacOS

brew install SpectoLabs/tap/hoverfly
More : http://hoverfly.readthedocs.io/en/latest/pages/introduction/downloadinstallation.html

Install Bolt DB

go get github.com/boltdb/bolt/...
More : https://github.com/boltdb/boltd

Hoverfly commands

Hoverfly behind a proxy
hoverctl start --upstream-proxy http://corp.proxy:8080

Upstream proxy authentication
hoverctl start --upstream-proxy http://my-user:my-pass@corp.proxy:8080

Controlling a remote Hoverfly instance with hoverctl

hoverfly -ap 8880 -pp 8555

On your local machine, you can create a target named remote using hoverctl. This target will be configured to communicate with Hoverfly.

hoverctl targets create remote
--host hoverfly.example.com
--admin-port 8880
--proxy-port 8555

Now that hoverctl knows the location of the remote Hoverfly instance, run the following commands on your local machine to capture and simulate a URL using this instance:

hoverctl -t remote mode capture
curl --proxy http://hoverfly.example.com:8555 http://ip.jsontest.com
hoverctl -t remote mode simulate
curl --proxy http://hoverfly.example.com:8555 http://ip.jsontest.com

You will now need to specify the remote target every time you want to interact with this Hoverfly instance. If you are only working with this remote instance, you can set it to be the default target instance for hoverctl.

hoverctl targets default remote

How can I view the Hoverfly logs?
hoverctl logs

Why am I not able to access my Hoverfly remotely? That’s because Hoverfly is bind to loopback interface by default, meaning that you can only access to it on localhost. To access it remotely, you can specify the IP address it listens on. For example, setting 0.0.0.0 to listen on all network interfaces.

hoverfly -listen-on-host 0.0.0.0

hoverfly -ap 8880 -pp 8555 -listen-on-host XXX.XXX.XXX.XXX -db "boltdb" -db-path ~/sample.db

INFO[2018-07-18T15:24:22+05:30] Default proxy port has been overwritten port=8555
INFO[2018-07-18T15:24:22+05:30] Default admin port has been overwritten port=8880
INFO[2018-07-18T15:24:22+05:30] Listen on specific interface host=XXX.XXX.XXX.XXX
INFO[2018-07-18T15:24:22+05:30] Initiating database databaseName=/Users/bhawani.s.shekhawat/sample.db
INFO[2018-07-18T15:24:22+05:30] Using boltdb backend

INFO[2018-07-18T15:24:22+05:30] Proxy prepared... Destination=. Mode=simulate ProxyPort=8555
INFO[2018-07-18T15:24:22+05:30] current proxy configuration destination=. mode=simulate port=8555
INFO[2018-07-18T15:24:22+05:30] Admin interface is starting... AdminPort=8880
INFO[2018-07-18T15:24:22+05:30] serving proxy

SpringBoot changes:

import org.springframework.http.client.SimpleClientHttpRequestFactory;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;

RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("XXX.XXX.XXX.XXX", 8555));
requestFactory.setProxy(proxy);
restTemplate = new RestTemplate(requestFactory);
Programme programme = restTemplate.getForObject("http://127.0.0.1:8091/programme/{id}", Programme.class, programId);

Using middleware:

Hoverfly middleware can be written in any language. Middleware modules receive a service data JSON string via the standard input (STDIN) and must return a service data JSON string to the standard output (STDOUT).
To implement more dynamic behaviour, middleware should be combined with Hoverfly's synthesize mode (see the Creating synthetic services section).

Javascript example

This example will change the response code and body in each response.
Ensure that you have captured some traffic with Hoverfly
Ensure that you have NodeJS installed
Save the following code into a file named example.js and make it executable (chmod +x example.js):
#!/usr/bin/env node

process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function(data) {
var parsed_json = JSON.parse(data);
// changing response
parsed_json.response.status = 201;
parsed_json.response.body = "body was replaced by JavaScript middleware\n";

// stringifying JSON response
var newJsonString = JSON.stringify(parsed_json);

process.stdout.write(newJsonString);
});
Restart Hoverfly in simulate mode with the example.js script specified as middleware:
./hoverfly -middleware "./example.js"
Repeat the steps you took to capture the traffic You will notice that every response will have the 201 status code,
and the body will have been replaced by the string specified in the script.

More about Hoverfly:

https://hoverfly.readthedocs.io/en/latest/pages/tutorials/advanced/advanced.html

hoverctl commands:

https://hoverfly.readthedocs.io/en/latest/pages/reference/hoverctl/hoverctlcommands.html#hoverctl-commands