summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.junit.http
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jgit.junit.http')
-rw-r--r--org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java39
1 files changed, 29 insertions, 10 deletions
diff --git a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java
index 69e2cd5957..e257cf65b6 100644
--- a/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java
+++ b/org.eclipse.jgit.junit.http/src/org/eclipse/jgit/junit/http/AppServer.java
@@ -55,6 +55,7 @@ import java.net.UnknownHostException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
+import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -97,6 +98,9 @@ public class AppServer {
/** SSL keystore password; must have at least 6 characters. */
private static final String keyPassword = "mykeys";
+ /** Role for authentication. */
+ private static final String authRole = "can-access";
+
static {
// Install a logger that throws warning messages.
//
@@ -136,10 +140,10 @@ public class AppServer {
/**
* @param port
- * for https, may be zero to allocate a port dynamically
+ * for http, may be zero to allocate a port dynamically
* @param sslPort
* for https,may be zero to allocate a port dynamically. If
- * negative, the server will be set up without https support..
+ * negative, the server will be set up without https support.
* @since 4.9
*/
public AppServer(int port, int sslPort) {
@@ -264,9 +268,10 @@ public class AppServer {
return ctx;
}
- public ServletContextHandler authBasic(ServletContextHandler ctx) {
+ public ServletContextHandler authBasic(ServletContextHandler ctx,
+ String... methods) {
assertNotYetSetUp();
- auth(ctx, new BasicAuthenticator());
+ auth(ctx, new BasicAuthenticator(), methods);
return ctx;
}
@@ -301,22 +306,36 @@ public class AppServer {
}
}
- private void auth(ServletContextHandler ctx, Authenticator authType) {
- final String role = "can-access";
-
- AbstractLoginService users = new TestMappedLoginService(role);
+ private ConstraintMapping createConstraintMapping() {
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(new Constraint());
cm.getConstraint().setAuthenticate(true);
cm.getConstraint().setDataConstraint(Constraint.DC_NONE);
- cm.getConstraint().setRoles(new String[] { role });
+ cm.getConstraint().setRoles(new String[] { authRole });
cm.setPathSpec("/*");
+ return cm;
+ }
+
+ private void auth(ServletContextHandler ctx, Authenticator authType,
+ String... methods) {
+ AbstractLoginService users = new TestMappedLoginService(authRole);
+ List<ConstraintMapping> mappings = new ArrayList<>();
+ if (methods == null || methods.length == 0) {
+ mappings.add(createConstraintMapping());
+ } else {
+ for (String method : methods) {
+ ConstraintMapping cm = createConstraintMapping();
+ cm.setMethod(method.toUpperCase(Locale.ROOT));
+ mappings.add(cm);
+ }
+ }
ConstraintSecurityHandler sec = new ConstraintSecurityHandler();
sec.setRealmName(realm);
sec.setAuthenticator(authType);
sec.setLoginService(users);
- sec.setConstraintMappings(new ConstraintMapping[] { cm });
+ sec.setConstraintMappings(
+ mappings.toArray(new ConstraintMapping[mappings.size()]));
sec.setHandler(ctx);
contexts.removeHandler(ctx);