From a1d3132138f277d4ff850099c2d504acb6b5cde0 Mon Sep 17 00:00:00 2001 From: stevenh Date: Thu, 16 Feb 2012 12:33:10 +0000 Subject: [PATCH] Added full test suite for RestAPI --- TShock.sln | 12 + .../Properties/AssemblyInfo.cs | 36 + TShockRestTestPlugin/TShockRestTestPlugin.cs | 192 ++ .../TShockRestTestPlugin.csproj | 62 + UnitTests/RestApiTests.webtest | 1542 +++++++++++++++++ UnitTests/UnitTests.csproj | 8 + 6 files changed, 1852 insertions(+) create mode 100644 TShockRestTestPlugin/Properties/AssemblyInfo.cs create mode 100644 TShockRestTestPlugin/TShockRestTestPlugin.cs create mode 100644 TShockRestTestPlugin/TShockRestTestPlugin.csproj create mode 100644 UnitTests/RestApiTests.webtest diff --git a/TShock.sln b/TShock.sln index 85d6a0f9..db301cae 100644 --- a/TShock.sln +++ b/TShock.sln @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Terraria.vsmdi = Terraria.vsmdi EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TShockRestTestPlugin", "TShockRestTestPlugin\TShockRestTestPlugin.csproj", "{F2FEDAFB-58DE-4611-9168-A86112C346C7}" +EndProject Global GlobalSection(TestCaseManagementSettings) = postSolution 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|Mixed Platforms.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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TShockRestTestPlugin/Properties/AssemblyInfo.cs b/TShockRestTestPlugin/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..17cb9a46 --- /dev/null +++ b/TShockRestTestPlugin/Properties/AssemblyInfo.cs @@ -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")] diff --git a/TShockRestTestPlugin/TShockRestTestPlugin.cs b/TShockRestTestPlugin/TShockRestTestPlugin.cs new file mode 100644 index 00000000..403b6f8f --- /dev/null +++ b/TShockRestTestPlugin/TShockRestTestPlugin.cs @@ -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(e.Response.BodyString); + RestObject response = serialiser.Deserialize(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; + } + } +} \ No newline at end of file diff --git a/TShockRestTestPlugin/TShockRestTestPlugin.csproj b/TShockRestTestPlugin/TShockRestTestPlugin.csproj new file mode 100644 index 00000000..bf2dd997 --- /dev/null +++ b/TShockRestTestPlugin/TShockRestTestPlugin.csproj @@ -0,0 +1,62 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {F2FEDAFB-58DE-4611-9168-A86112C346C7} + Library + Properties + TshockRestTestPlugin + TshockRestTestPlugin + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + {49606449-072B-4CF5-8088-AA49DA586694} + TShockAPI + + + + + \ No newline at end of file diff --git a/UnitTests/RestApiTests.webtest b/UnitTests/RestApiTests.webtest new file mode 100644 index 00000000..84057748 --- /dev/null +++ b/UnitTests/RestApiTests.webtest @@ -0,0 +1,1542 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index d9b9c7f9..923856ac 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -49,6 +49,7 @@ + ..\SqlBins\Mono.Data.Sqlite.dll @@ -87,6 +88,10 @@ {49606449-072B-4CF5-8088-AA49DA586694} TShockAPI + + {F2FEDAFB-58DE-4611-9168-A86112C346C7} + TShockRestTestPlugin + @@ -102,6 +107,9 @@ Always + + Always +