Skip to content

Commit

Permalink
Wiremock engine: make browser proxying, notifier verbosity and gzip c…
Browse files Browse the repository at this point in the history
…ompression configurable #151
  • Loading branch information
Piotr Kulasek-Szwed committed Nov 8, 2020
1 parent bba8a2e commit 4114fa5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.hycom.mokka.stubbing;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for WireMock engine.
*
* @author Piotr Kulasek-Szwed <[email protected]>
*/
@ConfigurationProperties(prefix = "wiremock")
@Data
public class WireMockProperties {

private boolean enabled = false;

private int httpPort = 8082;

private boolean verbose = false;

private boolean browserProxying = false;

private boolean gzip = true;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package pl.hycom.mokka.stubbing;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.standalone.MappingsSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pl.hycom.mokka.stubbing.responsetemplating.GroovyResponseTransformer;
Expand All @@ -14,25 +15,23 @@
* @author Piotr Kulasek-Szwed <[email protected]>
*/
@Configuration
@EnableConfigurationProperties(WireMockProperties.class)
public class WireMockServerConfiguration {

/**
* WireMock HTTP port to be exposed.
*/
@Value("${wiremock.httpPort}")
private int wiremockHttpPort;

/**
* Provides configured {@link com.github.tomakehurst.wiremock.WireMockServer}.
*
* @return
*/
@Bean
public WireMockServer wireMockServer(MappingsSource wireMockMappingSource) {
public WireMockServer wireMockServer(WireMockProperties wireMockProperties, MappingsSource wireMockMappingSource) {
WireMockConfiguration o = new WireMockConfiguration();
o.port(wiremockHttpPort);
o.port(wireMockProperties.getHttpPort());
o.mappingSource(wireMockMappingSource);
o.extensions(GroovyResponseTransformer.class);
o.enableBrowserProxying(wireMockProperties.isBrowserProxying());
o.notifier(new Slf4jNotifier(wireMockProperties.isVerbose()));
o.gzipDisabled(wireMockProperties.isGzip());
return new WireMockServer(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.github.tomakehurst.wiremock.WireMockServer;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

Expand All @@ -23,13 +21,11 @@ public class WireMockServerRunner implements CommandLineRunner {

private final WireMockServer wireMockServer;

@Setter
@Value("${wiremock.enabled}")
private boolean wiremockEnabled;
private final WireMockProperties wireMockProperties;

@Override
public void run(String... args) throws Exception {
if (!wiremockEnabled) {
if (!wireMockProperties.isEnabled()) {
log.info("Embedded Wiremock disabled by configuration.");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public class WireMockServerRunnerTest {
public void shouldNotStartWireMock() {
// given
WireMockServer server = new WireMockServer();
WireMockServerRunner runner = new WireMockServerRunner(server);
runner.setWiremockEnabled(false);
WireMockProperties wireMockProperties = new WireMockProperties();
wireMockProperties.setEnabled(false);
WireMockServerRunner runner = new WireMockServerRunner(server, wireMockProperties);

// when
try {
Expand All @@ -34,8 +35,9 @@ public void shouldNotStartWireMock() {
public void shouldStartWireMock() {
// given
WireMockServer server = new WireMockServer();
WireMockServerRunner runner = new WireMockServerRunner(server);
runner.setWiremockEnabled(true);
WireMockProperties wireMockProperties = new WireMockProperties();
wireMockProperties.setEnabled(true);
WireMockServerRunner runner = new WireMockServerRunner(server, wireMockProperties);

// when
try {
Expand Down

0 comments on commit 4114fa5

Please sign in to comment.