[HtmlUnit] SVN: [13750] trunk/htmlunit/src

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[HtmlUnit] SVN: [13750] trunk/htmlunit/src

rbri
Revision: 13750
          http://sourceforge.net/p/htmlunit/code/13750
Author:   rbri
Date:     2017-03-15 20:12:01 +0000 (Wed, 15 Mar 2017)
Log Message:
-----------
correct file type detection for files with css suffix
Issue 1858

Modified Paths:
--------------
    trunk/htmlunit/src/changes/changes.xml
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java

Modified: trunk/htmlunit/src/changes/changes.xml
===================================================================
--- trunk/htmlunit/src/changes/changes.xml 2017-03-15 20:11:03 UTC (rev 13749)
+++ trunk/htmlunit/src/changes/changes.xml 2017-03-15 20:12:01 UTC (rev 13750)
@@ -8,6 +8,9 @@
 
     <body>
         <release version="2.26" date="???" description="Bugfixes">
+            <action type="fix" issue="#1858" due-to="Joerg Werner">
+                Correct file type detection for files with css suffix.
+            </action>
             <action type="add" dev="rbri">
                 JavaScript: simple impl for MediaSource.isTypeSupported added (returns always false).
             </action>

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java 2017-03-15 20:11:03 UTC (rev 13749)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java 2017-03-15 20:12:01 UTC (rev 13750)
@@ -132,6 +132,7 @@
  * @author Nicolas Belisle
  * @author Ronald Brill
  * @author Frank Danek
+ * @author Joerg Werner
  */
 public class WebClient implements Serializable, AutoCloseable {
 
@@ -1205,11 +1206,20 @@
      * @return "application/octet-stream" if nothing could be guessed
      */
     public String guessContentType(final File file) {
-        String contentType = URLConnection.guessContentTypeFromName(file.getName());
         if (file.getName().endsWith(".xhtml")) {
-            // Java's mime type map doesn't know about XHTML files (at least in Sun JDK5).
-            contentType = "application/xhtml+xml";
+            // Java's mime type map returns application/xml in JDK8.
+            return "application/xhtml+xml";
         }
+
+        // Java's mime type map does not know these in JDK8.
+        if (file.getName().endsWith(".js")) {
+            return "text/javascript";
+        }
+        if (file.getName().toLowerCase().endsWith(".css")) {
+            return "text/css";
+        }
+
+        String contentType = URLConnection.guessContentTypeFromName(file.getName());
         if (contentType == null) {
             try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
                 contentType = URLConnection.guessContentTypeFromStream(inputStream);
@@ -1219,12 +1229,7 @@
             }
         }
         if (contentType == null) {
-            if (file.getName().endsWith(".js")) {
-                contentType = "text/javascript";
-            }
-            else {
-                contentType = "application/octet-stream";
-            }
+            contentType = "application/octet-stream";
         }
         return contentType;
     }

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java 2017-03-15 20:11:03 UTC (rev 13749)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/WebClientTest.java 2017-03-15 20:12:01 UTC (rev 13750)
@@ -76,6 +76,7 @@
  * @author Sudhan Moghe
  * @author Ronald Brill
  * @author Carsten Steul
+ * @author Joerg Werner
  */
 @RunWith(BrowserRunner.class)
 public class WebClientTest extends SimpleWebTestCase {
@@ -1428,6 +1429,7 @@
         assertEquals("empty.jpg", "image/jpeg", c.guessContentType(getTestFile("empty.jpg")));
         assertEquals("empty.gif", "image/gif", c.guessContentType(getTestFile("empty.gif")));
         assertEquals("empty.js", "text/javascript", c.guessContentType(getTestFile("empty.js")));
+        assertEquals("empty.css", "text/css", c.guessContentType(getTestFile("empty.css")));
 
         // test real files with bad file suffix
         assertEquals("tiny-png.img", "image/png", c.guessContentType(getTestFile("tiny-png.img")));


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop