Skip to content

Commit 4767cd3

Browse files
committedMay 27, 2022
mock path resolving bug (only affects cli) #2028
1 parent 6d977d4 commit 4767cd3

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed
 

‎karate-core/src/main/java/com/intuit/karate/core/MockHandler.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,22 @@ public class MockHandler implements ServerHandler {
7575
private boolean corsEnabled;
7676

7777
protected static final ThreadLocal<Request> LOCAL_REQUEST = new ThreadLocal<>();
78-
private String prefix = null;
79-
80-
public MockHandler withPrefix(String prefix) {
81-
this.prefix = prefix;
82-
return this;
83-
}
78+
private final String prefix;
8479

8580
public MockHandler(Feature feature) {
8681
this(feature, null);
8782
}
8883

8984
public MockHandler(Feature feature, Map<String, Object> args) {
90-
this(Collections.singletonList(feature), args);
85+
this(null, Collections.singletonList(feature), args);
9186
}
9287

9388
public MockHandler(List<Feature> features) {
94-
this(features, null);
89+
this(null, features, null);
9590
}
9691

97-
public MockHandler(List<Feature> features, Map<String, Object> args) {
92+
public MockHandler(String prefix, List<Feature> features, Map<String, Object> args) {
93+
this.prefix = "/".equals(prefix) ? null : prefix;
9894
for (Feature feature : features) {
9995
FeatureRuntime featureRuntime = FeatureRuntime.of(Suite.forTempUse(HttpClientFactory.DEFAULT), feature, args);
10096
FeatureSection section = new FeatureSection();

‎karate-core/src/main/java/com/intuit/karate/core/MockServer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public MockServer build() {
129129
} else {
130130
sb.http(port);
131131
}
132-
ServerHandler handler = watch ? new ReloadingMockHandler(features, args, prefix) : new MockHandler(features, args).withPrefix(prefix);
132+
ServerHandler handler = watch ? new ReloadingMockHandler(features, args, prefix) : new MockHandler(prefix, features, args);
133133
HttpService service = new HttpServerHandler(handler);
134134
sb.service("prefix:" + (prefix == null ? "/" : prefix), service);
135135
return new MockServer(sb);
@@ -151,15 +151,15 @@ public ReloadingMockHandler(List<Feature> features, Map<String, Object> args, St
151151
this.files.put(f.getResource().getFile(), f.getResource().getFile().lastModified());
152152
}
153153
logger.debug("watch mode init - {}", files);
154-
handler = new MockHandler(features, args).withPrefix(prefix);
154+
handler = new MockHandler(prefix, features, args);
155155
}
156156

157157
@Override
158158
public Response handle(Request request) {
159159
boolean reload = files.entrySet().stream().reduce(false, (modified, entry) -> entry.getKey().lastModified() > entry.getValue(), (a, b) -> a || b);
160160
if (reload) {
161161
List<Feature> features = files.keySet().stream().map(f -> Feature.read(f)).collect(Collectors.toList());
162-
handler = new MockHandler(features, args).withPrefix(prefix);
162+
handler = new MockHandler(prefix, features, args);
163163
}
164164
return handler.handle(request);
165165
}

‎karate-core/src/test/java/com/intuit/karate/core/mock/MockTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static HttpServer startMockServer() {
2222
MockServer server = MockServer.featurePaths(
2323
"classpath:com/intuit/karate/core/mock/_simple.feature",
2424
"classpath:com/intuit/karate/core/mock/_mock.feature")
25+
.pathPrefix("/") // ensure cli default works
2526
.build();
2627
System.setProperty("karate.server.port", server.getPort() + "");
2728
return server;

0 commit comments

Comments
 (0)
Please sign in to comment.