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 java.io.FileInputStream;
022: import java.net.MalformedURLException;
023: import java.net.URL;
024: import java.net.URLEncoder;
025:
026: import org.apache.jmeter.junit.JMeterTestCase;
027: import org.apache.jmeter.protocol.http.sampler.HTTPNullSampler;
028: import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
029: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
030: import org.apache.jmeter.save.SaveService;
031: import org.apache.jmeter.threads.JMeterContext;
032: import org.apache.jmeter.threads.JMeterContextService;
033: import org.apache.jorphan.io.TextFile;
034:
035: public class TestAnchorModifier extends JMeterTestCase {
036: private AnchorModifier parser = new AnchorModifier();
037:
038: public TestAnchorModifier(String name) {
039: super (name);
040: }
041:
042: private JMeterContext jmctx = null;
043:
044: public void setUp() {
045: jmctx = JMeterContextService.getContext();
046: parser.setThreadContext(jmctx);
047: }
048:
049: public void testProcessingHTMLFile(String HTMLFileName)
050: throws Exception {
051: HTTPSamplerBase config = (HTTPSamplerBase) SaveService
052: .loadTree(
053: new FileInputStream(System
054: .getProperty("user.dir")
055: + "/testfiles/load_bug_list.jmx"))
056: .getArray()[0];
057: config.setRunningVersion(true);
058: HTTPSampleResult result = new HTTPSampleResult();
059: HTTPSamplerBase context = (HTTPSamplerBase) SaveService
060: .loadTree(
061: new FileInputStream(System
062: .getProperty("user.dir")
063: + "/testfiles/Load_JMeter_Page.jmx"))
064: .getArray()[0];
065: jmctx.setCurrentSampler(context);
066: jmctx.setCurrentSampler(config);
067: result.setResponseData(new TextFile(System
068: .getProperty("user.dir")
069: + HTMLFileName).getText().getBytes());
070: result.setSampleLabel(context.toString());
071: result.setSamplerData(context.toString());
072: result
073: .setURL(new URL(
074: "http://issues.apache.org/fakepage.html"));
075: jmctx.setPreviousResult(result);
076: AnchorModifier modifier = new AnchorModifier();
077: modifier.setThreadContext(jmctx);
078: modifier.process();
079: assertEquals(
080: "http://issues.apache.org/bugzilla/buglist.cgi?"
081: + "bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED"
082: + "&email1=&emailtype1=substring&emailassigned_to1=1"
083: + "&email2=&emailtype2=substring&emailreporter2=1"
084: + "&bugidtype=include&bug_id=&changedin=&votes="
085: + "&chfieldfrom=&chfieldto=Now&chfieldvalue="
086: + "&product=JMeter&short_desc=&short_desc_type=substring"
087: + "&long_desc=&long_desc_type=substring&bug_file_loc="
088: + "&bug_file_loc_type=substring&keywords="
089: + "&keywords_type=anywords"
090: + "&field0-0-0=noop&type0-0-0=noop&value0-0-0="
091: + "&cmdtype=doit&order=Reuse+same+sort+as+last+time",
092: config.toString());
093: config.recoverRunningVersion();
094: assertEquals(
095: "http://issues.apache.org/bugzilla/buglist.cgi?"
096: + "bug_status=.*&bug_status=.*&bug_status=.*&email1="
097: + "&emailtype1=substring&emailassigned_to1=1&email2="
098: + "&emailtype2=substring&emailreporter2=1"
099: + "&bugidtype=include&bug_id=&changedin=&votes="
100: + "&chfieldfrom=&chfieldto=Now&chfieldvalue="
101: + "&product=JMeter&short_desc=&short_desc_type=substring"
102: + "&long_desc=&long_desc_type=substring&bug_file_loc="
103: + "&bug_file_loc_type=substring&keywords="
104: + "&keywords_type=anywords&field0-0-0=noop"
105: + "&type0-0-0=noop&value0-0-0=&cmdtype=doit"
106: + "&order=Reuse+same+sort+as+last+time", config
107: .toString());
108: }
109:
110: public void testModifySampler() throws Exception {
111: testProcessingHTMLFile("/testfiles/jmeter_home_page.html");
112: }
113:
114: public void testModifySamplerWithRelativeLink() throws Exception {
115: testProcessingHTMLFile("/testfiles/jmeter_home_page_with_relative_links.html");
116: }
117:
118: public void testModifySamplerWithBaseHRef() throws Exception {
119: testProcessingHTMLFile("/testfiles/jmeter_home_page_with_base_href.html");
120: }
121:
122: public void testSimpleParse() throws Exception {
123: HTTPSamplerBase config = makeUrlConfig(".*/index\\.html");
124: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
125: String responseText = "<html><head><title>Test page</title></head><body>"
126: + "<a href=\"index.html\">Goto index page</a></body></html>";
127: HTTPSampleResult result = new HTTPSampleResult();
128: jmctx.setCurrentSampler(context);
129: jmctx.setCurrentSampler(config);
130: result.setResponseData(responseText.getBytes());
131: result.setSampleLabel(context.toString());
132: result.setSamplerData(context.toString());
133: result.setURL(context.getUrl());
134: jmctx.setPreviousResult(result);
135: parser.process();
136: assertEquals("http://www.apache.org/subdir/index.html", config
137: .getUrl().toString());
138: }
139:
140: // Test https works too
141: public void testSimpleParse1() throws Exception {
142: HTTPSamplerBase config = makeUrlConfig(".*/index\\.html");
143: config.setProtocol(HTTPSamplerBase.PROTOCOL_HTTPS);
144: config.setPort(HTTPSamplerBase.DEFAULT_HTTPS_PORT);
145: HTTPSamplerBase context = makeContext("https://www.apache.org/subdir/previous.html");
146: String responseText = "<html><head><title>Test page</title></head><body>"
147: + "<a href=\"index.html\">Goto index page</a></body></html>";
148: HTTPSampleResult result = new HTTPSampleResult();
149: jmctx.setCurrentSampler(context);
150: jmctx.setCurrentSampler(config);
151: result.setResponseData(responseText.getBytes());
152: result.setSampleLabel(context.toString());
153: result.setSamplerData(context.toString());
154: result.setURL(context.getUrl());
155: jmctx.setPreviousResult(result);
156: parser.process();
157: assertEquals("https://www.apache.org/subdir/index.html", config
158: .getUrl().toString());
159: }
160:
161: public void testSimpleParse2() throws Exception {
162: HTTPSamplerBase config = makeUrlConfig("/index\\.html");
163: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
164: String responseText = "<html><head><title>Test page</title></head><body>"
165: + "<a href=\"/index.html\">Goto index page</a>"
166: + "hfdfjiudfjdfjkjfkdjf"
167: + "<b>bold text</b><a href=lowerdir/index.html>lower</a>"
168: + "</body></html>";
169: HTTPSampleResult result = new HTTPSampleResult();
170: result.setResponseData(responseText.getBytes());
171: result.setSampleLabel(context.toString());
172: result.setURL(context.getUrl());
173: jmctx.setCurrentSampler(context);
174: jmctx.setCurrentSampler(config);
175: jmctx.setPreviousResult(result);
176: parser.process();
177: String newUrl = config.getUrl().toString();
178: assertTrue("http://www.apache.org/index.html".equals(newUrl)
179: || "http://www.apache.org/subdir/lowerdir/index.html"
180: .equals(newUrl));
181: }
182:
183: public void testSimpleParse3() throws Exception {
184: HTTPSamplerBase config = makeUrlConfig(".*index.*");
185: config.getArguments().addArgument("param1", "value1");
186: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
187: String responseText = "<html><head><title>Test page</title></head><body>"
188: + "<a href=\"/home/index.html?param1=value1\">"
189: + "Goto index page</a></body></html>";
190: HTTPSampleResult result = new HTTPSampleResult();
191: result.setResponseData(responseText.getBytes());
192: result.setSampleLabel(context.toString());
193: result.setURL(context.getUrl());
194: jmctx.setCurrentSampler(context);
195: jmctx.setCurrentSampler(config);
196: jmctx.setPreviousResult(result);
197: parser.process();
198: String newUrl = config.getUrl().toString();
199: assertEquals(
200: "http://www.apache.org/home/index.html?param1=value1",
201: newUrl);
202: }
203:
204: public void testSimpleParse4() throws Exception {
205: HTTPSamplerBase config = makeUrlConfig("/subdir/index\\..*");
206: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
207: String responseText = "<html><head><title>Test page</title></head><body>"
208: + "<A HREF=\"index.html\">Goto index page</A></body></html>";
209: HTTPSampleResult result = new HTTPSampleResult();
210: result.setResponseData(responseText.getBytes());
211: result.setSampleLabel(context.toString());
212: result.setURL(context.getUrl());
213: jmctx.setCurrentSampler(context);
214: jmctx.setCurrentSampler(config);
215: jmctx.setPreviousResult(result);
216: parser.process();
217: String newUrl = config.getUrl().toString();
218: assertEquals("http://www.apache.org/subdir/index.html", newUrl);
219: }
220:
221: public void testSimpleParse5() throws Exception {
222: HTTPSamplerBase config = makeUrlConfig("/subdir/index\\.h.*");
223: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/one/previous.html");
224: String responseText = "<html><head><title>Test page</title></head><body>"
225: + "<a href=\"../index.html\">Goto index page</a></body></html>";
226: HTTPSampleResult result = new HTTPSampleResult();
227: result.setResponseData(responseText.getBytes());
228: result.setSampleLabel(context.toString());
229: result.setURL(context.getUrl());
230: jmctx.setCurrentSampler(context);
231: jmctx.setCurrentSampler(config);
232: jmctx.setPreviousResult(result);
233: parser.process();
234: String newUrl = config.getUrl().toString();
235: assertEquals("http://www.apache.org/subdir/index.html", newUrl);
236: }
237:
238: public void testFailSimpleParse1() throws Exception {
239: HTTPSamplerBase config = makeUrlConfig(".*index.*?param2=.+1");
240: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
241: String responseText = "<html><head><title>Test page</title></head><body>"
242: + "<a href=\"/home/index.html?param1=value1\">"
243: + "Goto index page</a></body></html>";
244: HTTPSampleResult result = new HTTPSampleResult();
245: String newUrl = config.getUrl().toString();
246: result.setResponseData(responseText.getBytes());
247: result.setSampleLabel(context.toString());
248: result.setURL(context.getUrl());
249: jmctx.setCurrentSampler(context);
250: jmctx.setCurrentSampler(config);
251: jmctx.setPreviousResult(result);
252: parser.process();
253: assertEquals(newUrl, config.getUrl().toString());
254: }
255:
256: public void testFailSimpleParse3() throws Exception {
257: HTTPSamplerBase config = makeUrlConfig("/home/index.html");
258: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
259: String responseText = "<html><head><title>Test page</title></head><body>"
260: + "<a href=\"/home/index.html?param1=value1\">"
261: + "Goto index page</a></body></html>";
262: HTTPSampleResult result = new HTTPSampleResult();
263: String newUrl = config.getUrl().toString();
264: result.setResponseData(responseText.getBytes());
265: result.setSampleLabel(context.toString());
266: result.setURL(context.getUrl());
267: jmctx.setCurrentSampler(context);
268: jmctx.setCurrentSampler(config);
269: jmctx.setPreviousResult(result);
270: parser.process();
271: assertEquals(newUrl + "?param1=value1", config.getUrl()
272: .toString());
273: }
274:
275: public void testFailSimpleParse2() throws Exception {
276: HTTPSamplerBase config = makeUrlConfig(".*login\\.html");
277: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
278: String responseText = "<html><head><title>Test page</title></head><body>"
279: + "<a href=\"/home/index.html?param1=value1\">"
280: + "Goto index page</a></body></html>";
281: HTTPSampleResult result = new HTTPSampleResult();
282: result.setResponseData(responseText.getBytes());
283: result.setSampleLabel(context.toString());
284: result.setURL(context.getUrl());
285: jmctx.setCurrentSampler(context);
286: jmctx.setPreviousResult(result);
287: parser.process();
288: String newUrl = config.getUrl().toString();
289: assertTrue(!"http://www.apache.org/home/index.html?param1=value1"
290: .equals(newUrl));
291: assertEquals(config.getUrl().toString(), newUrl);
292: }
293:
294: public void testSimpleFormParse() throws Exception {
295: HTTPSamplerBase config = makeUrlConfig(".*index.html");
296: config.addArgument("test", "g.*");
297: config.setMethod(HTTPSamplerBase.POST);
298: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
299: String responseText = "<html><head><title>Test page</title></head><body>"
300: + "<form action=\"index.html\" method=\"POST\">"
301: + "<input type=\"checkbox\" name=\"test\""
302: + " value=\"goto\">Goto index page</form></body></html>";
303: HTTPSampleResult result = new HTTPSampleResult();
304: result.setResponseData(responseText.getBytes());
305: result.setSampleLabel(context.toString());
306: result.setURL(context.getUrl());
307: jmctx.setCurrentSampler(context);
308: jmctx.setCurrentSampler(config);
309: jmctx.setPreviousResult(result);
310: parser.process();
311: assertEquals("http://www.apache.org/subdir/index.html", config
312: .getUrl().toString());
313: assertEquals("test=goto", config.getQueryString());
314: }
315:
316: public void testBadCharParse() throws Exception {
317: HTTPSamplerBase config = makeUrlConfig(".*index.html");
318: config.addArgument("te$st", "g.*");
319: config.setMethod(HTTPSamplerBase.POST);
320: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
321: String responseText = "<html><head><title>Test page</title></head><body>"
322: + "<form action=\"index.html\" method=\"POST\">"
323: + "<input type=\"checkbox\" name=\"te$st\""
324: + " value=\"goto\">Goto index page</form></body></html>";
325: HTTPSampleResult result = new HTTPSampleResult();
326: result.setResponseData(responseText.getBytes());
327: result.setSampleLabel(context.toString());
328: result.setURL(context.getUrl());
329: jmctx.setCurrentSampler(context);
330: jmctx.setCurrentSampler(config);
331: jmctx.setPreviousResult(result);
332: parser.process();
333: assertEquals("http://www.apache.org/subdir/index.html", config
334: .getUrl().toString());
335: assertEquals("te%24st=goto", config.getQueryString());
336: }
337:
338: public void testSpecialCharParse() throws Exception {
339: String specialChars = "-_.!~*'()%25";// These are some of the special characters
340: String htmlEncodedFixture = URLEncoder.encode(specialChars,
341: "UTF-8");
342:
343: HTTPSamplerBase config = makeUrlConfig(".*index.html");
344: config.addArgument("test", ".*");
345: config.setMethod(HTTPSamplerBase.POST);
346: HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
347: String responseText = "<html><head><title>Test page</title></head><body>"
348: + "<form action=\"index.html\" method=\"POST\">"
349: + "<input type=\"hidden\" name=\"test\""
350: + " value=\""
351: + htmlEncodedFixture
352: + "\">Goto index page</form></body></html>";
353:
354: HTTPSampleResult result = new HTTPSampleResult();
355: result.setResponseData(responseText.getBytes());
356: result.setSampleLabel(context.toString());
357: result.setURL(context.getUrl());
358: jmctx.setCurrentSampler(context);
359: jmctx.setCurrentSampler(config);
360: jmctx.setPreviousResult(result);
361: parser.process();
362: assertEquals("http://www.apache.org/subdir/index.html", config
363: .getUrl().toString());
364: assertEquals("test=" + htmlEncodedFixture, config
365: .getQueryString());
366: }
367:
368: private HTTPSamplerBase makeContext(String url)
369: throws MalformedURLException {
370: URL u = new URL(url);
371: HTTPSamplerBase context = new HTTPNullSampler();
372: context.setDomain(u.getHost());
373: context.setPath(u.getPath());
374: context.setPort(u.getPort());
375: context.setProtocol(u.getProtocol());
376: context.parseArguments(u.getQuery());
377: return context;
378: }
379:
380: private HTTPSamplerBase makeUrlConfig(String path) {
381: HTTPSamplerBase config = new HTTPNullSampler();
382: config.setDomain("www.apache.org");
383: config.setMethod(HTTPSamplerBase.GET);
384: config.setPath(path);
385: config.setPort(HTTPSamplerBase.DEFAULT_HTTP_PORT);
386: config.setProtocol(HTTPSamplerBase.PROTOCOL_HTTP);
387: return config;
388: }
389: }
|