From 1e07b497e37f7be8676691546c670de385345e93 Mon Sep 17 00:00:00 2001 From: Matt Houser Date: Mon, 29 Aug 2016 20:53:57 -0400 Subject: [PATCH] Catch error responses from the server --- .../EndToEnd/SendAsyncTests.cs | 32 +++++++++++++++++++ .../Helpers/ErrorResponseException.cs | 32 +++++++++++++++++++ SendWithUs.Client/SendWithUs.Client.csproj | 1 + SendWithUs.Client/SendWithUsClient.cs | 12 +++++++ 4 files changed, 77 insertions(+) create mode 100644 SendWithUs.Client/Helpers/ErrorResponseException.cs diff --git a/SendWithUs.Client.Tests/EndToEnd/SendAsyncTests.cs b/SendWithUs.Client.Tests/EndToEnd/SendAsyncTests.cs index 9eb8a50..a188863 100644 --- a/SendWithUs.Client.Tests/EndToEnd/SendAsyncTests.cs +++ b/SendWithUs.Client.Tests/EndToEnd/SendAsyncTests.cs @@ -117,5 +117,37 @@ public void SendAsync_WithFileAttachment_Succeeds() Assert.AreEqual("OK", response.Status, true); Assert.AreEqual(true, response.Success); } + + class BigDataClass + { + public BigDataClass() + { + var data = new List(); + for (int i = 0; i < 100000; ++i) + data.Add(Guid.NewGuid().ToString()); + this.Data = data; + } + public IEnumerable Data { get; set; } + } + + [TestMethod] + public void SendAsync_WithTooMuchData_Throws() + { + // Arrange + var testData = new TestData("EndToEnd/Data/SendRequest.xml"); + var request = new SendRequest(testData.TemplateId, testData.RecipientAddress) + { + Data = new BigDataClass() + }; + var client = new SendWithUsClient(testData.ApiKey); + + request.SenderAddress = testData.SenderAddress; + + // Act + var exception = TestHelper.CaptureException(() => client.SendAsync(request)); + + // Assert + Assert.IsInstanceOfType(exception, typeof(ErrorResponseException)); + } } } diff --git a/SendWithUs.Client/Helpers/ErrorResponseException.cs b/SendWithUs.Client/Helpers/ErrorResponseException.cs new file mode 100644 index 0000000..932b62b --- /dev/null +++ b/SendWithUs.Client/Helpers/ErrorResponseException.cs @@ -0,0 +1,32 @@ +// Copyright © 2014 Mimeo, Inc. + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +namespace SendWithUs.Client +{ + using System; + + public class ErrorResponseException : Exception + { + public ErrorResponseException(string responseMessage) + : base(responseMessage) + { + } + } +} diff --git a/SendWithUs.Client/SendWithUs.Client.csproj b/SendWithUs.Client/SendWithUs.Client.csproj index 0087354..a642a08 100644 --- a/SendWithUs.Client/SendWithUs.Client.csproj +++ b/SendWithUs.Client/SendWithUs.Client.csproj @@ -42,6 +42,7 @@ + diff --git a/SendWithUs.Client/SendWithUsClient.cs b/SendWithUs.Client/SendWithUsClient.cs index 3419b85..7e28801 100644 --- a/SendWithUs.Client/SendWithUsClient.cs +++ b/SendWithUs.Client/SendWithUsClient.cs @@ -202,6 +202,18 @@ protected async Task ExecuteAsync(IRequest request) where TResponse : class, IResponse { var httpResponse = await this.GetHttpResponseAsync(request); + if (!httpResponse.IsSuccessStatusCode) + { + string contentType = httpResponse.Content.Headers.ContentType.MediaType; + if (contentType == "text/html") + { + string content = await httpResponse.Content.ReadAsStringAsync(); + throw new ErrorResponseException(content); + } + + // Fall through. The EnsureSuccessStatusCode will catch it. + } + var json = await httpResponse.EnsureSuccessStatusCode().Content.ReadAsAsync(); return this.ResponseFactory.Create(httpResponse.StatusCode, json); }