diff --git a/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java b/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java index a806eb7fa..de2d53ba0 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java +++ b/karate-core/src/main/java/com/intuit/karate/core/MockHandler.java @@ -75,26 +75,22 @@ public class MockHandler implements ServerHandler { private boolean corsEnabled; protected static final ThreadLocal LOCAL_REQUEST = new ThreadLocal<>(); - private String prefix = null; - - public MockHandler withPrefix(String prefix) { - this.prefix = prefix; - return this; - } + private final String prefix; public MockHandler(Feature feature) { this(feature, null); } public MockHandler(Feature feature, Map args) { - this(Collections.singletonList(feature), args); + this(null, Collections.singletonList(feature), args); } public MockHandler(List features) { - this(features, null); + this(null, features, null); } - public MockHandler(List features, Map args) { + public MockHandler(String prefix, List features, Map args) { + this.prefix = "/".equals(prefix) ? null : prefix; for (Feature feature : features) { FeatureRuntime featureRuntime = FeatureRuntime.of(Suite.forTempUse(HttpClientFactory.DEFAULT), feature, args); FeatureSection section = new FeatureSection(); diff --git a/karate-core/src/main/java/com/intuit/karate/core/MockServer.java b/karate-core/src/main/java/com/intuit/karate/core/MockServer.java index 62929ae02..a7094dbc5 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/MockServer.java +++ b/karate-core/src/main/java/com/intuit/karate/core/MockServer.java @@ -129,7 +129,7 @@ public MockServer build() { } else { sb.http(port); } - ServerHandler handler = watch ? new ReloadingMockHandler(features, args, prefix) : new MockHandler(features, args).withPrefix(prefix); + ServerHandler handler = watch ? new ReloadingMockHandler(features, args, prefix) : new MockHandler(prefix, features, args); HttpService service = new HttpServerHandler(handler); sb.service("prefix:" + (prefix == null ? "/" : prefix), service); return new MockServer(sb); @@ -151,7 +151,7 @@ public ReloadingMockHandler(List features, Map args, St this.files.put(f.getResource().getFile(), f.getResource().getFile().lastModified()); } logger.debug("watch mode init - {}", files); - handler = new MockHandler(features, args).withPrefix(prefix); + handler = new MockHandler(prefix, features, args); } @Override @@ -159,7 +159,7 @@ public Response handle(Request request) { boolean reload = files.entrySet().stream().reduce(false, (modified, entry) -> entry.getKey().lastModified() > entry.getValue(), (a, b) -> a || b); if (reload) { List features = files.keySet().stream().map(f -> Feature.read(f)).collect(Collectors.toList()); - handler = new MockHandler(features, args).withPrefix(prefix); + handler = new MockHandler(prefix, features, args); } return handler.handle(request); } diff --git a/karate-core/src/test/java/com/intuit/karate/core/mock/MockTest.java b/karate-core/src/test/java/com/intuit/karate/core/mock/MockTest.java index 399632bba..134e2fba7 100644 --- a/karate-core/src/test/java/com/intuit/karate/core/mock/MockTest.java +++ b/karate-core/src/test/java/com/intuit/karate/core/mock/MockTest.java @@ -22,6 +22,7 @@ static HttpServer startMockServer() { MockServer server = MockServer.featurePaths( "classpath:com/intuit/karate/core/mock/_simple.feature", "classpath:com/intuit/karate/core/mock/_mock.feature") + .pathPrefix("/") // ensure cli default works .build(); System.setProperty("karate.server.port", server.getPort() + ""); return server;