You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2017/03/10 22:18:55 UTC

svn commit: r1786460 - in /jmeter/trunk: src/protocol/http/org/apache/jmeter/protocol/http/parser/ test/src/org/apache/jmeter/protocol/http/parser/

Author: pmouawad
Date: Fri Mar 10 22:18:55 2017
New Revision: 1786460

URL: http://svn.apache.org/viewvc?rev=1786460&view=rev
Log:
Bug 60842 - jmeter chokes on newline
Optimize by using static Pattern
Factor our code in base class
Use code in JSoup based implementation
Add Junit tests for it
Bugzilla Id: 60842

Added:
    jmeter/trunk/test/src/org/apache/jmeter/protocol/http/parser/TestJSoupBasedHtmlParser.java   (with props)
Modified:
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/JsoupBasedHtmlParser.java
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/LagartoBasedHtmlParser.java

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java?rev=1786460&r1=1786459&r2=1786460&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java (original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java Fri Mar 10 22:18:55 2017
@@ -71,6 +71,8 @@ public abstract class HTMLParser extends
     public static final String DEFAULT_PARSER =
         "org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser"; // $NON-NLS-1$
 
+    private static final Pattern NORMALIZE_URL_PATTERN = Pattern.compile("[\n\r\b\f]+"); //$NON-NLS-1$
+
     /**
      * Protected constructor to prevent instantiation except from within
      * subclasses.
@@ -217,4 +219,18 @@ public abstract class HTMLParser extends
             return null;
         }
     }
+
+    /**
+     * Normalizes URL as browsers do
+     * @param url {@link CharSequence}
+     */
+    protected static String normalizeUrlValue(CharSequence url) {
+        if (!StringUtils.isEmpty(url)) {
+            String trimmed = NORMALIZE_URL_PATTERN.matcher(url.toString().trim()).replaceAll("");
+            if (!trimmed.isEmpty()) {
+                return trimmed;
+            }
+        }
+        return null;
+    }
 }

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/JsoupBasedHtmlParser.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/JsoupBasedHtmlParser.java?rev=1786460&r1=1786459&r2=1786460&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/JsoupBasedHtmlParser.java (original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/JsoupBasedHtmlParser.java Fri Mar 10 22:18:55 2017
@@ -64,8 +64,9 @@ public class JsoupBasedHtmlParser extend
 
         private void extractAttribute(Element tag, String attributeName) {
             String url = tag.attr(attributeName);
-            if (!StringUtils.isEmpty(url)) {
-                urls.addURL(url, baseUrl.url);
+            String normalizedUrl = normalizeUrlValue(url);
+            if(normalizedUrl != null) {
+                urls.addURL(normalizedUrl, baseUrl.url);
             }
         }
 

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/LagartoBasedHtmlParser.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/LagartoBasedHtmlParser.java?rev=1786460&r1=1786459&r2=1786460&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/LagartoBasedHtmlParser.java (original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/parser/LagartoBasedHtmlParser.java Fri Mar 10 22:18:55 2017
@@ -79,13 +79,12 @@ public class LagartoBasedHtmlParser exte
 
         private void extractAttribute(Tag tag, String attributeName) {
             CharSequence url = tag.getAttributeValue(attributeName);
-            if (!StringUtils.isEmpty(url)) {
-                String trimmed = url.toString().trim().replaceAll("[\n\r\b\f]+", "");
-                if (!trimmed.isEmpty()) {
-                    urls.addURL(trimmed, baseUrl.url);
-                }
+            String normalizedUrl = normalizeUrlValue(url);
+            if(normalizedUrl != null) {
+                urls.addURL(normalizedUrl, baseUrl.url);
             }
         }
+        
         /*
          * (non-Javadoc)
          * 

Added: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/parser/TestJSoupBasedHtmlParser.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/parser/TestJSoupBasedHtmlParser.java?rev=1786460&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/protocol/http/parser/TestJSoupBasedHtmlParser.java (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/protocol/http/parser/TestJSoupBasedHtmlParser.java Fri Mar 10 22:18:55 2017
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.jmeter.protocol.http.parser;
+
+import static org.junit.Assert.assertThat;
+
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class TestJSoupBasedHtmlParser {
+    
+    private List<String> links;
+    private String html;
+
+    public TestJSoupBasedHtmlParser(String html, String links) {
+        this.html = html;
+        if (links.isEmpty()) {
+            this.links = Collections.emptyList();
+        } else {
+            this.links = Arrays.asList(links.split(","));
+        }
+    }
+
+    @Parameters
+    public static Object[][] params() {
+        return new Object[][] {
+                new Object[] {"<body background='abc.png'/>", "http://example.org/abc.png"},
+                new Object[] {"<link href='abc.css' rel='stylesheet'/>", "http://example.org/abc.css"},
+                new Object[] {"<img src='abc.png'/>", "http://example.org/abc.png"},
+                new Object[] {"<base href='http://another.org'/><img src='one.png'/>", "http://another.org/one.png"},
+                new Object[] {"<applet code='abc.jar'/>", "http://example.org/abc.jar"},
+                new Object[] {"<object codebase='abc.jar' data='something'/>", "http://example.org/abc.jar,http://example.org/something"},
+                new Object[] {"<object data='something'/>", "http://example.org/something"},
+                new Object[] {"<object codebase='abc.jar'/>", "http://example.org/abc.jar"},
+                new Object[] {"<input type='image' src='foo'/>", "http://example.org/foo"},
+                new Object[] {"<input type='text' src='foo'/>", ""},
+                // JSOUP Fails to parse this one
+//                new Object[] {"<frame src='foo'/>", "http://example.org/foo"},
+                new Object[] {"<iframe src='foo'/>", "http://example.org/foo"},
+                new Object[] {"<embed src='foo'/>", "http://example.org/foo"},
+                new Object[] {"<bgsound src='foo'/>", "http://example.org/foo"},
+                new Object[] {"<anytag background='foo'/>", "http://example.org/foo"},
+                new Object[] {"<anytag style='foo: url(\"bar\")'/>", "http://example.org/bar"},
+                new Object[] {"<anytag style=\"foo: url('bar')'\"/>", "http://example.org/bar"},
+                // new Object[] {"<anytag style=\"foo: url(bar)'\"/>", "http://example.org/bar"},
+                // new Object[] {"<anytag style=\"foo: url(bar)'; other: url(something);\"/>", "http://example.org/bar,http://example.org/something"},
+                new Object[] {"<link href='  abc\n.css  ' rel='stylesheet'/>", "http://example.org/abc.css"},
+                new Object[] {"<link href='  with spaces\n.css  ' rel='stylesheet'/>", "http://example.org/with spaces.css"},
+                new Object[] {"<embed src=''/>", ""},
+                new Object[] {"<embed src='  '/>", ""},
+        };
+    }
+    
+    @Test
+    public void testJsoupGetEmbeddedResourceURLsStringByteArrayURLURLCollectionString() throws Exception {
+        HTMLParser parser = new JsoupBasedHtmlParser();
+        final ArrayList<URLString> c = new ArrayList<>();
+        parser.getEmbeddedResourceURLs("Mozilla",
+                html.getBytes(StandardCharsets.UTF_8),
+                new URL("http://example.org"), new URLCollection(c),
+                StandardCharsets.UTF_8.name());
+        List<String> urlNames = c.stream().map(u -> u.toString()).collect(Collectors.toList());
+        assertThat(urlNames, CoreMatchers.is(CoreMatchers.equalTo(links)));
+    }
+
+}

Propchange: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/parser/TestJSoupBasedHtmlParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/parser/TestJSoupBasedHtmlParser.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain