001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.modifier;
020:
021: import org.apache.jmeter.config.Argument;
022: import org.apache.jmeter.config.Arguments;
023: import org.apache.jmeter.junit.JMeterTestCase;
024: import org.apache.jmeter.protocol.http.sampler.HTTPNullSampler;
025: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
026: import org.apache.jmeter.samplers.NullSampler;
027: import org.apache.jmeter.samplers.SampleResult;
028: import org.apache.jmeter.samplers.Sampler;
029: import org.apache.jmeter.threads.JMeterContext;
030: import org.apache.jmeter.threads.JMeterContextService;
031:
032: public class TestURLRewritingModifier extends JMeterTestCase {
033: private SampleResult response = null;
034:
035: private JMeterContext context = null;
036:
037: private URLRewritingModifier mod = null;
038:
039: public TestURLRewritingModifier(String name) {
040: super (name);
041: }
042:
043: public void setUp() {
044: context = JMeterContextService.getContext();
045: mod = new URLRewritingModifier();
046: mod.setThreadContext(context);
047: }
048:
049: public void testNonHTTPSampler() throws Exception {
050: Sampler sampler = new NullSampler();
051: response = new SampleResult();
052: context.setCurrentSampler(sampler);
053: context.setPreviousResult(response);
054: mod.process();
055: }
056:
057: public void testGrabSessionId() throws Exception {
058: String html = "location: http://server.com/index.html"
059: + "?session_id=jfdkjdkf%20jddkfdfjkdjfdf%22;";
060: response = new SampleResult();
061: response.setResponseData(html.getBytes());
062: mod.setArgumentName("session_id");
063: HTTPSamplerBase sampler = createSampler();
064: sampler.addArgument("session_id", "adfasdfdsafasdfasd");
065: context.setCurrentSampler(sampler);
066: context.setPreviousResult(response);
067: mod.process();
068: Arguments args = sampler.getArguments();
069: assertEquals("jfdkjdkf jddkfdfjkdjfdf\"", ((Argument) args
070: .getArguments().get(0).getObjectValue()).getValue());
071: assertEquals("http://server.com/index.html?"
072: + "session_id=jfdkjdkf+jddkfdfjkdjfdf%22", sampler
073: .toString());
074: }
075:
076: public void testGrabSessionId2() throws Exception {
077: String html = "<a href=\"http://server.com/index.html?"
078: + "session_id=jfdkjdkfjddkfdfjkdjfdf\">";
079: response = new SampleResult();
080: response.setResponseData(html.getBytes());
081: mod.setArgumentName("session_id");
082: HTTPSamplerBase sampler = createSampler();
083: context.setCurrentSampler(sampler);
084: context.setPreviousResult(response);
085: mod.process();
086: Arguments args = sampler.getArguments();
087: assertEquals("jfdkjdkfjddkfdfjkdjfdf", ((Argument) args
088: .getArguments().get(0).getObjectValue()).getValue());
089: }
090:
091: private HTTPSamplerBase createSampler() {
092: HTTPSamplerBase sampler = new HTTPNullSampler();
093: sampler.setDomain("server.com");
094: sampler.setPath("index.html");
095: sampler.setMethod(HTTPSamplerBase.GET);
096: sampler.setProtocol("http");
097: return sampler;
098: }
099:
100: public void testGrabSessionId3() throws Exception {
101: String html = "href='index.html?session_id=jfdkjdkfjddkfdfjkdjfdf'";
102: response = new SampleResult();
103: response.setResponseData(html.getBytes());
104: mod.setArgumentName("session_id");
105: HTTPSamplerBase sampler = createSampler();
106: context.setCurrentSampler(sampler);
107: context.setPreviousResult(response);
108: mod.process();
109: Arguments args = sampler.getArguments();
110: assertEquals("jfdkjdkfjddkfdfjkdjfdf", ((Argument) args
111: .getArguments().get(0).getObjectValue()).getValue());
112: }
113:
114: public void testGrabSessionIdEndedInTab() throws Exception {
115: String html = "href='index.html?session_id=jfdkjdkfjddkfdfjkdjfdf\t";
116: response = new SampleResult();
117: response.setResponseData(html.getBytes());
118: mod.setArgumentName("session_id");
119: HTTPSamplerBase sampler = createSampler();
120: context.setCurrentSampler(sampler);
121: context.setPreviousResult(response);
122: mod.process();
123: Arguments args = sampler.getArguments();
124: assertEquals("jfdkjdkfjddkfdfjkdjfdf", ((Argument) args
125: .getArguments().get(0).getObjectValue()).getValue());
126: }
127:
128: public void testGrabSessionId4() throws Exception {
129: String html = "href='index.html;%24sid%24KQNq3AAADQZoEQAxlkX8uQV5bjqVBPbT'";
130: response = new SampleResult();
131: response.setResponseData(html.getBytes());
132: mod.setArgumentName("%24sid%24");
133: mod.setPathExtension(true);
134: mod.setPathExtensionNoEquals(true);
135: HTTPSamplerBase sampler = createSampler();
136: context.setCurrentSampler(sampler);
137: context.setPreviousResult(response);
138: mod.process();
139: // Arguments args = sampler.getArguments();
140: assertEquals(
141: "index.html;%24sid%24KQNq3AAADQZoEQAxlkX8uQV5bjqVBPbT",
142: sampler.getPath());
143: }
144:
145: public void testGrabSessionId5() throws Exception {
146: String html = "location: http://server.com/index.html"
147: + "?session[33]=jfdkjdkf%20jddkfdfjkdjfdf%22;";
148: response = new SampleResult();
149: response.setResponseData(html.getBytes());
150: mod.setArgumentName("session[33]");
151: HTTPSamplerBase sampler = createSampler();
152: sampler.addArgument("session[33]", "adfasdfdsafasdfasd");
153: context.setCurrentSampler(sampler);
154: context.setPreviousResult(response);
155: mod.process();
156: Arguments args = sampler.getArguments();
157: assertEquals("jfdkjdkf jddkfdfjkdjfdf\"", ((Argument) args
158: .getArguments().get(0).getObjectValue()).getValue());
159: assertEquals(
160: "http://server.com/index.html?session%5B33%5D=jfdkjdkf+jddkfdfjkdjfdf%22",
161: sampler.toString());
162: }
163:
164: public void testGrabSessionIdFromForm() throws Exception {
165: String[] html = new String[] {
166: "<input name=\"sid\" value=\"myId\">",
167: "<input name='sid' value='myId'>",
168: "<input value=\"myId\" NAME='sid'>",
169: "<input VALUE='myId' name=\"sid\">",
170: "<input blah blah value=\"myId\" yoda yoda NAME='sid'>",
171: "<input type=\"HIDDEN\" name=\"sid\" value=\"myId\">",
172: "<input type=\"HIDDEN\" name=\"sid\"\tvalue=\"myId\">", };
173: for (int i = 0; i < html.length; i++) {
174: response = new SampleResult();
175: response.setResponseData(html[i].getBytes());
176: URLRewritingModifier newMod = new URLRewritingModifier();
177: newMod.setThreadContext(context);
178: newMod.setArgumentName("sid");
179: newMod.setPathExtension(false);
180: HTTPSamplerBase sampler = createSampler();
181: context.setCurrentSampler(sampler);
182: context.setPreviousResult(response);
183: newMod.process();
184: Arguments args = sampler.getArguments();
185: assertEquals("For case i=" + i, "myId", ((Argument) args
186: .getArguments().get(0).getObjectValue()).getValue());
187: }
188: }
189:
190: public void testCache() throws Exception {
191: String[] html = new String[] {
192: "<input name=\"sid\" value=\"myId\">", "<html></html>", // No entry; check it is still present
193: };
194: URLRewritingModifier newMod = new URLRewritingModifier();
195: newMod.setShouldCache(true);
196: newMod.setThreadContext(context);
197: newMod.setArgumentName("sid");
198: newMod.setPathExtension(false);
199: for (int i = 0; i < html.length; i++) {
200: response = new SampleResult();
201: response.setResponseData(html[i].getBytes());
202: HTTPSamplerBase sampler = createSampler();
203: context.setCurrentSampler(sampler);
204: context.setPreviousResult(response);
205: newMod.process();
206: Arguments args = sampler.getArguments();
207: assertEquals("For case i=" + i, "myId", ((Argument) args
208: .getArguments().get(0).getObjectValue()).getValue());
209: }
210: }
211:
212: public void testNoCache() throws Exception {
213: String[] html = new String[] {
214: "<input name=\"sid\" value=\"myId\">", "myId",
215: "<html></html>", "", };
216: URLRewritingModifier newMod = new URLRewritingModifier();
217: newMod.setThreadContext(context);
218: newMod.setArgumentName("sid");
219: newMod.setPathExtension(false);
220: newMod.setShouldCache(false);
221: for (int i = 0; i < html.length / 2; i++) {
222: response = new SampleResult();
223: response.setResponseData(html[i * 2].getBytes());
224: HTTPSamplerBase sampler = createSampler();
225: context.setCurrentSampler(sampler);
226: context.setPreviousResult(response);
227: newMod.process();
228: Arguments args = sampler.getArguments();
229: assertEquals("For case i=" + i, html[i * 2 + 1],
230: ((Argument) args.getArguments().get(0)
231: .getObjectValue()).getValue());
232: }
233: }
234: }
|