001: // ========================================================================
002: // Copyright 1999-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.jetty.servlet;
016:
017: import junit.framework.TestCase;
018:
019: import org.mortbay.jetty.HttpParserTest;
020:
021: /* ------------------------------------------------------------ */
022: /**
023: * Top level test harness.
024: *
025: * @author Greg Wilkins (gregw)
026: */
027: public class PathMapTest extends TestCase {
028: /**
029: * Constructor for HttpParserTest.
030: *
031: * @param arg0
032: */
033: public PathMapTest(String arg0) {
034: super (arg0);
035: }
036:
037: public static void main(String[] args) {
038: junit.textui.TestRunner.run(HttpParserTest.class);
039: }
040:
041: /**
042: * @see TestCase#setUp()
043: */
044: protected void setUp() throws Exception {
045: super .setUp();
046: }
047:
048: /**
049: * @see TestCase#tearDown()
050: */
051: protected void tearDown() throws Exception {
052: super .tearDown();
053: }
054:
055: /* --------------------------------------------------------------- */
056: public void testPathMap() throws Exception {
057: PathMap p = new PathMap();
058:
059: p.put("/abs/path", "1");
060: p.put("/abs/path/longer", "2");
061: p.put("/animal/bird/*", "3");
062: p.put("/animal/fish/*", "4");
063: p.put("/animal/*", "5");
064: p.put("*.tar.gz", "6");
065: p.put("*.gz", "7");
066: p.put("/", "8");
067: p.put("/XXX:/YYY", "9");
068:
069: String[][] tests = { { "/abs/path", "1" },
070: { "/abs/path/xxx", "8" }, { "/abs/pith", "8" },
071: { "/abs/path/longer", "2" }, { "/abs/path/", "8" },
072: { "/abs/path/xxx", "8" },
073: { "/animal/bird/eagle/bald", "3" },
074: { "/animal/fish/shark/grey", "4" },
075: { "/animal/insect/bug", "5" }, { "/animal", "5" },
076: { "/animal/", "5" }, { "/suffix/path.tar.gz", "6" },
077: { "/suffix/path.gz", "7" }, { "/animal/path.gz", "5" },
078: { "/Other/path", "8" }, };
079:
080: for (int i = 0; i < tests.length; i++) {
081: assertEquals(tests[i][0], tests[i][1], p.getMatch(
082: tests[i][0]).getValue());
083: }
084:
085: assertEquals("Get absolute path", "1", p.get("/abs/path"));
086: assertEquals("Match absolute path", "/abs/path", p.getMatch(
087: "/abs/path").getKey());
088: assertEquals(
089: "all matches",
090: "[/animal/bird/*=3, /animal/*=5, *.tar.gz=6, *.gz=7, /=8]",
091: p.getMatches("/animal/bird/path.tar.gz").toString());
092: assertEquals("Dir matches",
093: "[/animal/fish/*=4, /animal/*=5, /=8]", p.getMatches(
094: "/animal/fish/").toString());
095: assertEquals("Dir matches",
096: "[/animal/fish/*=4, /animal/*=5, /=8]", p.getMatches(
097: "/animal/fish").toString());
098: assertEquals("Dir matches", "[/=8]", p.getMatches("/")
099: .toString());
100: assertEquals("Dir matches", "[/=8]", p.getMatches("")
101: .toString());
102:
103: assertEquals("pathMatch exact", "/Foo/bar", PathMap.pathMatch(
104: "/Foo/bar", "/Foo/bar"));
105: assertEquals("pathMatch prefix", "/Foo", PathMap.pathMatch(
106: "/Foo/*", "/Foo/bar"));
107: assertEquals("pathMatch prefix", "/Foo", PathMap.pathMatch(
108: "/Foo/*", "/Foo/"));
109: assertEquals("pathMatch prefix", "/Foo", PathMap.pathMatch(
110: "/Foo/*", "/Foo"));
111: assertEquals("pathMatch suffix", "/Foo/bar.ext", PathMap
112: .pathMatch("*.ext", "/Foo/bar.ext"));
113: assertEquals("pathMatch default", "/Foo/bar.ext", PathMap
114: .pathMatch("/", "/Foo/bar.ext"));
115:
116: assertEquals("pathInfo exact", null, PathMap.pathInfo(
117: "/Foo/bar", "/Foo/bar"));
118: assertEquals("pathInfo prefix", "/bar", PathMap.pathInfo(
119: "/Foo/*", "/Foo/bar"));
120: assertEquals("pathInfo prefix", "/", PathMap.pathInfo("/Foo/*",
121: "/Foo/"));
122: assertEquals("pathInfo prefix", null, PathMap.pathInfo(
123: "/Foo/*", "/Foo"));
124: assertEquals("pathInfo suffix", null, PathMap.pathInfo("*.ext",
125: "/Foo/bar.ext"));
126: assertEquals("pathInfo default", null, PathMap.pathInfo("/",
127: "/Foo/bar.ext"));
128: assertEquals("multi paths", "9", p.getMatch("/XXX").getValue());
129: assertEquals("multi paths", "9", p.getMatch("/YYY").getValue());
130:
131: p.put("/*", "0");
132:
133: assertEquals("Get absolute path", "1", p.get("/abs/path"));
134: assertEquals("Match absolute path", "/abs/path", p.getMatch(
135: "/abs/path").getKey());
136: assertEquals("Match absolute path", "1", p
137: .getMatch("/abs/path").getValue());
138: assertEquals("Mismatch absolute path", "0", p.getMatch(
139: "/abs/path/xxx").getValue());
140: assertEquals("Mismatch absolute path", "0", p.getMatch(
141: "/abs/pith").getValue());
142: assertEquals("Match longer absolute path", "2", p.getMatch(
143: "/abs/path/longer").getValue());
144: assertEquals("Not exact absolute path", "0", p.getMatch(
145: "/abs/path/").getValue());
146: assertEquals("Not exact absolute path", "0", p.getMatch(
147: "/abs/path/xxx").getValue());
148:
149: assertEquals("Match longest prefix", "3", p.getMatch(
150: "/animal/bird/eagle/bald").getValue());
151: assertEquals("Match longest prefix", "4", p.getMatch(
152: "/animal/fish/shark/grey").getValue());
153: assertEquals("Match longest prefix", "5", p.getMatch(
154: "/animal/insect/bug").getValue());
155: assertEquals("mismatch exact prefix", "5", p
156: .getMatch("/animal").getValue());
157: assertEquals("mismatch exact prefix", "5", p.getMatch(
158: "/animal/").getValue());
159:
160: assertEquals("Match longest suffix", "0", p.getMatch(
161: "/suffix/path.tar.gz").getValue());
162: assertEquals("Match longest suffix", "0", p.getMatch(
163: "/suffix/path.gz").getValue());
164: assertEquals("prefix rather than suffix", "5", p.getMatch(
165: "/animal/path.gz").getValue());
166:
167: assertEquals("default", "0", p.getMatch("/Other/path")
168: .getValue());
169:
170: assertEquals("pathMatch /*", "", PathMap.pathMatch("/*",
171: "/xxx/zzz"));
172: assertEquals("pathInfo /*", "/xxx/zzz", PathMap.pathInfo("/*",
173: "/xxx/zzz"));
174:
175: assertTrue("match /", PathMap.match("/", "/anything"));
176: assertTrue("match /*", PathMap.match("/*", "/anything"));
177: assertTrue("match /foo", PathMap.match("/foo", "/foo"));
178: assertTrue("!match /foo", !PathMap.match("/foo", "/bar"));
179: assertTrue("match /foo/*", PathMap.match("/foo/*", "/foo"));
180: assertTrue("match /foo/*", PathMap.match("/foo/*", "/foo/"));
181: assertTrue("match /foo/*", PathMap.match("/foo/*",
182: "/foo/anything"));
183: assertTrue("!match /foo/*", !PathMap.match("/foo/*", "/bar"));
184: assertTrue("!match /foo/*", !PathMap.match("/foo/*", "/bar/"));
185: assertTrue("!match /foo/*", !PathMap.match("/foo/*",
186: "/bar/anything"));
187: assertTrue("match *.foo", PathMap
188: .match("*.foo", "anything.foo"));
189: assertTrue("!match *.foo", !PathMap.match("*.foo",
190: "anything.bar"));
191: }
192:
193: /**
194: * See JIRA issue: JETTY-88.
195: */
196: public void testPathMappingsOnlyMatchOnDirectoryNames()
197: throws Exception {
198: String spec = "/xyz/*";
199:
200: assertMatch(spec, "/xyz");
201: assertMatch(spec, "/xyz/");
202: assertMatch(spec, "/xyz/123");
203: assertMatch(spec, "/xyz/123/");
204: assertMatch(spec, "/xyz/123.txt");
205: assertNotMatch(spec, "/xyz123");
206: assertNotMatch(spec, "/xyz123;jessionid=99");
207: assertNotMatch(spec, "/xyz123/");
208: assertNotMatch(spec, "/xyz123/456");
209: assertNotMatch(spec, "/xyz.123");
210: assertNotMatch(spec, "/xyz;123"); // as if the ; was encoded and part of the path
211: assertNotMatch(spec, "/xyz?123"); // as if the ? was encoded and part of the path
212: }
213:
214: private void assertMatch(String spec, String path) {
215: boolean match = PathMap.match(spec, path);
216: assertTrue("PathSpec '" + spec + "' should match path '" + path
217: + "'", match);
218: }
219:
220: private void assertNotMatch(String spec, String path) {
221: boolean match = PathMap.match(spec, path);
222: assertFalse("PathSpec '" + spec + "' should not match path '"
223: + path + "'", match);
224: }
225: }
|