Permalink
Browse files

Combining Configuration and ProxyConfiguration together since they pr…

…ovide no value separate.
  • Loading branch information...
cowboygneox committed Jun 18, 2016
1 parent 3fa1a12 commit 81c8d19bcbef59c2319e2ba55b669c10e76c87eb
Showing with 257 additions and 347 deletions.
  1. +111 −11 betamax-core/src/main/java/software/betamax/Configuration.java
  2. +77 −33 betamax-core/src/main/java/software/betamax/ConfigurationBuilder.java
  3. +0 −139 betamax-core/src/main/java/software/betamax/ProxyConfiguration.java
  4. +0 −99 betamax-core/src/main/java/software/betamax/ProxyConfigurationBuilder.java
  5. +1 −1 betamax-core/src/main/java/software/betamax/Recorder.java
  6. +3 −3 betamax-core/src/main/java/software/betamax/proxy/ProxyServer.java
  7. +3 −3 betamax-specs2_2.11/src/main/scala/software/betamax/specs2/RecordedInteraction.scala
  8. +1 −1 betamax-tests/src/test/groovy/software/betamax/AnnotationSpec.groovy
  9. +1 −1 betamax-tests/src/test/groovy/software/betamax/AnnotationTest.groovy
  10. +1 −1 betamax-tests/src/test/groovy/software/betamax/BasicAuthSpec.groovy
  11. +1 −1 betamax-tests/src/test/groovy/software/betamax/LocalhostSpec.groovy
  12. +1 −1 betamax-tests/src/test/groovy/software/betamax/NoJUnitSpec.groovy
  13. +1 −1 betamax-tests/src/test/groovy/software/betamax/OCSPSpec.groovy
  14. +1 −1 betamax-tests/src/test/groovy/software/betamax/SmokeSpec.groovy
  15. +2 −2 betamax-tests/src/test/groovy/software/betamax/compatibility/HttpClient3Spec.groovy
  16. +2 −2 betamax-tests/src/test/groovy/software/betamax/compatibility/HttpClientSpec.groovy
  17. +2 −2 betamax-tests/src/test/groovy/software/betamax/compatibility/HttpURLConnectionSpec.groovy
  18. +2 −2 betamax-tests/src/test/groovy/software/betamax/compatibility/UnirestSpec.groovy
  19. +2 −2 betamax-tests/src/test/groovy/software/betamax/compatibility/WsLiteSpec.groovy
  20. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/CustomSecureSocketFactorySpec.groovy
  21. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/DisconnectedHttpsSpec.groovy
  22. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/HttpsSpec.groovy
  23. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/IgnoreHostsSpec.groovy
  24. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/NoTapeSpec.groovy
  25. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/PreExistingProxySpec.groovy
  26. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/ProxyAuthenticationSpec.groovy
  27. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/ProxyRecordAndPlaybackSpec.groovy
  28. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/ProxyTimeoutSpec.groovy
  29. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/RequestMethodsSpec.groovy
  30. +2 −2 betamax-tests/src/test/groovy/software/betamax/proxy/concurrency/ConcurrencyTest.groovy
  31. +3 −3 betamax-tests/src/test/groovy/software/betamax/proxy/matchRules/CustomMatcherSpec.groovy
  32. +8 −8 betamax-tests/src/test/groovy/software/betamax/recorder/ConfigurationSpec.groovy
  33. +7 −2 betamax-tests/src/test/groovy/software/betamax/tape/CustomMatchRuleSpec.groovy
  34. +2 −2 betamax-tests/src/test/groovy/software/betamax/tape/PostBodySpec.groovy
  35. +2 −2 betamax-tests/src/test/groovy/software/betamax/tape/ProxyMultiThreadedTapeWritingSpec.groovy
  36. +1 −2 betamax-tests/src/test/groovy/software/betamax/tape/ProxyTapeModeSpec.groovy
