Skip to content

Commit

Permalink
Creates a ProxyConfigurationFilter hook and cleans up the related Guice
Browse files Browse the repository at this point in the history
bindings.

git-svn-id: http://js-test-driver.googlecode.com/svn/trunk@887 205c1ca0-44cf-11de-84a6-839b6ed6c72e
  • Loading branch information
robertsdionne committed Feb 16, 2011
1 parent a2a7d05 commit e32227e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 40 deletions.
40 changes: 1 addition & 39 deletions JsTestDriver/src/com/google/jstestdriver/JsTestDriverModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,50 +137,12 @@ protected void configure() {
.toInstance(plugins);
bind(Integer.class).annotatedWith(BrowserCount.class).
toProvider(BrowserCountProvider.class).in(Singleton.class);
bind(JsonArray.class).annotatedWith(Names.named("rawProxy"))
.toInstance(proxyConfig);
bind(JsonArray.class).annotatedWith(Names.named("proxy"))
.toProvider(ProxyConfigurationProvider.class);
bind(JsonArray.class).annotatedWith(Names.named("proxy")).toInstance(proxyConfig);
bind(ConfigureProxyAction.Factory.class).toProvider(
FactoryProvider.newFactory(ConfigureProxyAction.Factory.class,
ConfigureProxyAction.class));
}

private static class ProxyConfigurationProvider implements Provider<JsonArray> {

private final JsonArray proxyConfig;

@Inject(optional = true)
private ProxyDestination destination;

@Inject
public ProxyConfigurationProvider(@Named("rawProxy") JsonArray proxyConfig) {
this.proxyConfig = proxyConfig;
}

public JsonArray get() {
if (destination == null) {
return proxyConfig;
} else {
JsonArray copy = copy(proxyConfig);
JsonObject defaultDestination = new JsonObject();
defaultDestination.addProperty("matcher", "*");
defaultDestination.addProperty(
"server", destination.getDestinationAddress());
copy.add(defaultDestination);
return copy;
}
}

private JsonArray copy(JsonArray original) {
JsonArray copy = new JsonArray();
for (JsonElement element : original) {
copy.add(element);
}
return copy;
}
}

/**
* Provides the number of browsers. Needed by any code that is aware of the threading model for
* running tests in multiple browsers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.inject.name.Named;
import com.google.jstestdriver.Action;
import com.google.jstestdriver.Server;
import com.google.jstestdriver.hooks.ProxyConfigurationFilter;
import com.google.jstestdriver.model.HandlerPathPrefix;
import com.google.jstestdriver.model.RunData;

Expand All @@ -24,23 +25,26 @@ public interface Factory {
private final HandlerPathPrefix prefixer;
private final String baseUrl;
private final Server server;
private final ProxyConfigurationFilter filter;
private final JsonArray proxyConfig;

@Inject
public ConfigureProxyAction(
@Named("serverHandlerPrefix") HandlerPathPrefix prefixer,
@Named("server") String baseUrl,
Server server,
ProxyConfigurationFilter filter,
@Assisted JsonArray proxyConfig) {
this.prefixer = prefixer;
this.baseUrl = baseUrl;
this.server = server;
this.filter = filter;
this.proxyConfig = proxyConfig;
}

public RunData run(RunData runData) {
if (proxyConfig != null && proxyConfig.size() > 0) {
server.postJson(baseUrl + "/proxy", proxyConfig);
server.postJson(baseUrl + "/proxy", filter.filter(proxyConfig));
}
return runData;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2011 Google Inc. All Rights Reserved.
package com.google.jstestdriver.hooks;

import com.google.gson.JsonArray;
import com.google.inject.ImplementedBy;
import com.google.jstestdriver.requesthandlers.DefaultProxyConfigurationFilter;

/**
* @author [email protected] (Robert Dionne)
*/
@ImplementedBy(DefaultProxyConfigurationFilter.class)
public interface ProxyConfigurationFilter {

JsonArray filter(JsonArray proxyConfig);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2011 Google Inc. All Rights Reserved.
package com.google.jstestdriver.requesthandlers;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import com.google.jstestdriver.hooks.ProxyConfigurationFilter;
import com.google.jstestdriver.hooks.ProxyDestination;

/**
* @author [email protected] (Robert Dionne)
*/
public class DefaultProxyConfigurationFilter implements ProxyConfigurationFilter {

@Inject(optional=true) private ProxyDestination destination;

public JsonArray filter(JsonArray proxyConfig) {
if (destination == null) {
return proxyConfig;
} else {
JsonObject entry = new JsonObject();
entry.addProperty("matcher", "*");
entry.addProperty("server", destination.getDestinationAddress());
proxyConfig = copy(proxyConfig);
proxyConfig.add(entry);
return proxyConfig;
}
}

private JsonArray copy(JsonArray original) {
JsonArray copy = new JsonArray();
for (JsonElement element : original) {
copy.add(element);
}
return copy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2011 Google Inc. All Rights Reserved.
package com.google.jstestdriver.requesthandlers;

/**
* @author [email protected] (Robert Dionne)
*/
public class ProxyMapping {
public String matcher;
public String server;
}

0 comments on commit e32227e

Please sign in to comment.