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());
}
}
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());
}
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));
}
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());
}
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));
}
Aggregations