1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
32
33
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 }