113 lines
3.0 KiB
C#
113 lines
3.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml;
|
|
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.
|
|
/// </summary>
|
|
/// <param name="html">The HTML</param>
|
|
/// <returns>An <see cref="XmlDocument"/> from the HTML</returns>
|
|
private static XmlDocument getHtmlXml(string html)
|
|
{
|
|
var htmlDocument = new HtmlDocument();
|
|
htmlDocument.LoadHtml(html);
|
|
|
|
var xmlDocument = new XmlDocument();
|
|
using (Stream stream = new MemoryStream())
|
|
{
|
|
XmlWriter xmlTextWriter = new XmlTextWriter(stream, Encoding.UTF8);
|
|
|
|
htmlDocument.Save(xmlTextWriter);
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
using (var sr = new StreamReader(stream))
|
|
xmlDocument.Load(sr);
|
|
|
|
xmlTextWriter.Close();
|
|
}
|
|
return xmlDocument;
|
|
}
|
|
|
|
/// <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.
|
|
/// </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)
|
|
{
|
|
return getHtmlXml(GetUrlContent(url, "application/x-www-form-urlencoded", requestContent, null));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the current status result of the endpoint
|
|
/// </summary>
|
|
/// <returns>Status</returns>
|
|
public override Status GetStatus()
|
|
{
|
|
var baseStatus = base.GetStatus();
|
|
if (baseStatus != Status.Up)
|
|
return baseStatus;
|
|
|
|
try
|
|
{
|
|
var xml = getHtmlXml(Uri, RequestContent);
|
|
|
|
// xml.Save(@"c:\test.xml");
|
|
|
|
var nodes = xml.SelectNodes(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)
|
|
{
|
|
StatusDescription = String.Format("Result was: '{0}', was expecting '{1}'", value,
|
|
ExpectedXpathResult);
|
|
return Status.Error;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
StatusDescription = ex.Message;
|
|
return Status.Error;
|
|
}
|
|
return Status.Up;
|
|
}
|
|
}
|
|
}
|