You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by fs...@apache.org on 2021/02/11 19:02:15 UTC
[jmeter] branch master updated: Detect mime-type for files
automatically when adding files to HTTP Sampler
This is an automated email from the ASF dual-hosted git repository.
fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git
The following commit(s) were added to refs/heads/master by this push:
new 9326c3f Detect mime-type for files automatically when adding files to HTTP Sampler
9326c3f is described below
commit 9326c3febe435307d36e904412593c2c2badf987
Author: Felix Schumacher <fe...@internetallee.de>
AuthorDate: Wed Dec 30 12:34:08 2020 +0100
Detect mime-type for files automatically when adding files to HTTP Sampler
Bugzilla Id: 65027
---
.../jmeter/protocol/http/util/HTTPFileArg.java | 40 +++++++++++++++++++++-
.../jmeter/protocol/http/util/TestHTTPFileArg.java | 2 +-
.../protocol/http/util/TestHTTPFileArgs.java | 10 +++---
xdocs/changes.xml | 1 +
xdocs/usermanual/component_reference.xml | 10 +++---
5 files changed, 52 insertions(+), 11 deletions(-)
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPFileArg.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPFileArg.java
index 220e718..a33fcb1 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPFileArg.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPFileArg.java
@@ -17,11 +17,18 @@
package org.apache.jmeter.protocol.http.util;
+import java.io.File;
+import java.io.IOException;
import java.io.Serializable;
+import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.StringProperty;
+import org.apache.tika.Tika;
+import org.apache.tika.config.TikaConfig;
+import org.apache.tika.exception.TikaException;
+import org.xml.sax.SAXException;
/**
* Class representing a file parameter for http upload.
@@ -46,6 +53,8 @@ public class HTTPFileArg extends AbstractTestElement implements Serializable {
/** temporary storage area for the body header. */
private String header;
+ private static Tika tika = createTika();
+
/**
* Constructor for an empty HTTPFileArg object
*/
@@ -82,7 +91,35 @@ public class HTTPFileArg extends AbstractTestElement implements Serializable {
}
setPath(path);
setParamName(paramname);
- setMimeType(mimetype);
+ setMimeType(detectMimeType(path, mimetype));
+ }
+
+ private static Tika createTika() {
+ try {
+ return new Tika(new TikaConfig(HTTPFileArg.class.getClassLoader()
+ .getResourceAsStream("org/apache/jmeter/protocol/http/gui/action/tika-config.xml")));
+ } catch (TikaException | IOException | SAXException e) {
+ return new Tika();
+ }
+ }
+
+ private String detectMimeType(String path, String mimetype) {
+ if (StringUtils.isNotBlank(mimetype)) {
+ return mimetype;
+ }
+ mimetype = StringUtils.defaultString(mimetype, "");
+ if (StringUtils.isBlank(path)) {
+ return mimetype;
+ }
+ File file = new File(path);
+ if (file.canRead()) {
+ try {
+ return tika.detect(file);
+ } catch (IOException e) {
+ // do nothing, we will detect it later by name
+ }
+ }
+ return tika.detect(path);
}
/**
@@ -171,6 +208,7 @@ public class HTTPFileArg extends AbstractTestElement implements Serializable {
* the new path
*/
public void setPath(String newPath) {
+ setMimeType(detectMimeType(newPath, getMimeType()));
setProperty(new StringProperty(FILEPATH, newPath));
}
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestHTTPFileArg.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestHTTPFileArg.java
index fbfb356..8499266 100644
--- a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestHTTPFileArg.java
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestHTTPFileArg.java
@@ -32,7 +32,7 @@ public class TestHTTPFileArg {
file = new HTTPFileArg("path");
assertEquals("single parameter failure", "path", file.getPath());
assertEquals("single parameter failure", "", file.getParamName());
- assertEquals("single parameter failure", "", file.getMimeType());
+ assertEquals("single parameter failure", "application/octet-stream", file.getMimeType());
file = new HTTPFileArg("path", "param", "mimetype");
assertEquals("three parameter failure", "path", file.getPath());
assertEquals("three parameter failure", "param", file.getParamName());
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestHTTPFileArgs.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestHTTPFileArgs.java
index 8b11327..9d6e30c 100644
--- a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestHTTPFileArgs.java
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestHTTPFileArgs.java
@@ -114,11 +114,11 @@ public class TestHTTPFileArgs {
public void testToString() throws Exception {
HTTPFileArgs files = new HTTPFileArgs();
files.addHTTPFileArg("file1");
- files.addHTTPFileArg("file2");
- files.addHTTPFileArg("file3");
- assertEquals("path:'file1'|param:''|mimetype:''\n"
- +"path:'file2'|param:''|mimetype:''\n"
- +"path:'file3'|param:''|mimetype:''",
+ files.addHTTPFileArg("file2.jpg");
+ files.addHTTPFileArg("file3.jar");
+ assertEquals("path:'file1'|param:''|mimetype:'application/octet-stream'\n"
+ +"path:'file2.jpg'|param:''|mimetype:'image/jpeg'\n"
+ +"path:'file3.jar'|param:''|mimetype:'application/java-archive'",
files.toString());
}
}
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 140c0f4..6f893ca 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -78,6 +78,7 @@ Summary
<h3>HTTP Samplers and Test Script Recorder</h3>
<ul>
+ <li><bug>65027</bug>Detect mime-type for files automatically when adding files to HTTP Sampler</li>
</ul>
<h3>Other samplers</h3>
diff --git a/xdocs/usermanual/component_reference.xml b/xdocs/usermanual/component_reference.xml
index 860d32e..3183c55 100644
--- a/xdocs/usermanual/component_reference.xml
+++ b/xdocs/usermanual/component_reference.xml
@@ -323,10 +323,12 @@ so unsafe characters may need to be encoded to avoid errors such as <code>URISyn
does not send a file, if filled in, JMeter automatically sends the request as
a multipart form request.
<p>
- If it is a <code>POST</code> or <code>PUT</code> or <code>PATCH</code> request and there is a single file whose 'Parameter name' attribute (below) is omitted,
- then the file is sent as the entire body
- of the request, i.e. no wrappers are added. This allows arbitrary bodies to be sent. This functionality is present for <code>POST</code> requests,
- and also for <code>PUT</code> requests.
+ When <code>MIME Type</code> is empty, JMeter will try to guess the MIME type of the given file.
+ </p>
+ <p>
+ If it is a <code>POST</code> or <code>PUT</code> or <code>PATCH</code> request and there is a single file whose '<code>Parameter name</code>'
+ attribute (below) is omitted, then the file is sent as the entire body of the request, i.e. no wrappers are added. This allows arbitrary
+ bodies to be sent. This functionality is present for <code>POST</code> requests, and also for <code>PUT</code> requests.
<b>See below for some further information on parameter handling.</b>
</p>
</property>