View Javadoc

1   // Copyright (C) 2006 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.plugin.http.tcpproxyfilter;
23  
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import junit.framework.TestCase;
28  
29  
30  /**
31   * Unit tests for {@link RegularExpressionsImplementation}.
32   *
33   * @author Philip Aston
34   */
35  public class TestRegularExpressionsImplementation extends TestCase {
36  
37    public void testHyperlinkURIPattern() throws Exception {
38      final RegularExpressions regularExpressions =
39        new RegularExpressionsImplementation();
40  
41      final Pattern pattern = regularExpressions.getHyperlinkURIPattern();
42  
43      final String text =
44        "  blah='./foo' <a href=\"http://bah\">http://blah</a>\n" +
45        "<a href='#fragment'/>";
46  
47      final Matcher matcher = pattern.matcher(text);
48  
49      assertTrue(matcher.find());
50      assertEquals("http://bah", matcher.group(1));
51  
52      assertTrue(matcher.find());
53      assertEquals("#fragment", matcher.group(1));
54  
55      assertFalse(matcher.find());
56    }
57  
58    public void testHiddenParameterPattern() throws Exception {
59      final RegularExpressions regularExpressions =
60        new RegularExpressionsImplementation();
61  
62      final Pattern pattern = regularExpressions.getHiddenInputPattern();
63  
64      final String[] goodMatches = {
65        " <input type='hidden' name=\"name\" value='myvalue'>",
66        " <input type='HIDDEN' />",
67        " <  Input Name='name' Type=\"hidden\" />",
68        "<input\ntype='hidden'\nname=\"name\"\nvalue='myvalue'>",
69      };
70  
71      for (int i = 0; i < goodMatches.length; ++i) {
72        final Matcher matcher = pattern.matcher(goodMatches[i]);
73        assertTrue("Matches '" + goodMatches[i] + "'", matcher.find());
74        assertEquals(goodMatches[i].trim(), matcher.group());
75      }
76  
77      final String[] badMatches = {
78          " <input type='hidden' name=\"name\" value='myvalue'",
79          "<output type='hidden' name=\"name\" value='myvalue'>",
80          "<input type='somethingelse' name=\"name\" value='myvalue'/>",
81          "<input>",
82        };
83  
84        for (int i = 0; i < badMatches.length; ++i) {
85          final Matcher matcher = pattern.matcher(badMatches[i]);
86          assertFalse("Fails to match '" + badMatches[i] + "'", matcher.find());
87        }
88    }
89  }