Search in sources :

Example 6 with GraphQLRequestParams

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);
}
Also used : GraphQLRequestParams(org.apache.jmeter.protocol.http.config.GraphQLRequestParams) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Argument(org.apache.jmeter.config.Argument)

Example 7 with GraphQLRequestParams

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);
}
Also used : GraphQLRequestParams(org.apache.jmeter.protocol.http.config.GraphQLRequestParams) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonNodeType(com.fasterxml.jackson.databind.node.JsonNodeType) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

GraphQLRequestParams (org.apache.jmeter.protocol.http.config.GraphQLRequestParams)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IOException (java.io.IOException)2 Arguments (org.apache.jmeter.config.Arguments)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonNodeType (com.fasterxml.jackson.databind.node.JsonNodeType)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 MalformedURLException (java.net.MalformedURLException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Argument (org.apache.jmeter.config.Argument)1 Header (org.apache.jmeter.protocol.http.control.Header)1 GraphQLHTTPSamplerGui (org.apache.jmeter.protocol.http.control.gui.GraphQLHTTPSamplerGui)1 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)1 TestElementProperty (org.apache.jmeter.testelement.property.TestElementProperty)1 SAXException (org.xml.sax.SAXException)1