Search in sources :

Example 1 with GraphQLRequestParams

use of org.apache.jmeter.protocol.http.config.GraphQLRequestParams in project jmeter by apache.

the class DefaultSamplerCreator method detectAndModifySamplerOnGraphQLRequest.

private void detectAndModifySamplerOnGraphQLRequest(final HTTPSamplerBase sampler, final HttpRequestHdr request) {
    final String method = request.getMethod();
    final Header header = request.getHeaderManager().getFirstHeaderNamed("Content-Type");
    final boolean graphQLContentType = header != null && GraphQLRequestParamUtils.isGraphQLContentType(header.getValue());
    GraphQLRequestParams params = null;
    if (HTTPConstants.POST.equals(method) && graphQLContentType) {
        try {
            byte[] postData = request.getRawPostData();
            if (postData != null && postData.length > 0) {
                params = GraphQLRequestParamUtils.toGraphQLRequestParams(request.getRawPostData(), sampler.getContentEncoding());
            }
        } catch (Exception e) {
            log.debug("Ignoring request, '{}' as it's not a valid GraphQL post data.", request);
        }
    } else if (HTTPConstants.GET.equals(method)) {
        try {
            params = GraphQLRequestParamUtils.toGraphQLRequestParams(sampler.getArguments(), sampler.getContentEncoding());
        } catch (Exception e) {
            log.debug("Ignoring request, '{}' as it does not valid GraphQL arguments.", request);
        }
    }
    if (params != null) {
        sampler.setProperty(TestElement.GUI_CLASS, GraphQLHTTPSamplerGui.class.getName());
        sampler.setProperty(GraphQLUrlConfigGui.OPERATION_NAME, params.getOperationName());
        sampler.setProperty(GraphQLUrlConfigGui.QUERY, params.getQuery());
        sampler.setProperty(GraphQLUrlConfigGui.VARIABLES, params.getVariables());
    }
}
Also used : GraphQLRequestParams(org.apache.jmeter.protocol.http.config.GraphQLRequestParams) GraphQLHTTPSamplerGui(org.apache.jmeter.protocol.http.control.gui.GraphQLHTTPSamplerGui) Header(org.apache.jmeter.protocol.http.control.Header) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 2 with GraphQLRequestParams

use of org.apache.jmeter.protocol.http.config.GraphQLRequestParams in project jmeter by apache.

the class TestGraphQLRequestParamUtils method testToGraphQLRequestParamsWithHttpArguments.

@Test
void testToGraphQLRequestParamsWithHttpArguments() throws Exception {
    Arguments args = new Arguments();
    args.addArgument(new HTTPArgument("query", "query { droid { id }}", "=", false));
    GraphQLRequestParams params = GraphQLRequestParamUtils.toGraphQLRequestParams(args, null);
    assertNull(params.getOperationName());
    assertEquals("query { droid { id }}", params.getQuery());
    assertNull(params.getVariables());
    args = new Arguments();
    args.addArgument(new HTTPArgument("operationName", "op1", "=", false));
    args.addArgument(new HTTPArgument("query", "query { droid { id }}", "=", false));
    args.addArgument(new HTTPArgument("variables", "{\"id\":123}", "=", false));
    params = GraphQLRequestParamUtils.toGraphQLRequestParams(args, null);
    assertEquals("op1", params.getOperationName());
    assertEquals("query { droid { id }}", params.getQuery());
    assertEquals("{\"id\":123}", params.getVariables());
    args = new Arguments();
    args.addArgument(new HTTPArgument("query", "query+%7B+droid+%7B+id+%7D%7D", "=", true));
    params = GraphQLRequestParamUtils.toGraphQLRequestParams(args, null);
    assertNull(params.getOperationName());
    assertEquals("query { droid { id }}", params.getQuery());
    assertNull(params.getVariables());
    args = new Arguments();
    args.addArgument(new HTTPArgument("query", "query%20%7B%20droid%20%7B%20id%20%7D%7D", "=", true));
    params = GraphQLRequestParamUtils.toGraphQLRequestParams(args, null);
    assertNull(params.getOperationName());
    assertEquals("query { droid { id }}", params.getQuery());
    assertNull(params.getVariables());
}
Also used : GraphQLRequestParams(org.apache.jmeter.protocol.http.config.GraphQLRequestParams) Arguments(org.apache.jmeter.config.Arguments) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with GraphQLRequestParams

