Skip to content

Commit

Permalink
[rewrite] #1281 mocks done, but with new api, see prev commit for diff
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Nov 10, 2020
1 parent a83f76e commit 7132686
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@
* @author pthomas3
*/
public class MockHandler implements ServerHandler {

private static final Logger logger = LoggerFactory.getLogger(MockHandler.class);

private static final String REQUEST_BYTES = "requestBytes";
private static final String REQUEST_PARAMS = "requestParams";
private static final String REQUEST_FILES = "requestFiles";

private static final String RESPONSE_DELAY = "responseDelay";

private static final String PATH_MATCHES = "pathMatches";
private static final String METHOD_IS = "methodIs";
private static final String TYPE_CONTAINS = "typeContains";
Expand All @@ -68,17 +68,22 @@ public class MockHandler implements ServerHandler {
private static final String PARAM_EXISTS = "paramExists";
private static final String PATH_PARAMS = "pathParams";
private static final String BODY_PATH = "bodyPath";

private final Feature feature;
private final String featureName;
private final ScenarioRuntime runtime; // holds global config and vars

protected static final ThreadLocal<Request> LOCAL_REQUEST = new ThreadLocal<Request>();

public MockHandler(Feature feature) {
this(feature, null);
}

public MockHandler(Feature feature, Map<String, Object> args) {
this.feature = feature;
featureName = feature.getPath().toFile().getName();
FeatureRuntime featureRuntime = new FeatureRuntime(SuiteRuntime.forMock(), feature);
featureRuntime.setCallArg(args);
FeatureSection section = new FeatureSection();
section.setIndex(-1); // TODO util for creating dummy scenario
Scenario dummy = new Scenario(feature, section, -1);
Expand All @@ -93,6 +98,7 @@ public MockHandler(Feature feature) {
runtime.engine.setVariable(HEADER_CONTAINS, (BiFunction<String, String, Boolean>) this::headerContains);
runtime.engine.setVariable(BODY_PATH, (Function<String, Object>) this::bodyPath);
if (feature.isBackgroundPresent()) {
ScenarioEngine.set(runtime.engine);
runtime.engine.init();
for (Step step : feature.getBackground().getSteps()) {
Result result = StepRuntime.execute(step, runtime.actions);
Expand All @@ -105,9 +111,9 @@ public MockHandler(Feature feature) {
}
runtime.logger.info("mock server initialized: {}", featureName);
}

private static final Result PASSED = Result.passed(0);

@Override
public Response handle(Request req) {
Thread.currentThread().setContextClassLoader(runtime.featureRuntime.suite.classLoader);
Expand Down Expand Up @@ -183,7 +189,9 @@ public Response handle(Request req) {
res.setBody(response.getAsByteArray());
if (res.getContentType() == null) {
ResourceType rt = ResourceType.fromObject(response.getValue());
res.setContentType(rt.contentType);
if (rt != null) {
res.setContentType(rt.contentType);
}
}
}
if (responseStatus != null) {
Expand All @@ -196,11 +204,12 @@ public Response handle(Request req) {
runtime.logger.warn("no scenarios matched, returning 404: {}", req);
return new Response(404);
}

private boolean isMatchingScenario(Scenario scenario, ScenarioEngine engine) {
String expression = StringUtils.trimToNull(scenario.getName() + scenario.getDescription());
if (expression == null) {
runtime.logger.debug("default scenario matched at line: {}", scenario.getLine());
return true;
}
try {
Variable v = engine.evalJs(expression);
Expand All @@ -216,7 +225,7 @@ private boolean isMatchingScenario(Scenario scenario, ScenarioEngine engine) {
return false;
}
}

public boolean pathMatches(String pattern) {
String uri = LOCAL_REQUEST.get().getPath();
Map<String, String> pathParams = HttpUtils.parseUriPattern(pattern, uri);
Expand All @@ -227,30 +236,30 @@ public boolean pathMatches(String pattern) {
return true;
}
}

public boolean paramExists(String name) {
Map<String, List<String>> params = LOCAL_REQUEST.get().getParams();
return params == null ? false : params.containsKey(name);
}

public String paramValue(String name) {
return LOCAL_REQUEST.get().getParam(name);
}

public boolean methodIs(String name) { // TODO no more supporting array arg
return LOCAL_REQUEST.get().getMethod().equalsIgnoreCase(name);
}

public boolean typeContains(String text) {
String contentType = LOCAL_REQUEST.get().getContentType();
return contentType == null ? false : contentType.contains(text);
}

public boolean acceptContains(String text) {
String acceptHeader = LOCAL_REQUEST.get().getHeader("Accept");
return acceptHeader == null ? false : acceptHeader.contains(text);
}

public boolean headerContains(String name, String value) {
List<String> values = LOCAL_REQUEST.get().getHeaderValues(name);
if (values != null) {
Expand All @@ -262,7 +271,7 @@ public boolean headerContains(String name, String value) {
}
return false;
}

public Object bodyPath(String path) {
Object body = LOCAL_REQUEST.get().getBodyConverted();
if (body == null) {
Expand All @@ -276,5 +285,5 @@ public Object bodyPath(String path) {
return JsValue.fromJava(json.get(path));
}
}

}
125 changes: 125 additions & 0 deletions karate-core/src/main/java/com/intuit/karate/runtime/MockServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* The MIT License
*
* Copyright 2020 Intuit Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.intuit.karate.runtime;

import com.intuit.karate.core.Feature;
import com.intuit.karate.core.FeatureParser;
import com.intuit.karate.server.HttpServer;
import com.intuit.karate.server.ServerHandler;
import com.intuit.karate.server.SslContextFactory;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServerBuilder;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author pthomas3
*/
public class MockServer extends HttpServer {

private MockServer(ServerBuilder sb, ServerHandler handler) {
super(sb, handler);
}

public static class Builder {

Builder(Feature feature) {
this.feature = feature;
}

final Feature feature;
int port;
boolean ssl;
File certFile;
File keyFile;
Map<String, Object> args;

public Builder http(int value) {
port = value;
return this;
}

public Builder https(int value) {
ssl = true;
port = value;
return this;
}

public Builder certFile(File value) {
certFile = value;
return this;
}

public Builder keyFile(File value) {
keyFile = value;
return this;
}

public Builder args(Map<String, Object> value) {
args = value;
return this;
}

public Builder arg(String name, Object value) {
if (args == null) {
args = new HashMap();
}
args.put(name, value);
return this;
}

public MockServer build() {
ServerBuilder sb = Server.builder();
if (ssl) {
sb.https(port);
SslContextFactory factory = new SslContextFactory();
factory.setCertFile(certFile);
factory.setKeyFile(keyFile);
factory.build();
sb.tls(factory.getCertFile(), factory.getKeyFile());
} else {
sb.http(port);
}
MockHandler mockHandler = new MockHandler(feature, args);
return new MockServer(sb, mockHandler);
}

}

public static Builder feature(String path) {
return new Builder(FeatureParser.parse(path));
}

public static Builder feature(File file) {
return new Builder(FeatureParser.parse(file));
}

public static Builder feature(Feature feature) {
return new Builder(feature);
}

}
Loading

0 comments on commit 7132686

Please sign in to comment.