package org.apache.jmeter.junit.protocol.http.parser;
import java.net.*;
import junit.framework.TestCase;
import org.apache.jmeter.protocol.http.config.UrlConfig;
import org.apache.jmeter.protocol.http.modifier.AnchorModifier;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.samplers.*;
/************************************************************
* Title: Description: Copyright: Copyright (c) 2001 Company:
*
*@author
*@created June 14, 2001
*@version 1.0
***********************************************************/
public class HtmlParserTester extends TestCase
{
AnchorModifier parser = new AnchorModifier();
/************************************************************
* Constructor for the HtmlParserTester object
*
*@param name Description of Parameter
***********************************************************/
public HtmlParserTester(String name)
{
super(name);
}
/************************************************************
* A unit test for JUnit
*
*@exception Exception Description of Exception
***********************************************************/
public void testSimpleParse() throws Exception
{
UrlConfig config = makeUrlConfig(".*/index\\.html");
URL context = new URL("http://www.apache.org/subdir/previous.html");
Entry entry = new Entry();
entry.addConfigElement(config);
String responseText = "<html><head><title>Test page</title></head><body>" +
"<a href=\"index.html\">Goto index page</a></body></html>";
SampleResult result = new SampleResult();
result.putValue(SampleResult.TEXT_RESPONSE,responseText.getBytes());
result.putValue(HTTPSampler.URL,context);
parser.modifyEntry(entry, result);
assertEquals("http://www.apache.org:80/subdir/index.html",
((UrlConfig)entry.getConfigElement(UrlConfig.class)).getUrl().toString());
}
public void testSimpleParse2() throws Exception
{
UrlConfig config = makeUrlConfig("/index\\.html");
URL context = new URL("http://www.apache.org/subdir/previous.html");
String responseText = "<html><head><title>Test page</title></head><body>" +
"<a href=\"/index.html\">Goto index page</a>hfdfjiudfjdfjkjfkdjf"+
"<b>bold text</b><a href=lowerdir/index.html>lower</a></body></html>";
Entry entry = new Entry();
entry.addConfigElement(config);
SampleResult result = new SampleResult();
result.putValue(SampleResult.TEXT_RESPONSE,responseText.getBytes());
result.putValue(HTTPSampler.URL,context);
parser.modifyEntry(entry, result);
String newUrl = ((UrlConfig)entry.getConfigElement(UrlConfig.class)).getUrl().toString();
assertTrue("http://www.apache.org:80/index.html".equals(newUrl)
|| "http://www.apache.org:80/subdir/lowerdir/index.html".equals(newUrl));
}
public void testSimpleParse3() throws Exception
{
UrlConfig config = makeUrlConfig(".*index.*");
config.addArgument("param1","value1");
URL context = new URL("http://www.apache.org/subdir/previous.html");
String responseText = "<html><head><title>Test page</title></head><body>" +
"<a href=\"/home/index.html?param1=value1\">Goto index page</a></body></html>";
Entry entry = new Entry();
entry.addConfigElement(config);
SampleResult result = new SampleResult();
result.putValue(SampleResult.TEXT_RESPONSE,responseText.getBytes());
result.putValue(HTTPSampler.URL,context);
parser.modifyEntry(entry, result);
String newUrl = ((UrlConfig)entry.getConfigElement(UrlConfig.class)).getUrl().toString();
assertEquals("http://www.apache.org:80/home/index.html?param1=value1",newUrl);
assertTrue(!config.getUrl().toString().equals(newUrl));
}
public void testSimpleParse4() throws Exception
{
UrlConfig config = makeUrlConfig("/subdir/index\\..*");
URL context = new URL("http://www.apache.org/subdir/previous.html");
String responseText = "<html><head><title>Test page</title></head><body>" +
"<A HREF=\"index.html\">Goto index page</A></body></html>";
Entry entry = new Entry();
entry.addConfigElement(config);
SampleResult result = new SampleResult();
result.putValue(SampleResult.TEXT_RESPONSE,responseText.getBytes());
result.putValue(HTTPSampler.URL,context);
parser.modifyEntry(entry, result);
String newUrl = ((UrlConfig)entry.getConfigElement(UrlConfig.class)).getUrl().toString();
assertEquals("http://www.apache.org:80/subdir/index.html",newUrl);
assertTrue(!config.getUrl().toString().equals(newUrl));
}
public void testSimpleParse5() throws Exception
{
UrlConfig config = makeUrlConfig("/subdir/index\\.h.*");
URL context = new URL("http://www.apache.org/subdir/one/previous.html");
String responseText = "<html><head><title>Test page</title></head><body>" +
"<a href=\"../index.html\">Goto index page</a></body></html>";
Entry entry = new Entry();
entry.addConfigElement(config);
SampleResult result = new SampleResult();
result.putValue(SampleResult.TEXT_RESPONSE,responseText.getBytes());
result.putValue(HTTPSampler.URL,context);
parser.modifyEntry(entry, result);
String newUrl = ((UrlConfig)entry.getConfigElement(UrlConfig.class)).getUrl().toString();
assertEquals("http://www.apache.org:80/subdir/index.html",newUrl);
assertTrue(!config.getUrl().toString().equals(newUrl));
}
public void testFailSimpleParse1() throws Exception
{
UrlConfig config = makeUrlConfig(".*index.*param2=.+1");
URL context = new URL("http://www.apache.org/subdir/previous.html");
String responseText = "<html><head><title>Test page</title></head><body>" +
"<a href=\"/home/index.html?param1=value1\">Goto index page</a></body></html>";
Entry entry = new Entry();
entry.addConfigElement(config);
SampleResult result = new SampleResult();
result.putValue(SampleResult.TEXT_RESPONSE,responseText.getBytes());
result.putValue(HTTPSampler.URL,context);
parser.modifyEntry(entry, result);
String newUrl = ((UrlConfig)entry.getConfigElement(UrlConfig.class)).getUrl().toString();
assertEquals(config.getUrl().toString(),newUrl);
}
public void testFailSimpleParse2() throws Exception
{
UrlConfig config = makeUrlConfig(".*login\\.html");
URL context = new URL("http://www.apache.org/subdir/previous.html");
String responseText = "<html><head><title>Test page</title></head><body>" +
"<a href=\"/home/index.html?param1=value1\">Goto index page</a></body></html>";
Entry entry = new Entry();
entry.addConfigElement(config);
SampleResult result = new SampleResult();
result.putValue(SampleResult.TEXT_RESPONSE,responseText.getBytes());
result.putValue(HTTPSampler.URL,context);
parser.modifyEntry(entry, result);
String newUrl = ((UrlConfig)entry.getConfigElement(UrlConfig.class)).getUrl().toString();
this.assertTrue(!"http://www.apache.org:80/home/index.html?param1=value1".equals(newUrl));
assertEquals(config.getUrl().toString(),newUrl);
}
/************************************************************
* A unit test for JUnit
*
*@exception Exception Description of Exception
***********************************************************/
public void testSimpleFormParse() throws Exception
{
UrlConfig config = makeUrlConfig(".*index.html");
config.addArgument("test","g.*");
config.setMethod(UrlConfig.POST);
URL context = new URL("http://www.apache.org/subdir/previous.html");
Entry entry = new Entry();
entry.addConfigElement(config);
String responseText = "<html><head><title>Test page</title></head><body>" +
"<form action=\"index.html\" method=\"POST\"><input type=\"checkbox\" name=\"test\""+
" value=\"goto\">Goto index page</form></body></html>";
SampleResult result = new SampleResult();
result.putValue(SampleResult.TEXT_RESPONSE,responseText.getBytes());
result.putValue(HTTPSampler.URL,context);
parser.modifyEntry(entry, result);
assertEquals("http://www.apache.org:80/subdir/index.html",
((UrlConfig)entry.getConfigElement(UrlConfig.class)).getUrl().toString());
assertEquals("test=goto",((UrlConfig)entry.getConfigElement(UrlConfig.class)).getQueryString());
}
private UrlConfig makeUrlConfig(String path)
{
UrlConfig config = new UrlConfig();
config.setDomain("www.apache.org");
config.setMethod(config.GET);
config.setPath(path);
config.setPort(80);
config.setProtocol("http");
return config;
}
}