All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.httpmock.rules.HttpMockServer Maven / Gradle / Ivy

There is a newer version: 1.1.9
Show newest version
package com.github.httpmock.rules;

import static com.github.httpmock.PortUtil.getRandomPorts;

import java.util.List;

import org.junit.rules.ExternalResource;

import com.github.httpmock.ServerException;
import com.github.httpmock.HttpMockServerStandalone;

public class HttpMockServer extends ExternalResource {
	private HttpMockServerStandalone applicationServer;
	private static final String MOCK_SERVER_CONTEXT = "/mockserver";

	@Override
	protected void before() throws Throwable {
		startServer();
	}

	private void startServer() {
		applicationServer = createApplicationServer();
		try {
			applicationServer.start();
		} catch (Exception e) {
			throw new ServerException(e);
		}
		applicationServer.deploy("target/wars/mockserver.war");
	}

	HttpMockServerStandalone createApplicationServer() {
		List randomPorts = getRandomPorts(2);
		return new HttpMockServerStandalone(randomPorts.get(0), randomPorts.get(1));
	}

	@Override
	protected void after() {
		stopServer();
	}

	private void stopServer() {
		try {
			applicationServer.stop();
		} catch (Exception e) {
			throw new ServerException(e);
		}
	}

	public int getPort() {
		return applicationServer.getHttpPort();
	}

	public MockService getMockService() {
		return new MockService(getBaseUri(), MOCK_SERVER_CONTEXT);
	}

	private String getBaseUri() {
		int port = getPort();
		String host = "localhost";
		return String.format("http://%s:%d", host, port);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy