diff --git a/README.md b/README.md old mode 100644 new mode 100755 index e95e4a4..e031b9b --- a/README.md +++ b/README.md @@ -211,6 +211,19 @@ Utilize these tools to validate your JSON schema ## More Examples +### Add new response type parser +RestAssured can already parse JSON and XML response bodies, however you are able to overwrite +these with your own parsing functions or add new parsing functionality. +```C# +//Create a method to parse your input +public static dynamic CsvToJson(string input) +{ + // Code to parse csv string to JObject +} +//Then inside main method you add this parser +RestAssured.AddParser("csv",CsvToJson); +``` + ### Breaking up a call chain ```C# //Create a new test suite diff --git a/src/RA.Tests/MockResponseContextWithJson.cs b/src/RA.Tests/MockResponseContextWithJson.cs old mode 100644 new mode 100755 diff --git a/src/RA.Tests/MockResponseWithCustomParse.cs b/src/RA.Tests/MockResponseWithCustomParse.cs new file mode 100755 index 0000000..1f649a8 --- /dev/null +++ b/src/RA.Tests/MockResponseWithCustomParse.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Net; +using Newtonsoft.Json.Linq; +using NUnit.Framework; + +namespace RA.Tests +{ + [TestFixture] + public class MockResponseWithCustomParse + { + private readonly ResponseContext _responseWithCsv; + private static readonly int _mockElapsedMs = 500; + private readonly TimeSpan _mockElapsedTimespan = new TimeSpan(0, 0, 0, 0, _mockElapsedMs); + + public static dynamic CsvToJson(string input) + { + string[] lines = input.Split("\n"); + string[] keys = lines[0].Split(","); + JArray jarr = new JArray(); + for(int i=1;i> + { + { + "Content-Type", + new List {"text/csv"} + } + }; + + var emptyHeader = new Dictionary>(); + + var loadResults = new List {new LoadResponse(200, 78978078)}; + + ResponseContext.AddParser("csv", CsvToJson); + + _responseWithCsv = new ResponseContext(HttpStatusCode.OK, responseObjectContent, header, + _mockElapsedTimespan, loadResults); + } + + [Test] + public void CustomParserShouldPass() + { + _responseWithCsv + .TestHeader("testHeader","Content-type", x => x.Contains("csv")) + .TestBody("first item has AL", x => x[0].key == "AL") + .TestBody("first item has Alabama", x => x[0].value == "Alabama") + .TestBody("second item has AK", x => x[1].key == "AK") + .AssertAll(); + } + } +} \ No newline at end of file diff --git a/src/RA/ResponseContext.cs b/src/RA/ResponseContext.cs old mode 100644 new mode 100755 index 183743b..bf3bf7c --- a/src/RA/ResponseContext.cs +++ b/src/RA/ResponseContext.cs @@ -220,54 +220,81 @@ private void Initialize() ParseLoad(); } - private void Parse() + /// + /// Holds response parser functions + /// + private static Dictionary> parsers = new Dictionary>() { - var contentType = ContentType(); - - if (contentType.Contains("json")) - { - if (!string.IsNullOrEmpty(_content)) + { "json", delegate(string instr) { try { - _parsedContent = JObject.Parse(_content); - return; + return JObject.Parse(instr); } catch { + try + { + return JArray.Parse(instr); + } + catch(Exception e) + { + throw e; + } } - + } + }, + { "xml", delegate(string instr) + { try { - _parsedContent = JArray.Parse(_content); - return; + return XDocument.Parse(instr); } - catch + catch(Exception e) { + throw e; } } - else - { - return; - } - } - else if (contentType.Contains("xml")) + }, + }; + + /// + /// Adds Parser to allow unsupporter response types to be parsed + /// + /// Content-Type to use provided function to parse + /// Function to parse type + public static void AddParser(string type, Func func) + { + if (parsers.ContainsKey(type)) + parsers[type] = func; + else + parsers.Add(type, func); + } + + private void Parse() + { + if (string.IsNullOrEmpty(_content)) + return; + + var contentType = ContentType(); + + foreach (var type in parsers.Keys) { - if (!string.IsNullOrEmpty(_content)) + if (contentType.Contains(type)) { try { - _parsedContent = XDocument.Parse(_content); + _parsedContent = parsers[type](_content); return; } catch { + throw new Exception(string.Format("{0} parser failed to build dynamic object from data", type)); } } } - if (!string.IsNullOrEmpty(_content)) - throw new Exception(string.Format("({0}) not supported", contentType)); + throw new Exception(string.Format("({0}) not supported. Consider adding a parser for this type using RestAssured.AddParser", contentType)); } private void ParseLoad() diff --git a/src/RA/RestAssured.cs b/src/RA/RestAssured.cs old mode 100644 new mode 100755 index 161407f..524572f --- a/src/RA/RestAssured.cs +++ b/src/RA/RestAssured.cs @@ -1,7 +1,18 @@ -namespace RA +using System; + +namespace RA { public class RestAssured { + /// + /// Adds Parser to allow unsupported response types to be parsed + /// + /// Content-Type to use provided function to parse + /// Function to parse from body string to dynamic object + public static void AddParser(string type, Func func) + => ResponseContext.AddParser(type, func); + + public SetupContext Given() { return new SetupContext();