Skip to content

Commit

Permalink
implemented support for loading external js files and make them avail…
Browse files Browse the repository at this point in the history
…able for expectations and let - generalised approach issue #157 commit 6cafe9d
  • Loading branch information
smartrics committed Mar 16, 2016
1 parent f4a81f6 commit 7983e93
Show file tree
Hide file tree
Showing 22 changed files with 1,014 additions and 965 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class PartsFactory {

private final BodyTypeAdapterFactory bodyTypeAdapterFactory;

public PartsFactory(final RunnerVariablesProvider variablesProvider) {
this.bodyTypeAdapterFactory = new BodyTypeAdapterFactory(variablesProvider);
public PartsFactory(final RunnerVariablesProvider variablesProvider, Config config) {
this.bodyTypeAdapterFactory = new BodyTypeAdapterFactory(variablesProvider, config);
}

/**
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/smartrics/rest/fitnesse/fixture/RestFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
* </tr>
* <tr>
* <td>restfixture.content.handlers.map</td>
* <td><i>a map of contenty type to type adapters, entries separated by \n, and
* <td><i>a map of content type to type adapters, entries separated by \n, and
* kye-value separated by '='. Available type adapters are JS, TEXT, JSON, XML
* (see {@link smartrics.rest.fitnesse.fixture.support.BodyTypeAdapterFactory}
* ).</i></td>
Expand All @@ -171,6 +171,14 @@
* set for this config key. This value can also be the empty string to replace
* null with empty.</i></td>
* </tr>
* <tr>
* <td>restfixture.javascript.imports.map</td>
* <td><i>a map of name to Url/File path. Each entry refers to a url/path to a javascript file that is imported in the
* JS context and available for evaluation. Files are checked for existence and access. If not available/not accessible,
* an exception is thrown. Urls are tried to be downloaded. A failure in accessing the content causes an error/exception.
* The map key is the name of the library - it can be a freeform string that appears in logs for debugging purposes.
* </i></td>
* </tr>
*
* </table>
*
Expand Down Expand Up @@ -282,7 +290,7 @@ public Variables createRunnerVariables() {
*/
public RestFixture() {
super();
this.partsFactory = new PartsFactory(this);
this.partsFactory = new PartsFactory(this, Config.getConfig(Config.DEFAULT_CONFIG_NAME));
this.displayActualOnRight = true;
this.minLenForCollapseToggle = -1;
this.resourceUrisAreEscaped = false;
Expand Down Expand Up @@ -313,19 +321,20 @@ public RestFixture(String hostName, String configName) {
this.minLenForCollapseToggle = -1;
this.resourceUrisAreEscaped = false;
this.displayAbsoluteURLInFull = true;
this.partsFactory = new PartsFactory(this);
this.config = Config.getConfig(configName);
this.partsFactory = new PartsFactory(this, config);
this.baseUrl = new Url(stripTag(hostName));
this.requestHeaders = new LinkedHashMap<String,String>();
}

/**
* VisibleForTesting
* @param partsFactory
* the factory of parts necessary to create the rest fixture
* @param hostName
* @param configName
*/
public RestFixture(PartsFactory partsFactory, String hostName, String configName) {
RestFixture(PartsFactory partsFactory, String hostName, String configName) {
this.displayActualOnRight = true;
this.minLenForCollapseToggle = -1;
this.resourceUrisAreEscaped = false;
Expand Down Expand Up @@ -809,7 +818,7 @@ public void let() {
if (letHandler != null) {
StringTypeAdapter adapter = new StringTypeAdapter();
try {
sValue = letHandler.handle(this, getLastResponse(), namespaceContext, expr);
sValue = letHandler.handle(this, config, getLastResponse(), namespaceContext, expr);
exprCell.body(getFormatter().gray(exprCell.body()));
} catch (RuntimeException e) {
getFormatter().exception(exprCell, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@
public class BodyTypeAdapterFactory {

private final RunnerVariablesProvider variablesProvider;
private Map<ContentType, BodyTypeAdapterCreator> contentTypeToBodyTypeAdapter =
new HashMap<ContentType, BodyTypeAdapterCreator>();
private final Config config;

private Map<ContentType, BodyTypeAdapterCreator> contentTypeToBodyTypeAdapter = new HashMap<ContentType, BodyTypeAdapterCreator>();
{
BodyTypeAdapterCreator jsonBodyTypeAdapterCreator = new BodyTypeAdapterCreator() {
@Override
public BodyTypeAdapter createBodyTypeAdapter() {
return new JSONBodyTypeAdapter(variablesProvider);
return new JSONBodyTypeAdapter(variablesProvider, config);
}
};

contentTypeToBodyTypeAdapter.put(ContentType.JS, jsonBodyTypeAdapterCreator);
contentTypeToBodyTypeAdapter.put(ContentType.JSON, jsonBodyTypeAdapterCreator);
contentTypeToBodyTypeAdapter.put(ContentType.XML, new BodyTypeAdapterCreator() {
Expand All @@ -62,8 +63,9 @@ public BodyTypeAdapter createBodyTypeAdapter() {
});;
}

public BodyTypeAdapterFactory(final RunnerVariablesProvider variablesProvider) {
public BodyTypeAdapterFactory(final RunnerVariablesProvider variablesProvider, Config config) {
this.variablesProvider = variablesProvider;
this.config = config;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,12 @@ public Integer getAsInteger(String key, Integer def) {
* @return a map representing the key value.
*/
public Map<String, String> getAsMap(String key, Map<String, String> def) {
Map<String, String> returnMap = new HashMap<String, String>(def);
String val = get(key);
try {
return Tools.convertStringToMap(val, "\n", "=", true);
Map<String, String> result = Tools.convertStringToMap(val, "=", "\n", true);
returnMap.putAll(result);
return returnMap;
} catch (RuntimeException e) {
return def;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class FitVariables extends Variables {

/**
* initialises variables with default config. See @link
* {@link #Variables(Config)}
* {@link #FitVariables(Config)}
*/
public FitVariables() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,30 @@
*/
package smartrics.rest.fitnesse.fixture.support;

import java.util.List;

import smartrics.rest.fitnesse.fixture.RunnerVariablesProvider;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Type adapted for cells containing JSON content.
*
*
* @author smartrics
*
*/
public class JSONBodyTypeAdapter extends XPathBodyTypeAdapter {
private boolean forceJsEvaluation = false;
private final Map<String, String> imports;
private final JavascriptWrapper wrapper;
private boolean forceJsEvaluation = false;

/**
* def ctor
* @param variablesProvider
*
* @param variablesProvider
*/
public JSONBodyTypeAdapter(RunnerVariablesProvider variablesProvider) {
wrapper = new JavascriptWrapper(variablesProvider);
public JSONBodyTypeAdapter(RunnerVariablesProvider variablesProvider, Config config) {
wrapper = new JavascriptWrapper(variablesProvider);
imports = config.getAsMap("restfixture.javascript.imports.map", new HashMap<String, String>());
}

@Override
Expand All @@ -48,7 +52,7 @@ protected boolean eval(String expr, String json) {
if (!forceJsEvaluation && Tools.isValidXPath(getContext(), expr) && !wrapper.looksLikeAJsExpression(expr)) {
throw new IllegalArgumentException("XPath expectations in JSON content are not supported anymore. Please use JavaScript expressions.");
}
Object exprResult = wrapper.evaluateExpression(json, expr);
Object exprResult = wrapper.evaluateExpression(json, expr, imports);
if (exprResult == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public class JavascriptException extends RuntimeException {
public JavascriptException(String message) {
super(message);
}

public JavascriptException(String message, Throwable t) {
super(message, t);
}
}
Loading

0 comments on commit 7983e93

Please sign in to comment.