@@ -19,11 +19,16 @@
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableSet;
import software.betamax.internal.RecorderListener;
import software.betamax.proxy.ProxyConfigurationException;
import software.betamax.proxy.ProxyServer;
import software.betamax.tape.EntityStorage;
import software.betamax.util.Network;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.Collection;
import java.util.Properties;
/**
* The configuration used by Betamax.
@@ -48,22 +53,55 @@
public static final MatchRule DEFAULT_MATCH_RULE = ComposedMatchRule.of(MatchRules.method, MatchRules.uri);
public static final EntityStorage DEFAULT_RESPONSE_BODY_STORAGE = EntityStorage.inline;
public static final String DEFAULT_PROXY_HOST = "0.0.0.0";
public static final int DEFAULT_REQUEST_BUFFER_SIZE = 8388608; //8MB
public static final int DEFAULT_PROXY_PORT = 5555;
public static final int DEFAULT_PROXY_TIMEOUT = 5;
private final File tapeRoot;
private final TapeMode defaultMode;
private final ImmutableCollection<String> ignoreHosts;
private final boolean ignoreLocalhost;
private final MatchRule defaultMatchRule;
protected Configuration(ConfigurationBuilder<?> builder) {
private final String proxyHost;
private final int proxyPort;
private final String proxyUser;
private final String proxyPassword;
private final int proxyTimeoutSeconds;
private final boolean sslEnabled;
private final int requestBufferSize;
protected Configuration(ConfigurationBuilder builder) {
this.tapeRoot = builder.tapeRoot;
this.defaultMode = builder.defaultMode;
this.defaultMatchRule = builder.defaultMatchRule;
this.ignoreHosts = builder.ignoreHosts;
this.ignoreLocalhost = builder.ignoreLocalhost;
this.proxyHost = builder.proxyHost;
this.proxyPort = builder.proxyPort;
this.proxyUser = builder.proxyUser;
this.proxyPassword = builder.proxyPassword;
this.proxyTimeoutSeconds = builder.proxyTimeoutSeconds;
this.sslEnabled = builder.sslEnabled;
this.requestBufferSize = builder.requestBufferSize;
}
public static ConfigurationBuilder<?> builder() {
return new Builder().configureFromPropertiesFile();
public static ConfigurationBuilder builder() {
try {
ConfigurationBuilder builder = new ConfigurationBuilder();
URL propertiesFile = Configuration.class.getResource("/betamax.properties");
if (propertiesFile != null) {
Properties properties = new Properties();
properties.load(propertiesFile.openStream());
return builder.withProperties(properties);
} else {
return builder;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
@@ -113,18 +151,80 @@ public boolean isIgnoreLocalhost() {
}
/**
* Called by the `Recorder` instance so that the configuration can add listeners.
*
* You should **not** call this method yourself.
* The port the Betamax proxy will listen on.
*/
public void registerListeners(Collection<RecorderListener> listeners) {
public int getProxyPort() {
return proxyPort;
}
/**
* The time (in seconds) the proxy will wait before aborting a request.
*/
public int getProxyTimeoutSeconds() {
return proxyTimeoutSeconds;
}
/**
* The buffer size the proxy will use to aggregate incoming requests.
* Needed if you want to match on request body.
*/
public int getRequestBufferSize() {
return requestBufferSize;
}
private static class Builder extends ConfigurationBuilder<Builder> {
@Override
protected Builder self() {
return this;
/**
* If set to true add support for proxying SSL (disable certificate
* checking).
*/
public boolean isSslEnabled() {
return sslEnabled;
}
/**
* @return the hostname or address where the proxy will run. A value of
* `null` means the proxy will bind to any local address.
* @see java.net.InetSocketAddress#InetSocketAddress(InetAddress, int)
*/
public InetAddress getProxyHost() {
try {
if (proxyHost == null) {
return InetAddress.getByName(DEFAULT_PROXY_HOST);
} else {
return InetAddress.getByName(proxyHost);
}
} catch (UnknownHostException e) {
throw new ProxyConfigurationException(String.format("Unable to resolve host %s", proxyHost), e);
}
}
/**
* The username required to authenticate with the proxy.
*/
public String getProxyUser() {
return proxyUser;
}
/**
* The password required to authenticate with the proxy.
*/
public String getProxyPassword() {
return proxyPassword;
}
/**
* @return a `java.net.Proxy` instance configured to point to the Betamax
* proxy.
*/
public Proxy getProxy() {
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(getProxyHost(), getProxyPort()));
}
/**
* Called by the `Recorder` instance so that the configuration can add listeners.
*
* You should **not** call this method yourself.
*/
public void registerListeners(Collection<RecorderListener> listeners) {
listeners.add(new ProxyServer(this));
}
}
@@ -21,16 +21,13 @@
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import software.betamax.util.TypedProperties;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
public abstract class ConfigurationBuilder<T extends ConfigurationBuilder<T>> {
protected abstract T self();
public class ConfigurationBuilder {
public Configuration build() {
return new Configuration(this);
@@ -42,22 +39,15 @@ public Configuration build() {
protected ImmutableCollection<String> ignoreHosts = ImmutableList.of();
protected boolean ignoreLocalhost;
protected T configureFromPropertiesFile() {
try {
URL propertiesFile = Configuration.class.getResource("/betamax.properties");
if (propertiesFile != null) {
Properties properties = new Properties();
properties.load(propertiesFile.openStream());
withProperties(properties);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return self();
}
protected String proxyHost = Configuration.DEFAULT_PROXY_HOST;
protected int proxyPort = Configuration.DEFAULT_PROXY_PORT;
protected String proxyUser;
protected String proxyPassword;
protected int proxyTimeoutSeconds = Configuration.DEFAULT_PROXY_TIMEOUT;
protected int requestBufferSize = Configuration.DEFAULT_REQUEST_BUFFER_SIZE;
protected boolean sslEnabled;
public T withProperties(Properties properties) {
public ConfigurationBuilder withProperties(Properties properties) {
if (properties.containsKey("betamax.tapeRoot")) {
tapeRoot(new File(properties.getProperty("betamax.tapeRoot")));
}
@@ -84,37 +74,91 @@ public MatchRule apply(String input) {
ignoreLocalhost(Boolean.valueOf(properties.getProperty("betamax.ignoreLocalhost")));
}
return self();
if (properties.containsKey("betamax.proxyHost")) {
proxyHost(properties.getProperty("betamax.proxyHost"));
}
if (properties.containsKey("betamax.proxyPort")) {
proxyPort(TypedProperties.getInteger(properties, "betamax.proxyPort"));
}
if (properties.containsKey("betamax.proxyTimeoutSeconds")) {
proxyTimeoutSeconds(TypedProperties.getInteger(properties, "betamax.proxyTimeoutSeconds"));
}
if (properties.containsKey("betamax.requestBufferSize")) {
requestBufferSize(TypedProperties.getInteger(properties, "betamax.requestBufferSize"));
}
if (properties.containsKey("betamax.sslEnabled")) {
sslEnabled(TypedProperties.getBoolean(properties, "betamax.sslEnabled"));
}
return this;
}
public T tapeRoot(File tapeRoot) {
public ConfigurationBuilder tapeRoot(File tapeRoot) {
this.tapeRoot = tapeRoot;
return self();
return this;
}
public T defaultMode(TapeMode defaultMode) {
public ConfigurationBuilder defaultMode(TapeMode defaultMode) {
this.defaultMode = defaultMode;
return self();
return this;
}
public T defaultMatchRule(MatchRule defaultMatchRule) {
public ConfigurationBuilder defaultMatchRule(MatchRule defaultMatchRule) {
this.defaultMatchRule = defaultMatchRule;
return self();
return this;
}
public T defaultMatchRules(MatchRule... defaultMatchRules) {
public ConfigurationBuilder defaultMatchRules(MatchRule... defaultMatchRules) {
this.defaultMatchRule = ComposedMatchRule.of(defaultMatchRules);
return self();
return this;
}
public T ignoreHosts(Iterable<String> ignoreHosts) {
public ConfigurationBuilder ignoreHosts(Iterable<String> ignoreHosts) {
this.ignoreHosts = ImmutableList.copyOf(ignoreHosts);
return self();
return this;
}
public T ignoreLocalhost(boolean ignoreLocalhost) {
public ConfigurationBuilder ignoreLocalhost(boolean ignoreLocalhost) {
this.ignoreLocalhost = ignoreLocalhost;
return self();
return this;
}
public ConfigurationBuilder proxyHost(String proxyHost) {
this.proxyHost = proxyHost;
return this;
}
public ConfigurationBuilder proxyPort(int proxyPort) {
this.proxyPort = proxyPort;
return this;
}
public ConfigurationBuilder proxyAuth(String username, String password) {
if (username == null || password == null) {
throw new IllegalArgumentException("The required proxy username and password cannot be null");
}
this.proxyUser = username;
this.proxyPassword = password;
return this;
}
public ConfigurationBuilder proxyTimeoutSeconds(int proxyTimeoutSeconds) {
this.proxyTimeoutSeconds = proxyTimeoutSeconds;
return this;
}
public ConfigurationBuilder requestBufferSize(int requestBufferSize){
this.requestBufferSize = requestBufferSize;
return this;
}
public ConfigurationBuilder sslEnabled(boolean sslEnabled) {
this.sslEnabled = sslEnabled;
return this;
}
}
Oops, something went wrong.

0 comments on commit 81c8d19

Please sign in to comment.