summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHoward van Rooijen <Howard.vanRooijen@endjin.com>2015-02-04 21:49:29 +0000
committerHoward van Rooijen <Howard.vanRooijen@endjin.com>2015-02-06 07:23:55 +0000
commit2705013d2b58d0e614205606c87c4ad2ecec8259 (patch)
tree93e610e85a2bf722bc0ce2581a42e386e84ccf2a
parent6354302c92e8994b44ae8fc89a992ad4ffca5b6d (diff)
downloadsendgrid-csharp-2705013d2b58d0e614205606c87c4ad2ecec8259.zip
sendgrid-csharp-2705013d2b58d0e614205606c87c4ad2ecec8259.tar.gz
sendgrid-csharp-2705013d2b58d0e614205606c87c4ad2ecec8259.tar.bz2
Refactor error handling. Add test for checking for bad username / password scenario
-rw-r--r--SendGrid/SendGridMail/Mail.csproj1
-rw-r--r--SendGrid/SendGridMail/Transport/ErrorChecker.cs57
-rw-r--r--SendGrid/SendGridMail/Transport/Web.cs66
-rw-r--r--SendGrid/Tests/Tests.csproj2
-rw-r--r--SendGrid/Tests/Transport/TestErrorChecker.cs34
5 files changed, 98 insertions, 62 deletions
diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj
index b1f8f84..998226f 100644
--- a/SendGrid/SendGridMail/Mail.csproj
+++ b/SendGrid/SendGridMail/Mail.csproj
@@ -103,6 +103,7 @@
<Compile Include="ISendGrid.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SendGrid.cs" />
+ <Compile Include="Transport\ErrorChecker.cs" />
<Compile Include="Transport\ITransport.cs" />
<Compile Include="Transport\Web.cs" />
</ItemGroup>
diff --git a/SendGrid/SendGridMail/Transport/ErrorChecker.cs b/SendGrid/SendGridMail/Transport/ErrorChecker.cs
new file mode 100644
index 0000000..035d2b8
--- /dev/null
+++ b/SendGrid/SendGridMail/Transport/ErrorChecker.cs
@@ -0,0 +1,57 @@
+namespace SendGrid
+{
+ using System;
+ using System.IO;
+ using System.Net;
+ using System.Net.Http;
+ using System.Threading.Tasks;
+ using System.Xml;
+
+ using Exceptions;
+
+ public static class ErrorChecker
+ {
+ public static void CheckForErrors(HttpResponseMessage response)
+ {
+ CheckForErrors(response, response.Content.ReadAsStreamAsync().Result);
+ }
+
+ public static async Task CheckForErrorsAsync(HttpResponseMessage response)
+ {
+ CheckForErrors(response, await response.Content.ReadAsStreamAsync());
+ }
+
+ private static void CheckForErrors(HttpResponseMessage response, Stream stream)
+ {
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ using (var reader = XmlReader.Create(stream))
+ {
+ while (reader.Read())
+ {
+ if (!reader.IsStartElement())
+ {
+ continue;
+ }
+
+ switch (reader.Name)
+ {
+ case "result":
+ continue;
+ case "message":
+ continue;
+ case "errors":
+ reader.ReadToFollowing("error");
+ var message = reader.ReadElementContentAsString("error", reader.NamespaceURI);
+ throw new InvalidApiRequestException(response.StatusCode, new[] { message }, response.ReasonPhrase);
+ case "error":
+ throw new ProtocolViolationException();
+ default:
+ throw new ArgumentException("Unknown element: " + reader.Name);
+ }
+ }
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index 7abf104..a125833 100644
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -66,7 +66,7 @@ namespace SendGrid
AttachFormParams(message, content);
AttachFiles(message, content);
var response = client.PostAsync(Endpoint + ".xml", content).Result;
- CheckForErrors(response);
+ ErrorChecker.CheckForErrors(response);
}
/// <summary>
@@ -87,7 +87,7 @@ namespace SendGrid
AttachFormParams(message, content);
AttachFiles(message, content);
var response = await client.PostAsync(Endpoint + ".xml", content);
- await CheckForErrorsAsync(response);
+ await ErrorChecker.CheckForErrorsAsync(response);
}
#region Support Methods
@@ -136,66 +136,8 @@ namespace SendGrid
}
}
- private static void CheckForErrors(HttpResponseMessage response)
- {
- var content = response.Content.ReadAsStreamAsync().Result;
- var errors = GetErrorsInResponse(content);
-
- // API error
- if (errors.Any())
- throw new InvalidApiRequestException(response.StatusCode, errors, response.ReasonPhrase);
-
- // Other error
- if (response.StatusCode != HttpStatusCode.OK)
- FindErrorsInResponse(content);
- }
-
- private static void FindErrorsInResponse(Stream content)
- {
- using (var reader = XmlReader.Create(content))
- {
- while (reader.Read())
- {
- if (!reader.IsStartElement()) continue;
- switch (reader.Name)
- {
- case "result":
- break;
- case "message": // success
- if (reader.ReadToNextSibling("errors"))
- throw new ProtocolViolationException();
- return;
- case "error": // failure
- throw new ProtocolViolationException();
- default:
- throw new ArgumentException("Unknown element: " + reader.Name);
- }
- }
- }
- }
-
- private static string[] GetErrorsInResponse(Stream content)
- {
- var xmlDoc = new XmlDocument();
- xmlDoc.Load(content);
- return (from XmlNode errorNode in xmlDoc.SelectNodes("//error") select errorNode.InnerText).ToArray();
- }
-
- private static async Task CheckForErrorsAsync(HttpResponseMessage response)
- {
- var content = await response.Content.ReadAsStreamAsync();
-
- var errors = GetErrorsInResponse(content);
-
- // API error
- if (errors.Any())
- throw new InvalidApiRequestException(response.StatusCode, errors, response.ReasonPhrase);
-
- // Other error
- if (response.StatusCode != HttpStatusCode.OK)
- FindErrorsInResponse(content);
- }
-
+
+
internal List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
{
var result = new List<KeyValuePair<string, string>>
diff --git a/SendGrid/Tests/Tests.csproj b/SendGrid/Tests/Tests.csproj
index 7a62424..2dba458 100644
--- a/SendGrid/Tests/Tests.csproj
+++ b/SendGrid/Tests/Tests.csproj
@@ -62,10 +62,12 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net" />
+ <Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestSendgrid.cs" />
+ <Compile Include="Transport\TestErrorChecker.cs" />
<Compile Include="Transport\TestWebApi.cs" />
</ItemGroup>
<ItemGroup>
diff --git a/SendGrid/Tests/Transport/TestErrorChecker.cs b/SendGrid/Tests/Transport/TestErrorChecker.cs
new file mode 100644
index 0000000..f178064
--- /dev/null
+++ b/SendGrid/Tests/Transport/TestErrorChecker.cs
@@ -0,0 +1,34 @@
+namespace Transport
+{
+ #region Using Directives
+
+ using System;
+ using System.Net;
+ using System.Net.Http;
+
+ using Exceptions;
+
+ using NUnit.Framework;
+
+ using SendGrid;
+
+ #endregion
+
+ [TestFixture]
+ public class TestErrorChecker
+ {
+ private const string BadUsernameOrPasswordResponseMessage = "<result><message>error</message><errors><error>Bad username / password</error></errors></result>";
+
+ [Test]
+ [ExpectedException(typeof(InvalidApiRequestException))]
+ public void WhenHttpResponseContainsBadUserErrorItIsDetectedAndAInvalidApiRequestIsThrown()
+ {
+ var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
+ {
+ Content = new StringContent(BadUsernameOrPasswordResponseMessage)
+ };
+
+ ErrorChecker.CheckForErrors(response);
+ }
+ }
+} \ No newline at end of file