apache / jmeter

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services
https://jmeter.apache.org/
Apache License 2.0
8.39k stars 2.1k forks source link

Error when using __FileToString to set GraphQL request body #5679

Open asfimport opened 2 years ago

asfimport commented 2 years ago

Mateus Dadalto (Bug 66122): I'm trying to set my body based on what is in a file and it fails saying the JSON is invalid. But when I set the request body manually the same content the request works. In the results tree view I can see that the only difference is that the manual request is "encoded", it has /n instead of visual newlines.

Steps to reproduce:

-1: Download and open the .jmx attached to this bug -2: Create a file with the content being the body from the first request ("With Body Set Manually") -3: change the path on the second request ("With Body From File") to your file path -4: Run and see in the View Results Tree that the first one works and the second one fails

Created attachment Jmeter%20GraphQl%20Bug.jmx: A sample project to reproduce the bug

Jmeter%20GraphQl%20Bug.jmx ````xml false true false continue false 1 1 1 false true Content-Type application/json false {"operationName":null,"query":"query {\n allFilms {\n films {\n title\n director\n releaseDate\n speciesConnection {\n species {\n name\n classification\n homeworld {\n name\n }\n }\n }\n }\n }\n}"} = true swapi-graphql.netlify.app https .netlify/functions/index POST false false true false query { allFilms { films { title director releaseDate speciesConnection { species { name classification homeworld { name } } } } } } true false {"operationName":null,"query":"${__FileToString(your-file-path,,)}"} = true swapi-graphql.netlify.app https .netlify/functions/index POST false false true false ${__FileToString(your-file-path,,)} true false saveConfig true true true true true true true false true true false false false true false false false true 0 true true true true true true ````

Severity: normal OS: Linux

asfimport commented 2 years ago

@FSchumacher (migrated from Bugzilla): I can reproduce the problem with JMeter 5.5 on Linux. Thanks for the detailed bug description.

asfimport commented 2 years ago

@FSchumacher (migrated from Bugzilla): Well, after trying out to replace the newlines (\n) with the string values "\n", it became clear to me, that you already found the solution yourself :)

When you are using giving the GraphQL Sampler the Query manually, JMeter knows, that this is something, that has to be encoded for usage in JSON. If you store the content in a file and include it, JMeter does not know, if it should do the encoding, or not and does not do the encoding itself.

The POST data without the escaped newlines will break the JSON stuff.

So for the moment, you will have to encode it yourself (might be done with a Pre-Processor). I am not sure, whether we can safely do the escaping automatically. Will have to think about it (not sorry, if someone else provides a good solution :))

asfimport commented 2 years ago

Mateus Dadalto (migrated from Bugzilla): I see what you mean... This will happen with anything that either comes from a function or it's stored in a variable. I still hope that this behavior gets fixed in future versions (IMHO it is not a user-friendly behavior).

I just want to add that you'll have to deal with everything that might break a JSON (graphQL syntax has quotes in places that can cause this). E.G.:

mutation { createCustomerV2( input: { firstname: "Bob" lastname: "Loblaw" email: "bobloblaw2@example.com" password: "b0bl0bl@w" is_subscribed: true } ) { customer { firstname lastname email is_subscribed } } }


For now the solution I came up with was to add a JSR223 PreProcessor and just treat these cases myself. Here's the PreProcessor code in case someone needs it.

def content = new File(vars.get('fileName')).text; vars.put('queryContent', content.replaceAll(/(\r\n|\r|\n)/, /\n/).replaceAll(/"/, /\"/))