1 18 19 package org.apache.jmeter.protocol.http.modifier; 20 import java.io.Serializable ; 21 22 import junit.framework.TestCase; 23 24 import org.apache.jmeter.config.Argument; 25 import org.apache.jmeter.config.Arguments; 26 import org.apache.jmeter.processor.PreProcessor; 27 import org.apache.jmeter.protocol.http.sampler.HTTPSampler; 28 import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase; 29 import org.apache.jmeter.protocol.http.util.HTTPArgument; 30 import org.apache.jmeter.samplers.SampleResult; 31 import org.apache.jmeter.samplers.Sampler; 32 import org.apache.jmeter.testelement.AbstractTestElement; 33 import org.apache.jmeter.testelement.property.BooleanProperty; 34 import org.apache.jmeter.threads.JMeterContext; 35 import org.apache.jmeter.threads.JMeterContextService; 36 import org.apache.jmeter.util.JMeterUtils; 37 import org.apache.oro.text.regex.MatchResult; 38 import org.apache.oro.text.regex.Pattern; 39 import org.apache.oro.text.regex.Perl5Compiler; 40 import org.apache.oro.text.regex.Perl5Matcher; 41 42 47 public class URLRewritingModifier 48 extends AbstractTestElement 49 implements Serializable , PreProcessor 50 { 51 52 private Pattern pathExtensionEqualsQuestionmarkRegexp; 53 private Pattern pathExtensionEqualsNoQuestionmarkRegexp; 54 private Pattern parameterRegexp; 55 private Pattern pathExtensionNoEqualsQuestionmarkRegexp; 56 private Pattern pathExtensionNoEqualsNoQuestionmarkRegexp; 57 private final static String ARGUMENT_NAME = "argument_name"; 59 private final static String PATH_EXTENSION = "path_extension"; 60 private final static String PATH_EXTENSION_NO_EQUALS = 61 "path_extension_no_equals"; 62 private final static String PATH_EXTENSION_NO_QUESTIONMARK = 63 "path_extension_no_questionmark"; 64 65 public void process() 66 { 67 JMeterContext ctx = getThreadContext(); 68 Sampler sampler = ctx.getCurrentSampler(); 69 SampleResult responseText = ctx.getPreviousResult(); 70 if(responseText == null) 71 { 72 return; 73 } 74 initRegex(getArgumentName()); 75 String text = new String (responseText.getResponseData()); 76 Perl5Matcher matcher = JMeterUtils.getMatcher(); 77 String value = ""; 78 if (isPathExtension() && isPathExtensionNoEquals() && isPathExtensionNoQuestionmark()) 79 { 80 if (matcher.contains(text, pathExtensionNoEqualsNoQuestionmarkRegexp)) 81 { 82 MatchResult result = matcher.getMatch(); 83 value = result.group(1); 84 } 85 } 86 else if (isPathExtension() && isPathExtensionNoEquals()) { 88 if (matcher.contains(text, pathExtensionNoEqualsQuestionmarkRegexp)) 89 { 90 MatchResult result = matcher.getMatch(); 91 value = result.group(1); 92 } 93 } 94 else if (isPathExtension() && isPathExtensionNoQuestionmark()) { 96 if (matcher.contains(text, pathExtensionEqualsNoQuestionmarkRegexp)) 97 { 98 MatchResult result = matcher.getMatch(); 99 value = result.group(1); 100 } 101 } 102 else if (isPathExtension()) { 104 if (matcher.contains(text, pathExtensionEqualsQuestionmarkRegexp)) 105 { 106 MatchResult result = matcher.getMatch(); 107 value = result.group(1); 108 } 109 } 110 else { 112 if (matcher.contains(text, parameterRegexp)) 113 { 114 MatchResult result = matcher.getMatch(); 115 for (int i=1; i<result.groups(); i++) 116 { 117 value = result.group(i); 118 if (value != null) break; 119 } 120 } 121 } 122 123 modify((HTTPSamplerBase) sampler, value); 124 } 125 private void modify(HTTPSamplerBase sampler, String value) 126 { 127 if (isPathExtension()) 128 { 129 if (isPathExtensionNoEquals()) 130 { 131 sampler.setPath( 132 sampler.getPath() + ";" + getArgumentName() + value); 133 } 134 else 135 { 136 sampler.setPath( 137 sampler.getPath() + ";" + getArgumentName() + "=" + value); 138 } 139 } 140 else 141 { 142 sampler.getArguments().removeArgument(getArgumentName()); 143 sampler.getArguments().addArgument( 144 new HTTPArgument(getArgumentName(), value, true)); 145 } 146 } 147 public void setArgumentName(String argName) 148 { 149 setProperty(ARGUMENT_NAME, argName); 150 } 151 private void initRegex(String argName) 152 { 153 pathExtensionEqualsQuestionmarkRegexp = 154 JMeterUtils.getPatternCache().getPattern( 155 ";"+argName + "=([^\"'>&\\s;]*)[&\\s\"'>;]?$?", 156 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK); 157 158 pathExtensionEqualsNoQuestionmarkRegexp = 159 JMeterUtils.getPatternCache().getPattern( 160 ";"+argName + "=([^\"'>&\\s;?]*)[&\\s\"'>;?]?$?", 161 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK); 162 163 pathExtensionNoEqualsQuestionmarkRegexp = 164 JMeterUtils.getPatternCache().getPattern( 165 ";"+argName + "([^\"'>&\\s;]*)[&\\s\"'>;]?$?", 166 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK); 167 168 pathExtensionNoEqualsNoQuestionmarkRegexp = 169 JMeterUtils.getPatternCache().getPattern( 170 ";"+argName + "([^\"'>&\\s;?]*)[&\\s\"'>;?]?$?", 171 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK); 172 173 parameterRegexp = 174 JMeterUtils.getPatternCache().getPattern( 175 "[;\\?&]"+argName + "=([^\"'>&\\s;]*)[&\\s\"'>;]?$?" 176 + "|\\s[Nn][Aa][Mm][Ee]\\s*=\\s*[\"']" 177 + argName 178 + "[\"']" 179 + "[^>]*" 180 + "\\s[vV][Aa][Ll][Uu][Ee]\\s*=\\s*[\"']" 181 + "([^\"']*)" 182 + "[\"']" 183 + "|\\s[vV][Aa][Ll][Uu][Ee]\\s*=\\s*[\"']" 184 + "([^\"']*)" 185 + "[\"']" 186 + "[^>]*" 187 + "\\s[Nn][Aa][Mm][Ee]\\s*=\\s*[\"']" 188 + argName 189 + "[\"']", 190 Perl5Compiler.MULTILINE_MASK | Perl5Compiler.READ_ONLY_MASK); 191 } 196 public String getArgumentName() 197 { 198 return getPropertyAsString(ARGUMENT_NAME); 199 } 200 public void setPathExtension(boolean pathExt) 201 { 202 setProperty(new BooleanProperty(PATH_EXTENSION, pathExt)); 203 } 204 public void setPathExtensionNoEquals(boolean pathExtNoEquals) 205 { 206 setProperty( 207 new BooleanProperty(PATH_EXTENSION_NO_EQUALS, pathExtNoEquals)); 208 } 209 public void setPathExtensionNoQuestionmark(boolean pathExtNoQuestionmark) 210 { 211 setProperty( 212 new BooleanProperty(PATH_EXTENSION_NO_QUESTIONMARK, pathExtNoQuestionmark)); 213 } 214 public boolean isPathExtension() 215 { 216 return getPropertyAsBoolean(PATH_EXTENSION); 217 } 218 public boolean isPathExtensionNoEquals() 219 { 220 return getPropertyAsBoolean(PATH_EXTENSION_NO_EQUALS); 221 } 222 public boolean isPathExtensionNoQuestionmark() 223 { 224 return getPropertyAsBoolean(PATH_EXTENSION_NO_QUESTIONMARK); 225 } 226 227 public static class Test extends TestCase 229 { 230 private SampleResult response = null; 231 232 private JMeterContext context = null; 233 private URLRewritingModifier mod = null; 234 235 public Test(String name) 236 { 237 super(name); 238 } 239 public void setUp() 240 { 241 context = JMeterContextService.getContext(); 242 mod = new URLRewritingModifier(); 243 mod.setThreadContext(context); 244 } 245 public void testGrabSessionId() throws Exception 246 { 247 String html = 248 "location: http://server.com/index.html" 249 + "?session_id=jfdkjdkf%20jddkfdfjkdjfdf%22;"; 250 response = new SampleResult(); 251 response.setResponseData(html.getBytes()); 252 mod.setArgumentName("session_id"); 253 HTTPSampler sampler = createSampler(); 254 sampler.addArgument("session_id", "adfasdfdsafasdfasd"); 255 context.setCurrentSampler(sampler); 256 context.setPreviousResult(response); 257 mod.process(); 258 Arguments args = sampler.getArguments(); 259 assertEquals( 260 "jfdkjdkf jddkfdfjkdjfdf\"", 261 ((Argument) args.getArguments().get(0).getObjectValue()) 262 .getValue()); 263 assertEquals( 264 "http://server.com/index.html?" 265 + "session_id=jfdkjdkf+jddkfdfjkdjfdf%22", 266 sampler.toString()); 267 } 268 public void testGrabSessionId2() throws Exception 269 { 270 String html = 271 "<a HREF=\"http://server.com/index.html?" 272 + "session_id=jfdkjdkfjddkfdfjkdjfdf\">"; 273 response = new SampleResult(); 274 response.setResponseData(html.getBytes()); 275 mod.setArgumentName("session_id"); 276 HTTPSampler sampler = createSampler(); 277 context.setCurrentSampler(sampler); 278 context.setPreviousResult(response); 279 mod.process(); 280 Arguments args = sampler.getArguments(); 281 assertEquals( 282 "jfdkjdkfjddkfdfjkdjfdf", 283 ((Argument) args.getArguments().get(0).getObjectValue()) 284 .getValue()); 285 } 286 private HTTPSampler createSampler() 287 { 288 HTTPSampler sampler = new HTTPSampler(); 289 sampler.setDomain("server.com"); 290 sampler.setPath("index.html"); 291 sampler.setMethod(HTTPSampler.GET); 292 sampler.setProtocol("http"); 293 return sampler; 294 } 295 296 public void testGrabSessionId3() throws Exception 297 { 298 String html = "href='index.html?session_id=jfdkjdkfjddkfdfjkdjfdf'"; 299 response = new SampleResult(); 300 response.setResponseData(html.getBytes()); 301 mod.setArgumentName("session_id"); 302 HTTPSampler sampler = createSampler(); 303 context.setCurrentSampler(sampler); 304 context.setPreviousResult(response); 305 mod.process(); 306 Arguments args = sampler.getArguments(); 307 assertEquals( 308 "jfdkjdkfjddkfdfjkdjfdf", 309 ((Argument) args.getArguments().get(0).getObjectValue()) 310 .getValue()); 311 } 312 313 public void testGrabSessionIdEndedInTab() throws Exception 314 { 315 String html = "href='index.html?session_id=jfdkjdkfjddkfdfjkdjfdf\t"; 316 response = new SampleResult(); 317 response.setResponseData(html.getBytes()); 318 mod.setArgumentName("session_id"); 319 HTTPSampler sampler = createSampler(); 320 context.setCurrentSampler(sampler); 321 context.setPreviousResult(response); 322 mod.process(); 323 Arguments args = sampler.getArguments(); 324 assertEquals( 325 "jfdkjdkfjddkfdfjkdjfdf", 326 ((Argument) args.getArguments().get(0).getObjectValue()) 327 .getValue()); 328 } 329 330 public void testGrabSessionId4() throws Exception 331 { 332 String html = 333 "href='index.html;%24sid%24KQNq3AAADQZoEQAxlkX8uQV5bjqVBPbT'"; 334 response = new SampleResult(); 335 response.setResponseData(html.getBytes()); 336 mod.setArgumentName("%24sid%24"); 337 mod.setPathExtension(true); 338 mod.setPathExtensionNoEquals(true); 339 HTTPSampler sampler = createSampler(); 340 context.setCurrentSampler(sampler); 341 context.setPreviousResult(response); 342 mod.process(); 343 assertEquals( 345 "index.html;%24sid%24KQNq3AAADQZoEQAxlkX8uQV5bjqVBPbT", 346 sampler.getPath()); 347 } 348 349 public void testGrabSessionIdFromForm() throws Exception 350 { 351 String [] html = new String [] { 352 "<input name=\"sid\" value=\"myId\">", 353 "<input name='sid' value='myId'>", 354 "<input value=\"myId\" NAME='sid'>", 355 "<input VALUE='myId' name=\"sid\">", 356 "<input blah blah value=\"myId\" yoda yoda NAME='sid'>", 357 }; 358 for (int i=0; i<html.length; i++) 359 { 360 response = new SampleResult(); 361 response.setResponseData(html[i].getBytes()); 362 URLRewritingModifier mod = new URLRewritingModifier(); 363 mod.setThreadContext(context); 364 mod.setArgumentName("sid"); 365 mod.setPathExtension(false); 366 HTTPSampler sampler = createSampler(); 367 context.setCurrentSampler(sampler); 368 context.setPreviousResult(response); 369 mod.process(); 370 Arguments args = sampler.getArguments(); 371 assertEquals( 372 "For case i="+i, 373 "myId", 374 ((Argument) args.getArguments().get(0).getObjectValue()) 375 .getValue()); 376 } 377 } 378 } 379 } 380 | Popular Tags |