/*********************************************************/
using System;
using System.Text;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.VisualStudio.QualityTools.WebTestFramework;
using Microsoft.VisualStudio.QualityTools.WebTestFramework.Rules;
/**********************************************************************************************/
// Handle validation of response header values
/**********************************************************************************************/
namespace HeaderValidationRule
{
/**********************************************************************************************/
// Inherits validation rule
/**********************************************************************************************/
public class ValidateHeader : ValidationRule
{
// Header name
private string m_headerStringParameterName;
public string HeaderStringParameterName
{
set
{
m_headerStringParameterName = value;
}
get
{
return m_headerStringParameterName;
}
}
// Header value
private string m_headerStringParameterExpectedValue;
public string HeaderStringParameterExpectedValue
{
get
{
return m_headerStringParameterExpectedValue;
}
set
{
m_headerStringParameterExpectedValue = value;
}
}
// Fail if not found?
private bool m_isRequired = true;
[DisplayName("Is Required")]
public bool IsRequired
{
get
{
return m_isRequired;
}
set
{
m_isRequired = value;
}
}
/**********************************************************************************************/
// Add logic to handle header validation
/**********************************************************************************************/
public override void Validate(object sender, ValidationEventArgs e)
{
e.IsValid = false;
e.Message = "Not found";
for(int i = 0; i < e.Response.Headers.Keys.Count; ++i)
{
if (e.Response.Headers.GetKey(i) == HeaderStringParameterName)
{
string strVal = e.Response.Headers.Get(HeaderStringParameterName);
if (strVal.Contains(HeaderStringParameterExpectedValue))
{
e.IsValid = true;
e.Message = "Found";
break;
}
}
}
}
}
}