use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.
the class SpiderHtmlFormParserUnitTest method shouldUseBaseHtmlUrlWhenParsingPostForm.
@Test
public void shouldUseBaseHtmlUrlWhenParsingPostForm() {
// Given
SpiderHtmlFormParser htmlParser = createSpiderHtmlFormParser();
TestSpiderParserListener listener = createTestSpiderParserListener();
htmlParser.addSpiderParserListener(listener);
HttpMessage msg = createMessageWith("POST", "FormWithHtmlBase.html", "search", "http://base.example.com/");
Source source = createSource(msg);
// When
boolean completelyParsed = htmlParser.parseResource(msg, source, BASE_DEPTH);
// Then
assertThat(completelyParsed, is(equalTo(false)));
assertThat(listener.getNumberOfResourcesFound(), is(equalTo(1)));
assertThat(listener.getResourcesFound(), contains(postResource(msg, 1, "http://base.example.com/search", "q=Search&submit=Submit")));
}
use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.
the class SpiderHtmlFormParserUnitTest method shouldParseGetFormWithMultipleSubmitFields.
@Test
public void shouldParseGetFormWithMultipleSubmitFields() {
// Given
SpiderHtmlFormParser htmlParser = createSpiderHtmlFormParser();
TestSpiderParserListener listener = createTestSpiderParserListener();
htmlParser.addSpiderParserListener(listener);
HttpMessage messageHtmlResponse = createMessageWith("GET", "FormMultipleSubmitFields.html");
Source source = createSource(messageHtmlResponse);
// When
boolean completelyParsed = htmlParser.parseResource(messageHtmlResponse, source, BASE_DEPTH);
// Then
assertThat(completelyParsed, is(equalTo(false)));
assertThat(listener.getNumberOfUrlsFound(), is(equalTo(5)));
assertThat(listener.getUrlsFound(), contains("http://example.org/?field1=Text+1&field2=Text+2&submit1=Submit+1", "http://example.org/?field1=Text+1&field2=Text+2&submit2=Submit+2", "http://example.org/?field1=Text+1&field2=Text+2&submit3=Submit+3", "http://example.org/?field1=Text+1&field2=Text+2&submit=Submit+4", "http://example.org/?field1=Text+1&field2=Text+2&submit=Submit+5"));
}
use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.
the class SpiderTextParserUnitTest method shouldFailToParseAnUndefinedMessage.
@Test(expected = NullPointerException.class)
public void shouldFailToParseAnUndefinedMessage() {
// Given
HttpMessage undefinedMessage = null;
SpiderTextParser spiderParser = new SpiderTextParser();
Source source = createSource(createMessageWith(EMPTY_BODY));
// When
spiderParser.parseResource(undefinedMessage, source, BASE_DEPTH);
// Then = NullPointerException
}
use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.
the class SpiderHtmlParser method parseResource.
/**
* @throws NullPointerException if {@code message} is null.
*/
@Override
public boolean parseResource(HttpMessage message, Source source, int depth) {
// Prepare the source, if not provided
if (source == null) {
source = new Source(message.getResponseBody().toString());
}
// Get the context (base url)
String baseURL = message.getRequestHeader().getURI().toString();
// Try to see if there's any BASE tag that could change the base URL
Element base = source.getFirstElement(HTMLElementName.BASE);
if (base != null) {
if (log.isDebugEnabled()) {
log.debug("Base tag was found in HTML: " + base.getDebugInfo());
}
String href = base.getAttributeValue("href");
if (href != null && !href.isEmpty()) {
baseURL = URLCanonicalizer.getCanonicalURL(href, baseURL);
}
}
// Parse the source
parseSource(message, source, depth, baseURL);
// Parse the comments
if (params.isParseComments()) {
List<StartTag> comments = source.getAllStartTags(StartTagType.COMMENT);
for (StartTag comment : comments) {
Source s = new Source(comment.getTagContent());
if (!parseSource(message, s, depth, baseURL)) {
Matcher matcher = PLAIN_COMMENTS_URL_PATTERN.matcher(s.toString());
while (matcher.find()) {
processURL(message, depth, matcher.group(), baseURL);
}
}
}
}
return false;
}
use of net.htmlparser.jericho.Source in project zaproxy by zaproxy.
the class SpiderHtmlFormParserUnitTest method shouldNeverConsiderCompletelyParsed.
@Test
public void shouldNeverConsiderCompletelyParsed() {
// Given
SpiderHtmlFormParser htmlParser = createSpiderHtmlFormParser();
HttpMessage messageHtmlResponse = createMessageWith("NoForms.html");
Source source = createSource(messageHtmlResponse);
// When
boolean completelyParsed = htmlParser.parseResource(messageHtmlResponse, source, BASE_DEPTH);
// Then
assertThat(completelyParsed, is(equalTo(false)));
}
Aggregations