nl.hsac.fitnesse.fixture.slim.JsonHttpTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hsac-fitnesse-fixtures Show documentation
Show all versions of hsac-fitnesse-fixtures Show documentation
Fixtures to assist in testing via FitNesse
package nl.hsac.fitnesse.fixture.slim;
import net.minidev.json.JSONObject;
import nl.hsac.fitnesse.fixture.util.DataUrlHelper;
import nl.hsac.fitnesse.fixture.util.JsonPathHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import java.util.ArrayList;
import java.util.List;
/**
* Fixture to make Http calls and interpret the result as JSON.
*/
public class JsonHttpTest extends HttpTest {
public static final String JSON_CONTENT_TYPE = ContentType.APPLICATION_JSON.toString();
public boolean postValuesAsJsonTo(String serviceUrl) {
return sendToImpl(jsonEncodeCurrentValues(), serviceUrl, getContentTypeForJson(), "POST");
}
public boolean putValuesAsJsonTo(String serviceUrl) {
return sendToImpl(jsonEncodeCurrentValues(), serviceUrl, getContentTypeForJson(), "PUT");
}
public boolean deleteWithValuesAsJson(String serviceUrl) {
return sendToImpl(jsonEncodeCurrentValues(), serviceUrl, getContentTypeForJson(), "DELETE");
}
protected String jsonEncodeCurrentValues() {
return JSONObject.toJSONString(getCurrentValues());
}
protected String getContentTypeForJson() {
// for methods that post JSON we change the default content type to be application/json
String contentType;
if (isExplicitContentTypeSet()) {
contentType = getContentType();
} else {
contentType = JSON_CONTENT_TYPE;
}
return contentType;
}
@Override
protected String formatValue(String value) {
String formatted = super.formatValue(value);
if (value != null) {
String trimmed = value.trim();
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
formatted = getEnvironment().getHtmlForJson(trimmed);
}
}
return formatted;
}
public Object jsonPath(String path) {
String responseString = getResponseBody();
String jsonPath = getPathExpr(path);
return getPathHelper().getJsonPath(responseString, jsonPath);
}
/**
* @param baseName base of filename to generate (a number might be added to the name to make it unique).
* @param jsonPath expression to evaluate.
* @return link to created file.
*/
public String createFileFromBase64ContentOf(String baseName, String jsonPath) {
Object base64Content = jsonPath(jsonPath);
if (base64Content instanceof String) {
if ("".equals(base64Content)) {
throw new SlimFixtureException(false, "No content from json path: '" + getPathExpr(jsonPath) + "'");
} else {
return createFileFromBase64(baseName, (String) base64Content);
}
} else {
throw new SlimFixtureException(false, "Non string result from json path. '" + getPathExpr(jsonPath) + "' returned: " + base64Content);
}
}
@Override
protected String createFileFromBase64(String baseName, String base64Content) {
if (DataUrlHelper.isDataUrl(base64Content)) {
base64Content = DataUrlHelper.getData(base64Content);
}
return super.createFileFromBase64(baseName, base64Content);
}
public Object elementOfJsonPath(int index, String path) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy