Am 23.12.20 um 23:38 schrieb Nikola Aleksic:
> Thank you again :)
>
> I used Groovy way and to detect MIME type automatically
> URLConnection.guessContentTypeFromName() method is used. I declared two
> arrays with file paths and parameters so we can put as many files as we
> want to upload:
>
> import org.apache.jmeter.protocol.http.util.HTTPFileArg
>
> def filePath = ["uploadFiles/desktop.jpg", "uploadFiles/desktop.jpg"]
> def fileParameters = ["FrontsideImage", "BacksideImage"]
> def files = []
>
> for (i = 0; i < filePath.size(); i++) {
> files += new HTTPFileArg (filePath[i], fileParameters[i],
> URLConnection.guessContentTypeFromName(new File(filePath[i]).getName()))
> }
> sampler.HTTPFiles = files
This can be written with a map and the collect method as follow:
import org.apache.jmeter.protocol.http.util.HTTPFileArg
def files = [
"FrontsideImage": "uploadFiles/desktop.jpg",
"BacksideImage": "uploadFiles/desktop.jpg"
].collect {
new HTTPFileArg(
it.key,
it.value,
URLConnection.guessContentTypeFromName(new File(filePath[i]).getName()))
}
sampler.HTTPFiles = files
The method collect iterates over all entries in the map. Inside the
curly braces (a closure), you can access the element by the variable 'it'.
The result of the last statement in the closure is collected into a
list, which is stored in files and then silently converted to an array
and given to the sampler.
>
> I was curious to see how URLConnection.guessContentTypeFromName() method
> actually works. I renamed .jpg extension to .png and image/png is generated
> so I guess that it scans extension rather than file hex signature.
If you want to have it guess from the content as well, you would have to
use URLConnection#guessContentTypeFromStream (and give it a
inputstream), or you could use Tika (it is available on the classpath,
as it is used in the curl-to-JMeter parser). There you could use
def tika = new Tika()
def contentType = tika.detect(new File(filename))
Felix
>
> Regards,
> Nikola
>
> On Wed, 23 Dec 2020 at 19:16, Felix Schumacher <
> [email protected]> wrote:
>
>> Am 23.12.20 um 18:36 schrieb Nikola Aleksic:
>>> Thanks, for the fast response.
>>>
>>> It worked great! I like how the code looks, very simple.
>>> May I ask how to do the opposite, to generate file(s) to upload in the
>> same
>>> simple way using Groovy?
>> To generate file parameters, you have to fill the array. Again it is
>> probably easier to do so with a list, so a example would be:
>>
>> import org.apache.jmeter.protocol.http.util.HTTPFileArg
>>
>> def files = new ArrayList();
>> files.add(new
>> HTTPFileArg('../xdocs/images/screenshots/simpledatawriter.png', 'pic',
>> 'image/png'));
>>
>> sampler.HTTPFiles = files.toArray()
>>
>> Again, I think the import stuff is the working with the list and here
>> the construction of a HTTPFileArg with the three parameters which point
>> to the file, the name of the http parameter and the content type (which
>> you have to provide yourself).
>>
>> If you want to do it in a more Groovy way, you can use the list literal
>> [] instead of new ArrayList() and the +-operator instead of add and
>> count on Groovy to do the right thing (convert a list to an array).
>> Which would give you
>>
>> import org.apache.jmeter.protocol.http.util.HTTPFileArg
>>
>> def files = []
>> files += new
>> HTTPFileArg('../xdocs/images/screenshots/simpledatawriter.png', 'pic',
>> 'image/png')
>>
>> sampler.HTTPFiles = files
>>
>> Felix
>>
>>> On Wed, 23 Dec 2020 at 16:28, Felix Schumacher <
>>> [email protected]> wrote:
>>>
>>>> Am 23.12.20 um 16:05 schrieb Nikola Aleksic:
>>>>> Hello,
>>>>>
>>>>> I was trying to list all parameters in HTTP Sampler including ones in
>>>>> File upload tab using the following code and remove the desired one in
>>>>> File upload tab, but File upload tab parameters FrontsideImage and
>>>>> BacksideImage are not listed:
>>>>>
>>>>> import org.apache.jmeter.config.Arguments;
>>>>>
>>>>> Arguments args = sampler.getArguments();
>>>>> Iterator it = args.iterator();
>>>>>
>>>>> while (it.hasNext()) {
>>>>> def argument = it.next();
>>>>> if (argument.getStringValue().contains('somevalue')) {
>>>>> args.removeArgument(argument.getName());
>>>>> }
>>>>> log.warn(argument.getName())
>>>>> }
>>>>>
>>>>> Code source: https://stackoverflow.com/a/40918642
>>>>>
>>>>> Can someone provide a corresponding method to list Files upload
>>>>> parameters as well and remove desired one as per its value? Check jmx
>>>>> example in the attachment. Set log level to WARN, Run test and check
>>>>> the logs.
>>>> The files are hidden away under sampler.getHTTPFiles() it returns an
>>>> array, so probably want to change it to a modern collection first. Then
>>>> remove the empty elements, or whatever you want to do. Last you convert
>>>> it back to an array and store it with sampler.setHTTPFiles().
>>>>
>>>>
>>>> In a JSR-223 PreProcessor with Groovy that would be
>>>>
>>>> def files = new ArrayList(Arrays.asList(sampler.HTTPFiles))
>>>> log.info("files: " + files)
>>>>
>>>> files.removeIf(f -> f.path == "something" && f.paramName == "nothing" ||
>>>> f.mimeType == "text/json")
>>>>
>>>> log.info("files: " + files)
>>>>
>>>> sampler.HTTPFiles = files.toArray()
>>>>
>>>>
>>>> The log.info(...) is added to show, what has been done by the
>>>> pre-processor.
>>>>
>>>> The stuff after 'f -> ' is the interesting part, as it is the predicate,
>>>> which decides, which files should be removed from the collection.
>>>>
>>>> Felix
>>>>
>>>>> Regards,
>>>>> Nikola
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: [email protected]
>>>>> For additional commands, e-mail: [email protected]
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]