From e483a10a2bcba1aaeb4df4f18d22269877ab7bbd Mon Sep 17 00:00:00 2001 From: Dayne Dougan Date: Mon, 12 Aug 2019 11:32:57 +0100 Subject: [PATCH 1/3] Added ability to add custom response parsers --- src/RA/ResponseContext.cs | 77 +++++++++++++++++++++++++++------------ src/RA/RestAssured.cs | 8 ++++ 2 files changed, 61 insertions(+), 24 deletions(-) mode change 100644 => 100755 src/RA/ResponseContext.cs mode change 100644 => 100755 src/RA/RestAssured.cs diff --git a/src/RA/ResponseContext.cs b/src/RA/ResponseContext.cs old mode 100644 new mode 100755 index 183743b..0014032 --- a/src/RA/ResponseContext.cs +++ b/src/RA/ResponseContext.cs @@ -220,54 +220,83 @@ 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 + internal 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 json from data", type)); } } + else + { + throw new Exception(string.Format("({0}) not supported", contentType)); + } } - - if (!string.IsNullOrEmpty(_content)) - throw new Exception(string.Format("({0}) not supported", contentType)); } private void ParseLoad() @@ -362,4 +391,4 @@ public ResponseContext WriteAssertions() return this; } } -} \ No newline at end of file +} diff --git a/src/RA/RestAssured.cs b/src/RA/RestAssured.cs old mode 100644 new mode 100755 index 161407f..21c578d --- a/src/RA/RestAssured.cs +++ b/src/RA/RestAssured.cs @@ -6,5 +6,13 @@ public SetupContext Given() { return new SetupContext(); } + + /// + /// Adds Parser to allow unsupporter response types to be parsed + /// + /// Content-Type to use provided function to parse + /// Function to parse type + internal static void AddParser(string type, System.Func func) + => ResponseContext.AddParser(type, func); } } From b94521943220fbc3217c67e0cfd6dc06ff7f749a Mon Sep 17 00:00:00 2001 From: Dayne Dougan Date: Mon, 12 Aug 2019 11:32:57 +0100 Subject: [PATCH 2/3] Added ability to add custom response parsers --- README.md | 13 +++++++ src/RA/ResponseContext.cs | 77 +++++++++++++++++++++++++++------------ src/RA/RestAssured.cs | 8 ++++ 3 files changed, 74 insertions(+), 24 deletions(-) mode change 100644 => 100755 src/RA/ResponseContext.cs mode change 100644 => 100755 src/RA/RestAssured.cs diff --git a/README.md b/README.md index e95e4a4..a116a8e 100644 --- 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 +} +//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/ResponseContext.cs b/src/RA/ResponseContext.cs old mode 100644 new mode 100755 index 183743b..0014032 --- a/src/RA/ResponseContext.cs +++ b/src/RA/ResponseContext.cs @@ -220,54 +220,83 @@ 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 + internal 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 json from data", type)); } } + else + { + throw new Exception(string.Format("({0}) not supported", contentType)); + } } - - if (!string.IsNullOrEmpty(_content)) - throw new Exception(string.Format("({0}) not supported", contentType)); } private void ParseLoad() @@ -362,4 +391,4 @@ public ResponseContext WriteAssertions() return this; } } -} \ No newline at end of file +} diff --git a/src/RA/RestAssured.cs b/src/RA/RestAssured.cs old mode 100644 new mode 100755 index 161407f..21c578d --- a/src/RA/RestAssured.cs +++ b/src/RA/RestAssured.cs @@ -6,5 +6,13 @@ public SetupContext Given() { return new SetupContext(); } + + /// + /// Adds Parser to allow unsupporter response types to be parsed + /// + /// Content-Type to use provided function to parse + /// Function to parse type + internal static void AddParser(string type, System.Func func) + => ResponseContext.AddParser(type, func); } } From f9a73acc1c00c54fa59d301fb9f840c02fb498dd Mon Sep 17 00:00:00 2001 From: Dayne Dougan Date: Mon, 12 Aug 2019 17:11:07 +0100 Subject: [PATCH 3/3] Adding port into SetUri when this was given during setup --- src/RA/HttpActionContext.cs | 12 ++++++++++-- src/RA/SetupContext.cs | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/RA/HttpActionContext.cs b/src/RA/HttpActionContext.cs index b774fc3..4b73869 100644 --- a/src/RA/HttpActionContext.cs +++ b/src/RA/HttpActionContext.cs @@ -165,7 +165,15 @@ private ExecutionContext SetHttpAction(string url, HttpActionType actionType) public void SetUrl(string url) { - if (url.IsEmpty() && _setupContext.Host().IsEmpty()) + if(!new System.Text.RegularExpressions.Regex(@":[0-9]{1,4}").IsMatch(url) && _setupContext.PortSpecified()) + { + if (new System.Text.RegularExpressions.Regex(@"https?:\/\/").IsMatch(url)) + url = url.Substring(0, url.IndexOf("/", 8)) + ":3005" + url.Substring(url.IndexOf("/", 8)); + else + url = url.Substring(0, url.IndexOf("/")) + ":3005" + url.Substring(url.IndexOf("/")); + } + + if (url.IsEmpty() && _setupContext.Host().IsEmpty()) throw new ArgumentException("url must be provided"); var uri = url.IsNotEmpty() @@ -175,4 +183,4 @@ public void SetUrl(string url) _url = uri.OriginalString; } } -} \ No newline at end of file +} diff --git a/src/RA/SetupContext.cs b/src/RA/SetupContext.cs index 82783b5..9f10bed 100644 --- a/src/RA/SetupContext.cs +++ b/src/RA/SetupContext.cs @@ -91,7 +91,7 @@ public string Host() return PortSpecified() ? $"{_host}:{_port}":_host; } - private bool PortSpecified() + internal bool PortSpecified() { return _port > 0 && _port != 88; }