1 22 23 24 package org.jboss.web.rewrite; 25 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 public class RewriteTestCase extends TestCase { 31 32 protected class TestResolver extends Resolver { 33 34 public String resolve(String key) { 35 return "server_variable_value[" + key + "]"; 36 } 37 38 public String resolveHttp(String key) { 39 return "http_header_value[" + key + "]"; 40 } 41 42 public String resolveSsl(String key) { 43 return "ssl_property_value[" + key + "]"; 44 } 45 46 public boolean resolveResource(int type, String name) { 47 return true; 48 } 49 50 } 51 52 57 public RewriteTestCase(String name) { 58 super(name); 59 } 60 61 64 public void setUp() { 65 } 66 67 70 public static Test suite() { 71 return (new TestSuite(RewriteTestCase.class)); 72 } 73 74 77 public void tearDown() { 78 } 79 80 public void testCanonicalUrl() { 81 Resolver resolver = new TestResolver(); 82 83 RewriteRule rule1 = new RewriteRule(); 84 rule1.setPatternString("^/~([^/]+)/?(.*)"); 85 rule1.setSubstitutionString("/u/$1/$2"); 86 87 RewriteRule rule2 = new RewriteRule(); 88 rule2.setPatternString("^/([uge])/([^/]+)$"); 89 rule2.setSubstitutionString("/$1/$2/"); 90 91 RewriteRule[] rules = new RewriteRule[2]; 92 rules[0] = rule1; 93 rules[1] = rule2; 94 for (int i = 0; i < rules.length; i++) { 95 rules[i].parse(null); 96 } 97 98 String result = rewriteUrl("/~user/foo/bar", resolver, rules).toString(); 99 assertEquals("/u/user/foo/bar", result); 100 result = rewriteUrl("/u/user", resolver, rules).toString(); 101 assertEquals("/u/user/", result); 102 } 103 104 public void testCanonicalHostname() { 105 Resolver resolver = new TestResolver(); 106 107 RewriteRule rule = new RewriteRule(); 108 rule.setPatternString("^/(.*)"); 109 rule.setSubstitutionString("http://fully.qualified.domain.name:%{SERVER_PORT}/$1"); 110 RewriteRule[] rules = new RewriteRule[1]; 111 rules[0] = rule; 112 113 RewriteCond cond1 = new RewriteCond(); 114 cond1.setTestString("%{HTTP_HOST}"); 115 cond1.setCondPattern("!^fully\\.qualified\\.domain\\.name"); 116 cond1.setNocase(true); 117 118 RewriteCond cond2 = new RewriteCond(); 119 cond2.setTestString("%{HTTP_HOST}"); 120 cond2.setCondPattern("!^$"); 121 122 RewriteCond cond3 = new RewriteCond(); 123 cond3.setTestString("%{SERVER_PORT}"); 124 cond3.setCondPattern("!^80$"); 125 126 rule.addCondition(cond1); 127 rule.addCondition(cond2); 128 rule.addCondition(cond3); 129 130 for (int i = 0; i < rules.length; i++) { 131 rules[i].parse(null); 132 } 133 134 String result = rewriteUrl("/foo/bar", resolver, rules).toString(); 135 assertEquals("http://fully.qualified.domain.name:server_variable_value[SERVER_PORT]/foo/bar", result); 136 } 137 138 public void testMovedDocumentRoot() { 139 Resolver resolver = new TestResolver(); 140 141 RewriteRule rule = new RewriteRule(); 142 rule.setPatternString("^/$"); 143 rule.setSubstitutionString("/about/"); 144 145 RewriteRule[] rules = new RewriteRule[1]; 146 rules[0] = rule; 147 for (int i = 0; i < rules.length; i++) { 148 rules[i].parse(null); 149 } 150 151 String result = rewriteUrl("/", resolver, rules).toString(); 152 assertEquals("/about/", result); 153 } 154 155 public void testParsing() { 156 Object result = null; 157 String test = null; 158 RewriteRule resultRule = null; 159 RewriteCond resultCond = null; 160 161 test = "RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]"; 162 result = RewriteValve.parse(test); 163 if (result instanceof RewriteRule) { 164 resultRule = (RewriteRule) result; 165 } 166 assertNotNull(resultRule); 167 assertTrue(resultRule.isRedirect()); 168 assertEquals(resultRule.getPatternString(), "^/~([^/]+)/?(.*)"); 169 assertEquals(resultRule.getSubstitutionString(), "/u/$1/$2"); 170 resultRule = null; 171 172 test = "RewriteRule ^/(.*) http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R]"; 173 result = RewriteValve.parse(test); 174 if (result instanceof RewriteRule) { 175 resultRule = (RewriteRule) result; 176 } 177 assertNotNull(resultRule); 178 assertTrue(resultRule.isRedirect()); 179 assertTrue(resultRule.isLast()); 180 assertEquals(resultRule.getPatternString(), "^/(.*)"); 181 assertEquals(resultRule.getSubstitutionString(), "http://fully.qualified.domain.name:%{SERVER_PORT}/$1"); 182 resultRule = null; 183 184 test = "RewriteCond %{HTTP_HOST} !^fully\\.qualified\\.domain\\.name [NC]"; 185 result = RewriteValve.parse(test); 186 if (result instanceof RewriteCond) { 187 resultCond = (RewriteCond) result; 188 } 189 assertNotNull(resultCond); 190 assertTrue(resultCond.isNocase()); 191 assertEquals(resultCond.getTestString(), "%{HTTP_HOST}"); 192 assertEquals(resultCond.getCondPattern(), "!^fully\\.qualified\\.domain\\.name"); 193 resultCond = null; 194 195 } 196 197 198 public static CharSequence rewriteUrl(CharSequence url, Resolver resolver, RewriteRule[] rules) { 199 if (rules == null) 200 return url; 201 for (int i = 0; i < rules.length; i++) { 202 CharSequence newurl = rules[i].evaluate(url, resolver); 203 if (newurl != null) { 204 url = newurl; 207 } 208 } 209 return url; 210 } 211 212 } 213 | Popular Tags |