use of org.apache.jmeter.protocol.http.config.GraphQLRequestParams in project jmeter by apache.
the class GraphQLRequestParamUtils method toGraphQLRequestParams.
/**
* Parse {@code arguments} and convert it to a {@link GraphQLRequestParams} object if it has valid GraphQL HTTP arguments.
* @param arguments arguments
* @param contentEncoding content encoding
* @return a converted {@link GraphQLRequestParams} object form the {@code arguments}
* @throws IllegalArgumentException if {@code arguments} does not contain valid GraphQL request arguments
* @throws UnsupportedEncodingException if it fails to decode parameter value
*/
public static GraphQLRequestParams toGraphQLRequestParams(final Arguments arguments, final String contentEncoding) throws UnsupportedEncodingException {
final String encoding = StringUtils.defaultIfEmpty(contentEncoding, EncoderCache.URL_ARGUMENT_ENCODING);
String operationName = null;
String query = null;
String variables = null;
for (JMeterProperty prop : arguments) {
final Argument arg = (Argument) prop.getObjectValue();
if (!(arg instanceof HTTPArgument)) {
continue;
}
final String name = arg.getName();
final String metadata = arg.getMetaData();
final String value = StringUtils.trimToNull(arg.getValue());
if ("=".equals(metadata) && value != null) {
final boolean alwaysEncoded = ((HTTPArgument) arg).isAlwaysEncoded();
if (OPERATION_NAME_FIELD.equals(name)) {
operationName = encodedField(value, encoding, alwaysEncoded);
} else if (QUERY_FIELD.equals(name)) {
query = encodedField(value, encoding, alwaysEncoded);
} else if (VARIABLES_FIELD.equals(name)) {
variables = encodedField(value, encoding, alwaysEncoded);
}
}
}
if (isNoQueryOrMutation(query)) {
throw new IllegalArgumentException("Not a valid GraphQL query.");
}
if (isNoJsonObject(variables)) {
throw new IllegalArgumentException("Not a valid object node for GraphQL variables.");
}
return new GraphQLRequestParams(operationName, query, variables);
}
use of org.apache.jmeter.protocol.http.config.GraphQLRequestParams in project jmeter by apache.
the class GraphQLRequestParamUtils method toGraphQLRequestParams.
/**
* Parse {@code postData} and convert it to a {@link GraphQLRequestParams} object if it is a valid GraphQL post data.
* @param postData post data
* @param contentEncoding content encoding
* @return a converted {@link GraphQLRequestParams} object form the {@code postData}
* @throws IllegalArgumentException if {@code postData} is not a GraphQL post JSON data or not a valid JSON
* @throws JsonProcessingException if it fails to serialize a parsed JSON object to string
* @throws UnsupportedEncodingException if it fails to decode parameter value
*/
public static GraphQLRequestParams toGraphQLRequestParams(byte[] postData, final String contentEncoding) throws JsonProcessingException, UnsupportedEncodingException {
final String encoding = StringUtils.isNotEmpty(contentEncoding) ? contentEncoding : EncoderCache.URL_ARGUMENT_ENCODING;
final ObjectMapper mapper = new ObjectMapper();
ObjectNode data;
try (InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(postData), encoding)) {
data = mapper.readValue(reader, ObjectNode.class);
} catch (IOException e) {
throw new IllegalArgumentException("Invalid json data: " + e.getLocalizedMessage(), e);
}
String operationName = null;
String query;
String variables = null;
final JsonNode operationNameNode = data.has(OPERATION_NAME_FIELD) ? data.get(OPERATION_NAME_FIELD) : null;
if (operationNameNode != null) {
operationName = getJsonNodeTextContent(operationNameNode, true);
}
if (!data.has(QUERY_FIELD)) {
throw new IllegalArgumentException("Not a valid GraphQL query.");
}
final JsonNode queryNode = data.get(QUERY_FIELD);
query = getJsonNodeTextContent(queryNode, false);
final String trimmedQuery = StringUtils.trim(query);
if (!StringUtils.startsWith(trimmedQuery, QUERY_FIELD) && !StringUtils.startsWith(trimmedQuery, "mutation")) {
throw new IllegalArgumentException("Not a valid GraphQL query.");
}
final JsonNode variablesNode = data.has(VARIABLES_FIELD) ? data.get(VARIABLES_FIELD) : null;
if (variablesNode != null) {
final JsonNodeType nodeType = variablesNode.getNodeType();
if (nodeType != JsonNodeType.NULL) {
if (nodeType == JsonNodeType.OBJECT) {
variables = mapper.writeValueAsString(variablesNode);
} else {
throw new IllegalArgumentException("Not a valid object node for GraphQL variables.");
}
}
}
return new GraphQLRequestParams(operationName, query, variables);
}
Aggregations