Skip to content

Commit

Permalink
feat: add IsSuccessStatusCode (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoter authored Dec 14, 2020
1 parent fbb3764 commit a00a259
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/SendGrid/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public HttpStatusCode StatusCode
}
}

/// <summary>
/// Gets a value indicating whether Status Code of this response indicates success.
/// </summary>
public bool IsSuccessStatusCode
{
get { return ((int)StatusCode >= 200) && ((int)StatusCode <= 299); }
}

/// <summary>
/// Gets or sets the response body returned from Twilio SendGrid.
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -2725,6 +2726,7 @@ public void TestSetGoogleAnalytics()
msg.SetGoogleAnalytics(true, "campaign3", "content3", "medium3", "source3", "term3");
Assert.Equal("{\"tracking_settings\":{\"ganalytics\":{\"enable\":true,\"utm_source\":\"source3\",\"utm_medium\":\"medium3\",\"utm_term\":\"term3\",\"utm_content\":\"content3\",\"utm_campaign\":\"campaign3\"}}}", msg.Serialize());
}

[Fact]
public async Task TestAccessSettingsActivityGet()
{
Expand Down Expand Up @@ -6004,6 +6006,33 @@ public void TestInjectSameHttpClientWithMultipleInstance()
var sg1 = new SendGridClient(clientToInject, fixture.apiKey);
var sg2 = new SendGridClient(clientToInject, fixture.apiKey);
}

[Fact]
public void TestBadRequestIsSuccessStatusCodeReturnsFalse()
{
var message = new HttpResponseMessage();
var response = new Response(HttpStatusCode.BadRequest, message.Content, message.Headers);
Assert.False(response.IsSuccessStatusCode);
}

[Fact]
public void TestOkRequestIsSuccessStatusCodeReturnsTrue()
{
var message = new HttpResponseMessage();
var response = new Response(HttpStatusCode.OK, message.Content, message.Headers);
Assert.True(response.IsSuccessStatusCode);
}

[Fact]
public void TestIsSuccessStatusCodeEvery2xxCodeReturnsTrue()
{
for (int i = 200; i <= 299; ++i)
{
var message = new HttpResponseMessage();
var response = new Response((HttpStatusCode)i, message.Content, message.Headers);
Assert.True(response.IsSuccessStatusCode);
}
}
}

public class FakeWebProxy : IWebProxy
Expand Down

0 comments on commit a00a259

Please sign in to comment.