Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.junit.protocol.http.parser » [javadoc | source]
    1   // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/junit/protocol/http/parser/HtmlParserTester.java,v 1.14 2005/07/12 20:51:01 mstover1 Exp $
    2   /*
    3    * Copyright 2001-2004 The Apache Software Foundation.
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    * 
   17    */
   18   
   19   package org.apache.jmeter.junit.protocol.http.parser;
   20   
   21   import java.net.MalformedURLException;
   22   import java.net.URL;
   23   
   24   import org.apache.jmeter.junit.JMeterTestCase;
   25   import org.apache.jmeter.protocol.http.modifier.AnchorModifier;
   26   import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
   27   import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
   28   import org.apache.jmeter.protocol.http.sampler.HTTPNullSampler;
   29   import org.apache.jmeter.threads.JMeterContext;
   30   import org.apache.jmeter.threads.JMeterContextService;
   31   
   32   /**
   33    * Created June 14, 2001
   34    * 
   35    * @version $Revision: 1.14 $ Last updated: $Date: 2005/07/12 20:51:01 $
   36    */
   37   public class HtmlParserTester extends JMeterTestCase {
   38   	AnchorModifier parser = new AnchorModifier();
   39   
   40   	/**
   41   	 * Constructor for the HtmlParserTester object.
   42   	 */
   43   	public HtmlParserTester(String name) {
   44   		super(name);
   45   	}
   46   
   47   	private JMeterContext jmctx = null;
   48   
   49   	public void setUp() {
   50   		jmctx = JMeterContextService.getContext();
   51   		parser.setThreadContext(jmctx);
   52   	}
   53   
   54   	/**
   55   	 * A unit test for JUnit.
   56   	 */
   57   	public void testSimpleParse() throws Exception {
   58   		HTTPSamplerBase config = makeUrlConfig(".*/index\\.html");
   59   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
   60   		String responseText = "<html><head><title>Test page</title></head><body>"
   61   				+ "<a href=\"index.html\">Goto index page</a></body></html>";
   62   		HTTPSampleResult result = new HTTPSampleResult();
   63   		jmctx.setCurrentSampler(context);
   64   		jmctx.setCurrentSampler(config);
   65   		result.setResponseData(responseText.getBytes());
   66   		result.setSampleLabel(context.toString());
   67   		result.setSamplerData(context.toString());
   68   		result.setURL(context.getUrl());
   69   		jmctx.setPreviousResult(result);
   70   		parser.process();
   71   		assertEquals("http://www.apache.org/subdir/index.html", config.getUrl().toString());
   72   	}
   73   
   74   	public void testSimpleParse2() throws Exception {
   75   		HTTPSamplerBase config = makeUrlConfig("/index\\.html");
   76   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
   77   		String responseText = "<html><head><title>Test page</title></head><body>"
   78   				+ "<a href=\"/index.html\">Goto index page</a>" + "hfdfjiudfjdfjkjfkdjf"
   79   				+ "<b>bold text</b><a href=lowerdir/index.html>lower</a>" + "</body></html>";
   80   		HTTPSampleResult result = new HTTPSampleResult();
   81   		result.setResponseData(responseText.getBytes());
   82   		result.setSampleLabel(context.toString());
   83   		result.setURL(context.getUrl());
   84   		jmctx.setCurrentSampler(context);
   85   		jmctx.setCurrentSampler(config);
   86   		jmctx.setPreviousResult(result);
   87   		parser.process();
   88   		String newUrl = config.getUrl().toString();
   89   		assertTrue("http://www.apache.org/index.html".equals(newUrl)
   90   				|| "http://www.apache.org/subdir/lowerdir/index.html".equals(newUrl));
   91   	}
   92   
   93   	public void testSimpleParse3() throws Exception {
   94   		HTTPSamplerBase config = makeUrlConfig(".*index.*");
   95   		config.getArguments().addArgument("param1", "value1");
   96   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
   97   		String responseText = "<html><head><title>Test page</title></head><body>"
   98   				+ "<a href=\"/home/index.html?param1=value1\">" + "Goto index page</a></body></html>";
   99   		HTTPSampleResult result = new HTTPSampleResult();
  100   		result.setResponseData(responseText.getBytes());
  101   		result.setSampleLabel(context.toString());
  102   		result.setURL(context.getUrl());
  103   		jmctx.setCurrentSampler(context);
  104   		jmctx.setCurrentSampler(config);
  105   		jmctx.setPreviousResult(result);
  106   		parser.process();
  107   		String newUrl = config.getUrl().toString();
  108   		assertEquals("http://www.apache.org/home/index.html?param1=value1", newUrl);
  109   	}
  110   
  111   	public void testSimpleParse4() throws Exception {
  112   		HTTPSamplerBase config = makeUrlConfig("/subdir/index\\..*");
  113   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
  114   		String responseText = "<html><head><title>Test page</title></head><body>"
  115   				+ "<A HREF=\"index.html\">Goto index page</A></body></html>";
  116   		HTTPSampleResult result = new HTTPSampleResult();
  117   		result.setResponseData(responseText.getBytes());
  118   		result.setSampleLabel(context.toString());
  119   		result.setURL(context.getUrl());
  120   		jmctx.setCurrentSampler(context);
  121   		jmctx.setCurrentSampler(config);
  122   		jmctx.setPreviousResult(result);
  123   		parser.process();
  124   		String newUrl = config.getUrl().toString();
  125   		assertEquals("http://www.apache.org/subdir/index.html", newUrl);
  126   	}
  127   
  128   	public void testSimpleParse5() throws Exception {
  129   		HTTPSamplerBase config = makeUrlConfig("/subdir/index\\.h.*");
  130   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/one/previous.html");
  131   		String responseText = "<html><head><title>Test page</title></head><body>"
  132   				+ "<a href=\"../index.html\">Goto index page</a></body></html>";
  133   		HTTPSampleResult result = new HTTPSampleResult();
  134   		result.setResponseData(responseText.getBytes());
  135   		result.setSampleLabel(context.toString());
  136   		result.setURL(context.getUrl());
  137   		jmctx.setCurrentSampler(context);
  138   		jmctx.setCurrentSampler(config);
  139   		jmctx.setPreviousResult(result);
  140   		parser.process();
  141   		String newUrl = config.getUrl().toString();
  142   		assertEquals("http://www.apache.org/subdir/index.html", newUrl);
  143   	}
  144   
  145   	public void testFailSimpleParse1() throws Exception {
  146   		HTTPSamplerBase config = makeUrlConfig(".*index.*?param2=.+1");
  147   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
  148   		String responseText = "<html><head><title>Test page</title></head><body>"
  149   				+ "<a href=\"/home/index.html?param1=value1\">" + "Goto index page</a></body></html>";
  150   		HTTPSampleResult result = new HTTPSampleResult();
  151   		String newUrl = config.getUrl().toString();
  152   		result.setResponseData(responseText.getBytes());
  153   		result.setSampleLabel(context.toString());
  154   		result.setURL(context.getUrl());
  155   		jmctx.setCurrentSampler(context);
  156   		jmctx.setCurrentSampler(config);
  157   		jmctx.setPreviousResult(result);
  158   		parser.process();
  159   		assertEquals(newUrl, config.getUrl().toString());
  160   	}
  161   
  162   	public void testFailSimpleParse3() throws Exception {
  163   		HTTPSamplerBase config = makeUrlConfig("/home/index.html");
  164   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
  165   		String responseText = "<html><head><title>Test page</title></head><body>"
  166   				+ "<a href=\"/home/index.html?param1=value1\">" + "Goto index page</a></body></html>";
  167   		HTTPSampleResult result = new HTTPSampleResult();
  168   		String newUrl = config.getUrl().toString();
  169   		result.setResponseData(responseText.getBytes());
  170   		result.setSampleLabel(context.toString());
  171   		result.setURL(context.getUrl());
  172   		jmctx.setCurrentSampler(context);
  173   		jmctx.setCurrentSampler(config);
  174   		jmctx.setPreviousResult(result);
  175   		parser.process();
  176   		assertEquals(newUrl + "?param1=value1", config.getUrl().toString());
  177   	}
  178   
  179   	public void testFailSimpleParse2() throws Exception {
  180   		HTTPSamplerBase config = makeUrlConfig(".*login\\.html");
  181   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
  182   		String responseText = "<html><head><title>Test page</title></head><body>"
  183   				+ "<a href=\"/home/index.html?param1=value1\">" + "Goto index page</a></body></html>";
  184   		HTTPSampleResult result = new HTTPSampleResult();
  185   		result.setResponseData(responseText.getBytes());
  186   		result.setSampleLabel(context.toString());
  187   		result.setURL(context.getUrl());
  188   		jmctx.setCurrentSampler(context);
  189   		jmctx.setPreviousResult(result);
  190   		parser.process();
  191   		String newUrl = config.getUrl().toString();
  192   		assertTrue(!"http://www.apache.org/home/index.html?param1=value1".equals(newUrl));
  193   		assertEquals(config.getUrl().toString(), newUrl);
  194   	}
  195   
  196   	/**
  197   	 * A unit test for JUnit.
  198   	 */
  199   	public void testSimpleFormParse() throws Exception {
  200   		HTTPSamplerBase config = makeUrlConfig(".*index.html");
  201   		config.addArgument("test", "g.*");
  202   		config.setMethod(HTTPSamplerBase.POST);
  203   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
  204   		String responseText = "<html><head><title>Test page</title></head><body>"
  205   				+ "<form action=\"index.html\" method=\"POST\">" + "<input type=\"checkbox\" name=\"test\""
  206   				+ " value=\"goto\">Goto index page</form></body></html>";
  207   		HTTPSampleResult result = new HTTPSampleResult();
  208   		result.setResponseData(responseText.getBytes());
  209   		result.setSampleLabel(context.toString());
  210   		result.setURL(context.getUrl());
  211   		jmctx.setCurrentSampler(context);
  212   		jmctx.setCurrentSampler(config);
  213   		jmctx.setPreviousResult(result);
  214   		parser.process();
  215   		assertEquals("http://www.apache.org/subdir/index.html", config.getUrl().toString());
  216   		assertEquals("test=goto", config.getQueryString());
  217   	}
  218   
  219   	/**
  220   	 * A unit test for JUnit.
  221   	 */
  222   	public void testBadCharParse() throws Exception {
  223   		HTTPSamplerBase config = makeUrlConfig(".*index.html");
  224   		config.addArgument("te$st", "g.*");
  225   		config.setMethod(HTTPSamplerBase.POST);
  226   		HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
  227   		String responseText = "<html><head><title>Test page</title></head><body>"
  228   				+ "<form action=\"index.html\" method=\"POST\">" + "<input type=\"checkbox\" name=\"te$st\""
  229   				+ " value=\"goto\">Goto index page</form></body></html>";
  230   		HTTPSampleResult result = new HTTPSampleResult();
  231   		result.setResponseData(responseText.getBytes());
  232   		result.setSampleLabel(context.toString());
  233   		result.setURL(context.getUrl());
  234   		jmctx.setCurrentSampler(context);
  235   		jmctx.setCurrentSampler(config);
  236   		jmctx.setPreviousResult(result);
  237   		parser.process();
  238   		assertEquals("http://www.apache.org/subdir/index.html", config.getUrl().toString());
  239   		assertEquals("te%24st=goto", config.getQueryString());
  240   	}
  241   
  242   	private HTTPSamplerBase makeContext(String url) throws MalformedURLException {
  243   		URL u = new URL(url);
  244   		HTTPSamplerBase context = new HTTPNullSampler();
  245   		context.setDomain(u.getHost());
  246   		context.setPath(u.getPath());
  247   		context.setPort(u.getPort());
  248   		context.setProtocol(u.getProtocol());
  249   		context.parseArguments(u.getQuery());
  250   		return context;
  251   	}
  252   
  253   	private HTTPSamplerBase makeUrlConfig(String path) {
  254   		HTTPSamplerBase config = new HTTPNullSampler();
  255   		config.setDomain("www.apache.org");
  256   		config.setMethod(HTTPSamplerBase.GET);
  257   		config.setPath(path);
  258   		config.setPort(80);
  259   		config.setProtocol("http");
  260   		return config;
  261   	}
  262   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.junit.protocol.http.parser » [javadoc | source]