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 nl.hsac.fitnesse.fixture.util.JsonPathHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.json.JSONObject;
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 postToImpl(jsonEncodeCurrentValues(), serviceUrl, getContentTypeForJson());
}
public boolean putValuesAsJsonTo(String serviceUrl) {
return putToImpl(jsonEncodeCurrentValues(), serviceUrl, getContentTypeForJson());
}
protected String jsonEncodeCurrentValues() {
return new JSONObject(getCurrentValues()).toString();
}
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 && value.trim().startsWith("{")) {
formatted = getEnvironment().getHtmlForJson(value);
}
return formatted;
}
public Object jsonPath(String path) {
String responseString = getResponseBody();
String jsonPath = getPathExpr(path);
return getPathHelper().getJsonPath(responseString, jsonPath);
}
public Object elementOfJsonPath(int index, String path) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy