Skip to content

Commit

Permalink
mock path resolving bug (only affects cli) #2028
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed May 27, 2022
1 parent 6d977d4 commit 4767cd3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,22 @@ public class MockHandler implements ServerHandler {
private boolean corsEnabled;

protected static final ThreadLocal<Request> 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<String, Object> args) {
this(Collections.singletonList(feature), args);
this(null, Collections.singletonList(feature), args);
}

public MockHandler(List<Feature> features) {
this(features, null);
this(null, features, null);
}

public MockHandler(List<Feature> features, Map<String, Object> args) {
public MockHandler(String prefix, List<Feature> features, Map<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -151,15 +151,15 @@ public ReloadingMockHandler(List<Feature> features, Map<String, Object> 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
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<Feature> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4767cd3

Please sign in to comment.