Configuration branch
This commit is contained in:
+35
-37
@@ -6,36 +6,15 @@ using HtmlAgilityPack;
|
||||
|
||||
namespace Endpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// A website endpoint
|
||||
/// </summary>
|
||||
public class HtmlEndpoint : HttpEndpoint
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the xpath query for the html document
|
||||
/// </summary>
|
||||
/// <value>The xpath query.</value>
|
||||
public string XpathQuery { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the expected query results.
|
||||
/// </summary>
|
||||
/// <value>The expected query results.</value>
|
||||
public string ExpectedXpathResult { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the post.
|
||||
/// </summary>
|
||||
/// <value>The post.</value>
|
||||
public string RequestContent { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the HTML parsed into a standard <see cref="XmlDocument"/>.
|
||||
/// Uses the <see cref="HtmlAgilityPack"/> library for "out of the web" (poorly formatted) html file support.
|
||||
/// Returns the HTML parsed into a standard XmlDocument.
|
||||
/// Uses the HtmlAgilityPack library for "out of the web" (poorly formatted) html file support.
|
||||
/// </summary>
|
||||
/// <param name="html">The HTML</param>
|
||||
/// <returns>An <see cref="XmlDocument"/> from the HTML</returns>
|
||||
private static XmlDocument getHtmlXml(string html)
|
||||
/// <returns>An XmlDocument from the HTML</returns>
|
||||
private static XmlDocument GetHtmlXml(string html)
|
||||
{
|
||||
var htmlDocument = new HtmlDocument();
|
||||
htmlDocument.LoadHtml(html);
|
||||
@@ -58,15 +37,34 @@ namespace Endpoint
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the HTML grabbed from the passed in URL parsed into a standard <see cref="XmlDocument"/>.
|
||||
/// Uses the <see cref="HtmlAgilityPack"/> library for "out of the web" html file support.
|
||||
/// Returns the HTML grabbed from the passed in URL parsed into a standard XmlDocument.
|
||||
/// Uses the HtmlAgilityPack library for "out of the web" html file support.
|
||||
/// </summary>
|
||||
/// <param name="url">The URL.</param>
|
||||
/// <param name="requestContent">The post data.</param>
|
||||
/// <returns>An <see cref="XmlDocument"/> from the HTML</returns>
|
||||
private static XmlDocument getHtmlXml(Uri url, string requestContent)
|
||||
/// <returns>An XmlDocument from the HTML</returns>
|
||||
private static XmlDocument GetHtmlXml(Uri url, string requestContent)
|
||||
{
|
||||
return GetHtmlXml(GetUrlContent(url, "application/x-www-form-urlencoded", requestContent, null));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the HTTP endpoint configuration.
|
||||
/// </summary>
|
||||
/// <value>The HTTP endpoint configuration.</value>
|
||||
public HtmlEndpointConfiguration HtmlEndpointConfiguration
|
||||
{
|
||||
get { return (HtmlEndpointConfiguration)_httpEndpointConfiguration; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HttpEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="config">The HttpEndpointConfiguration.</param>
|
||||
public HtmlEndpoint(HtmlEndpointConfiguration config)
|
||||
: base(config)
|
||||
{
|
||||
return getHtmlXml(GetUrlContent(url, "application/x-www-form-urlencoded", requestContent, null));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -75,29 +73,29 @@ namespace Endpoint
|
||||
/// <returns>Status</returns>
|
||||
public override Status GetStatus()
|
||||
{
|
||||
var baseStatus = base.GetStatus();
|
||||
Status baseStatus = base.GetStatus();
|
||||
if (baseStatus != Status.Up)
|
||||
return baseStatus;
|
||||
|
||||
try
|
||||
{
|
||||
var xml = getHtmlXml(Uri, RequestContent);
|
||||
XmlDocument xml = GetHtmlXml(HttpEndpointConfiguration.Uri, HtmlEndpointConfiguration.RequestContent);
|
||||
|
||||
// xml.Save(@"c:\test.xml");
|
||||
|
||||
var nodes = xml.SelectNodes(XpathQuery);
|
||||
|
||||
XmlNodeList nodes = xml.SelectNodes(HtmlEndpointConfiguration.XpathQuery);
|
||||
|
||||
// verify html has expected value
|
||||
if (nodes == null || nodes.Count == 0)
|
||||
{
|
||||
StatusDescription = "Couldn't find expected value in html";
|
||||
return Status.Error;
|
||||
}
|
||||
var value = nodes[0].Value.Trim();
|
||||
if (value != ExpectedXpathResult)
|
||||
string value = nodes[0].Value.Trim();
|
||||
if (value != HtmlEndpointConfiguration.ExpectedXpathResult)
|
||||
{
|
||||
StatusDescription = String.Format("Result was: '{0}', was expecting '{1}'", value,
|
||||
ExpectedXpathResult);
|
||||
HtmlEndpointConfiguration.ExpectedXpathResult);
|
||||
return Status.Error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user