Added full test suite for RestAPI

This commit is contained in:
stevenh 2012-02-16 12:33:10 +00:00
parent dee2c1a9f4
commit a1d3132138
6 changed files with 1852 additions and 0 deletions

View file

@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Terraria.vsmdi = Terraria.vsmdi Terraria.vsmdi = Terraria.vsmdi
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TShockRestTestPlugin", "TShockRestTestPlugin\TShockRestTestPlugin.csproj", "{F2FEDAFB-58DE-4611-9168-A86112C346C7}"
EndProject
Global Global
GlobalSection(TestCaseManagementSettings) = postSolution GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = Terraria.vsmdi CategoryFile = Terraria.vsmdi
@ -52,6 +54,16 @@ Global
{F3742F51-D7BF-4754-A68A-CD944D2A21FF}.Release|Any CPU.ActiveCfg = Release|Any CPU {F3742F51-D7BF-4754-A68A-CD944D2A21FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3742F51-D7BF-4754-A68A-CD944D2A21FF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {F3742F51-D7BF-4754-A68A-CD944D2A21FF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{F3742F51-D7BF-4754-A68A-CD944D2A21FF}.Release|x86.ActiveCfg = Release|Any CPU {F3742F51-D7BF-4754-A68A-CD944D2A21FF}.Release|x86.ActiveCfg = Release|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Debug|x86.ActiveCfg = Debug|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Release|Any CPU.Build.0 = Release|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{F2FEDAFB-58DE-4611-9168-A86112C346C7}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View file

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ClassLibrary1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Multiplay")]
[assembly: AssemblyProduct("ClassLibrary1")]
[assembly: AssemblyCopyright("Copyright © Multiplay 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c6aed7ee-6282-49a2-8177-b79cad20d6d3")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -0,0 +1,192 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
using Rests;
namespace TshockRestTestPlugin
{
[DisplayName("JSON Status")]
[Description("Checks to see the that the JSON response has the specified status response")]
public class JsonValidateStatus : JsonValidate
{
public override void Validate(object sender, ValidationEventArgs e)
{
if (null != ValidateJson(sender, e))
e.IsValid = true;
}
}
[DisplayName("JSON Regexp Property")]
[Description("Checks to see the that the JSON response contains the specified property and is matches the specified regexp")]
public class JsonValidateRegexpProperty : JsonValidateProperty
{
// The name of the desired JSON property
[DisplayName("Regexp")]
[DefaultValue(true)]
public new bool UseRegularExpression { get { return base.UseRegularExpression; } set { base.UseRegularExpression = value; } }
}
[DisplayName("JSON Error")]
[Description("Checks to see the that the JSON response contains the specified error")]
public class JsonValidateError : JsonValidateProperty
{
// The status of the JSON request
[DisplayName("JSON Status")]
[DefaultValue("400")]
public new string JSonStatus { get { return base.JSonStatus; } set { base.JSonStatus = value; } }
// The name of the desired JSON property
[DisplayName("Property")]
[DefaultValue("error")]
public new string PropertyName { get { return base.PropertyName; } set { base.PropertyName = value; } }
}
[DisplayName("JSON Missing Parameter")]
[Description("Checks to see the that the JSON response indicates a missing or invalid parameter")]
public class JsonValidateMissingParameter : JsonValidateError
{
// The value of the desired JSON property
[DisplayName("Missing Value")]
public new string PropertyValue { get { return base.PropertyValue; } set { base.PropertyValue = String.Format("Missing or empty {0} parameter", value); } }
}
[DisplayName("JSON Invalid Parameter")]
[Description("Checks to see the that the JSON response indicates a missing or invalid parameter")]
public class JsonValidateInvalidParameter : JsonValidateError
{
// The value of the desired JSON property
[DisplayName("Invalid Value")]
public new string PropertyValue { get { return base.PropertyValue; } set { base.PropertyValue = String.Format("Missing or invalid {0} parameter", value); } }
}
[DisplayName("JSON Response")]
[Description("Checks to see the that the JSON response contains the specified message")]
public class JsonValidateResponse : JsonValidateProperty
{
// The name of the desired JSON property
[DisplayName("Response")]
[DefaultValue("response")]
public new string PropertyName { get { return base.PropertyName; } set { base.PropertyName = value; } }
}
[DisplayName("JSON Property")]
[Description("Checks to see the that the JSON response contains the specified property and is set to the specified value")]
public class JsonValidateProperty : JsonValidate
{
// The name of the desired JSON property
[DisplayName("Property")]
public string PropertyName { get; set; }
// The value of the desired JSON property
[DisplayName("Value")]
public string PropertyValue { get; set; }
// Is the value a regexp of the desired JSON property
[DisplayName("Regexp")]
[DefaultValue(false)]
public bool UseRegularExpression { get; set; }
public override void Validate(object sender, ValidationEventArgs e)
{
RestObject response = ValidateJson(sender, e);
if (null == response)
return;
if (null == response[PropertyName])
{
e.Message = String.Format("{0} Not Found", PropertyName);
e.IsValid = false;
return;
}
if (UseRegularExpression)
{
var re = new Regex(PropertyValue);
if (!re.IsMatch((string)response[PropertyName]))
{
e.Message = String.Format("{0} => '{1}' !~ '{2}'", PropertyName, response[PropertyName], PropertyValue);
e.IsValid = false;
return;
}
}
else
{
if (PropertyValue != (string)response[PropertyName])
{
e.Message = String.Format("{0} => '{1}' != '{2}'", PropertyName, response[PropertyName], PropertyValue);
e.IsValid = false;
return;
}
}
e.IsValid = true;
//e.WebTest.Context.Add(ContextParameterName, propertyValue);
}
}
[DisplayName("JSON Has Properties")]
[Description("Checks to see the that the JSON response contains the specified properties (comma seperated)")]
public class JsonHasProperties : JsonValidate
{
// The name of the desired JSON properties to check
[DisplayName("Properties")]
[Description("A comma seperated list of property names to check exist")]
public string PropertyNames { get; set; }
//---------------------------------------------------------------------
public override void Validate(object sender, ValidationEventArgs e)
{
RestObject response = ValidateJson(sender, e);
if (null == response)
return;
foreach (var p in PropertyNames.Split(','))
{
if (null == response[p])
{
e.Message = String.Format("'{0}' Not Found", p);
e.IsValid = false;
return;
}
}
e.IsValid = true;
//e.WebTest.Context.Add(ContextParameterName, propertyValue);
}
}
public abstract class JsonValidate : ValidationRule
{
// The status of the JSON request
[DisplayName("JSON Status")]
[DefaultValue("200")]
public string JSonStatus { get; set; }
public RestObject ValidateJson(object sender, ValidationEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Response.BodyString))
{
e.IsValid = false;
e.Message = String.Format("Empty or null response {0}", e.Response.StatusCode);
return null;
}
JavaScriptSerializer serialiser = new JavaScriptSerializer();
//dynamic data = serialiser.Deserialize<dynamic>(e.Response.BodyString);
RestObject response = serialiser.Deserialize<RestObject>(e.Response.BodyString);
if (JSonStatus != response.Status)
{
e.IsValid = false;
e.Message = String.Format("Response Status '{0}' not '{1}'", response.Status, JSonStatus);
return null;
}
return response;
}
}
}

View file

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{F2FEDAFB-58DE-4611-9168-A86112C346C7}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TshockRestTestPlugin</RootNamespace>
<AssemblyName>TshockRestTestPlugin</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="TShockRestTestPlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TShockAPI\TShockAPI.csproj">
<Project>{49606449-072B-4CF5-8088-AA49DA586694}</Project>
<Name>TShockAPI</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

File diff suppressed because it is too large Load diff

View file

@ -49,6 +49,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.QualityTools.WebTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<Reference Include="Mono.Data.Sqlite"> <Reference Include="Mono.Data.Sqlite">
<HintPath>..\SqlBins\Mono.Data.Sqlite.dll</HintPath> <HintPath>..\SqlBins\Mono.Data.Sqlite.dll</HintPath>
</Reference> </Reference>
@ -87,6 +88,10 @@
<Project>{49606449-072B-4CF5-8088-AA49DA586694}</Project> <Project>{49606449-072B-4CF5-8088-AA49DA586694}</Project>
<Name>TShockAPI</Name> <Name>TShockAPI</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\TShockRestTestPlugin\TShockRestTestPlugin.csproj">
<Project>{F2FEDAFB-58DE-4611-9168-A86112C346C7}</Project>
<Name>TShockRestTestPlugin</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="BanManagerTest.orderedtest"> <None Include="BanManagerTest.orderedtest">
@ -102,6 +107,9 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="UnitTests.licenseheader" /> <None Include="UnitTests.licenseheader" />
<None Include="RestApiTests.webtest">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0"> <BootstrapperPackage Include=".NETFramework,Version=v4.0">