Skip to content

Commit

Permalink
fix bad npe bug in app-server js eval helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Mar 21, 2021
1 parent c19eb80 commit dd7a65c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
10 changes: 0 additions & 10 deletions karate-core/src/main/java/com/intuit/karate/http/RequestCycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public static RequestCycle init(JsEngine je) {
}

private final JsEngine engine;

private JsEngine localEngine;
private Session session;
private Response response;
private ServerContext context;
Expand Down Expand Up @@ -88,14 +86,6 @@ public void close() {
THREAD_LOCAL.remove();
}

public JsEngine getLocalEngine() {
return localEngine;
}

public void setLocalEngine(JsEngine localEngine) {
this.localEngine = localEngine;
}

public Session getSession() {
return session;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,15 @@ public String readAsString(String resource) {
public Object read(String resource) {
String raw = readAsString(resource);
ResourceType resourceType = ResourceType.fromFileExtension(resource);
return JsValue.fromString(raw, false, resourceType);
if (resourceType == ResourceType.JS) {
return eval(raw);
} else {
return JsValue.fromString(raw, false, resourceType);
}
}

public Object eval(String source) {
return RequestCycle.get().getLocalEngine().evalForValue("(" + source + ")");
return RequestCycle.get().getEngine().evalForValue(source);
}

public String toJson(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
* @author pthomas3
*/
public class KarateTemplateEngine {

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

private final StandardEngineContextFactory standardFactory;
private final TemplateEngine wrapped;

public KarateTemplateEngine(JsEngine je, IDialect... dialects) {
standardFactory = new StandardEngineContextFactory();
wrapped = new TemplateEngine();
Expand All @@ -70,22 +70,22 @@ public KarateTemplateEngine(JsEngine je, IDialect... dialects) {
wrapped.addDialect(dialect);
}
}

public void setTemplateResolver(ITemplateResolver templateResolver) {
wrapped.setTemplateResolver(templateResolver);
}

public String process(String template) {
return process(template, TemplateContext.LOCALE_US);
}

public String process(String template, IContext context) {
TemplateSpec templateSpec = new TemplateSpec(template, TemplateMode.HTML);
Writer stringWriter = new FastStringWriter(100);
process(templateSpec, context, stringWriter);
return stringWriter.toString();
}

public void process(TemplateSpec templateSpec, IContext context, Writer writer) {
try {
TemplateManager templateManager = wrapped.getConfiguration().getTemplateManager();
Expand Down Expand Up @@ -113,5 +113,5 @@ public void process(TemplateSpec templateSpec, IContext context, Writer writer)
throw new RuntimeException(e);
}
}

}

0 comments on commit dd7a65c

Please sign in to comment.