use of org.apache.jmeter.protocol.http.config.GraphQLRequestParams in project jmeter by apache.

the class TestGraphQLRequestParamUtils method postBodyFieldNameAndJsonNodes.

static Stream<org.junit.jupiter.params.provider.Arguments> postBodyFieldNameAndJsonNodes() throws Exception {
    final JsonNode expectedPostBodyJson = objectMapper.readTree(EXPECTED_POST_BODY);
    final JsonNode actualPostBodyJson = objectMapper.readTree(GraphQLRequestParamUtils.toPostBodyString(new GraphQLRequestParams(OPERATION_NAME, QUERY, VARIABLES)));
    return Stream.of(arguments(GraphQLRequestParamUtils.OPERATION_NAME_FIELD, expectedPostBodyJson, actualPostBodyJson), arguments(GraphQLRequestParamUtils.VARIABLES_FIELD, expectedPostBodyJson, actualPostBodyJson), arguments(GraphQLRequestParamUtils.QUERY_FIELD, expectedPostBodyJson, actualPostBodyJson));
}
Also used : GraphQLRequestParams(org.apache.jmeter.protocol.http.config.GraphQLRequestParams) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with GraphQLRequestParams

use of org.apache.jmeter.protocol.http.config.GraphQLRequestParams in project jmeter by apache.

the class TestGraphQLRequestParamUtils method testToGraphQLRequestParamsWithPostData.

@Test
void testToGraphQLRequestParamsWithPostData() throws Exception {
    GraphQLRequestParams params = GraphQLRequestParamUtils.toGraphQLRequestParams(EXPECTED_POST_BODY.getBytes(StandardCharsets.UTF_8), null);
    assertNull(params.getOperationName());
    assertEquals(QUERY.trim(), params.getQuery());
    assertEquals(EXPECTED_VARIABLES_GET_PARAM_VALUE, params.getVariables());
    params = GraphQLRequestParamUtils.toGraphQLRequestParams("{\"operationName\":\"op1\",\"variables\":{\"id\":123},\"query\":\"query { droid { id }}\"}".getBytes(StandardCharsets.UTF_8), null);
    assertEquals("op1", params.getOperationName());
    assertEquals("query { droid { id }}", params.getQuery());
    assertEquals("{\"id\":123}", params.getVariables());
}
Also used : GraphQLRequestParams(org.apache.jmeter.protocol.http.config.GraphQLRequestParams) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with GraphQLRequestParams

use of org.apache.jmeter.protocol.http.config.GraphQLRequestParams in project jmeter by apache.

the class GraphQLUrlConfigGui method modifyTestElement.

@Override
public void modifyTestElement(TestElement element) {
    super.modifyTestElement(element);
    final String method = element.getPropertyAsString(HTTPSamplerBase.METHOD);
    final GraphQLRequestParams params = new GraphQLRequestParams(operationNameText.getText(), queryContent.getText(), variablesContent.getText());
    element.setProperty(OPERATION_NAME, params.getOperationName());
    element.setProperty(QUERY, params.getQuery());
    element.setProperty(VARIABLES, params.getVariables());
    element.setProperty(HTTPSamplerBase.POST_BODY_RAW, !HTTPConstants.GET.equals(method));
    final Arguments args;
    if (HTTPConstants.GET.equals(method)) {
        args = createHTTPArgumentsTestElement();
        if (StringUtils.isNotBlank(params.getOperationName())) {
            args.addArgument(createHTTPArgument("operationName", params.getOperationName().trim(), true));
        }
        args.addArgument(createHTTPArgument("query", GraphQLRequestParamUtils.queryToGetParamValue(params.getQuery()), true));
        if (StringUtils.isNotBlank(params.getVariables())) {
            final String variablesParamValue = GraphQLRequestParamUtils.variablesToGetParamValue(params.getVariables());
            if (variablesParamValue != null) {
                args.addArgument(createHTTPArgument("variables", variablesParamValue, true));
            }
        }
    } else {
        args = new Arguments();
        args.addArgument(createHTTPArgument("", GraphQLRequestParamUtils.toPostBodyString(params), false));
    }
    element.setProperty(new TestElementProperty(HTTPSamplerBase.ARGUMENTS, args));
}
Also used : GraphQLRequestParams(org.apache.jmeter.protocol.http.config.GraphQLRequestParams) TestElementProperty(org.apache.jmeter.testelement.property.TestElementProperty) Arguments(org.apache.jmeter.config.Arguments)

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