1 /* 2 * Copyright (C) 2014 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.inject.servlet; 18 19 import static junit.framework.Assert.fail; 20 21 import junit.framework.TestCase; 22 23 public class UriPatternTypeTest extends TestCase { 24 testMatches_servlet()25 public void testMatches_servlet() { 26 UriPatternMatcher pattern = UriPatternType.get(UriPatternType.SERVLET, "/foo/*"); 27 assertTrue(pattern.matches("/foo/asdf")); 28 assertTrue(pattern.matches("/foo/asdf?val=1")); 29 assertFalse(pattern.matches("/path/file.bar")); 30 assertFalse(pattern.matches("/path/file.bar?val=1")); 31 assertFalse(pattern.matches("/asdf")); 32 assertFalse(pattern.matches("/asdf?val=1")); 33 34 pattern = UriPatternType.get(UriPatternType.SERVLET, "*.bar"); 35 assertFalse(pattern.matches("/foo/asdf")); 36 assertFalse(pattern.matches("/foo/asdf?val=1")); 37 assertTrue(pattern.matches("/path/file.bar")); 38 assertTrue(pattern.matches("/path/file.bar?val=1")); 39 assertFalse(pattern.matches("/asdf")); 40 assertFalse(pattern.matches("/asdf?val=1")); 41 42 pattern = UriPatternType.get(UriPatternType.SERVLET, "/asdf"); 43 assertFalse(pattern.matches("/foo/asdf")); 44 assertFalse(pattern.matches("/foo/asdf?val=1")); 45 assertFalse(pattern.matches("/path/file.bar")); 46 assertFalse(pattern.matches("/path/file.bar?val=1")); 47 assertTrue(pattern.matches("/asdf")); 48 assertTrue(pattern.matches("/asdf?val=1")); 49 } 50 testMatches_regex()51 public void testMatches_regex() { 52 UriPatternMatcher pattern = UriPatternType.get(UriPatternType.REGEX, "/.*/foo"); 53 assertFalse(pattern.matches("/foo/asdf")); 54 assertFalse(pattern.matches("/foo/asdf?val=1")); 55 assertTrue(pattern.matches("/path/foo")); 56 assertTrue(pattern.matches("/path/foo?val=1")); 57 assertFalse(pattern.matches("/foo")); 58 assertFalse(pattern.matches("/foo?val=1")); 59 } 60 testPatternWithPercentEncodedChars_servlet()61 public void testPatternWithPercentEncodedChars_servlet() { 62 try { 63 UriPatternType.get(UriPatternType.SERVLET, "/foo/%2f/*"); 64 fail(); 65 } catch (IllegalArgumentException iae) { 66 assertTrue(iae.getMessage().contains("Servlet patterns cannot contain escape patterns.")); 67 } 68 } 69 } 70