Skip to content

Commit a00a259

Browse files
authored
feat: add IsSuccessStatusCode (#1077)
1 parent fbb3764 commit a00a259

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/SendGrid/Response.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public HttpStatusCode StatusCode
6262
}
6363
}
6464

65+
/// <summary>
66+
/// Gets a value indicating whether Status Code of this response indicates success.
67+
/// </summary>
68+
public bool IsSuccessStatusCode
69+
{
70+
get { return ((int)StatusCode >= 200) && ((int)StatusCode <= 299); }
71+
}
72+
6573
/// <summary>
6674
/// Gets or sets the response body returned from Twilio SendGrid.
6775
/// </summary>

tests/SendGrid.Tests/Integration.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Net;
66
using System.Net.Http;
7+
using System.Net.Http.Headers;
78
using System.Text;
89
using System.Threading;
910
using System.Threading.Tasks;
@@ -2725,6 +2726,7 @@ public void TestSetGoogleAnalytics()
27252726
msg.SetGoogleAnalytics(true, "campaign3", "content3", "medium3", "source3", "term3");
27262727
Assert.Equal("{\"tracking_settings\":{\"ganalytics\":{\"enable\":true,\"utm_source\":\"source3\",\"utm_medium\":\"medium3\",\"utm_term\":\"term3\",\"utm_content\":\"content3\",\"utm_campaign\":\"campaign3\"}}}", msg.Serialize());
27272728
}
2729+
27282730
[Fact]
27292731
public async Task TestAccessSettingsActivityGet()
27302732
{
@@ -6004,6 +6006,33 @@ public void TestInjectSameHttpClientWithMultipleInstance()
60046006
var sg1 = new SendGridClient(clientToInject, fixture.apiKey);
60056007
var sg2 = new SendGridClient(clientToInject, fixture.apiKey);
60066008
}
6009+
6010+
[Fact]
6011+
public void TestBadRequestIsSuccessStatusCodeReturnsFalse()
6012+
{
6013+
var message = new HttpResponseMessage();
6014+
var response = new Response(HttpStatusCode.BadRequest, message.Content, message.Headers);
6015+
Assert.False(response.IsSuccessStatusCode);
6016+
}
6017+
6018+
[Fact]
6019+
public void TestOkRequestIsSuccessStatusCodeReturnsTrue()
6020+
{
6021+
var message = new HttpResponseMessage();
6022+
var response = new Response(HttpStatusCode.OK, message.Content, message.Headers);
6023+
Assert.True(response.IsSuccessStatusCode);
6024+
}
6025+
6026+
[Fact]
6027+
public void TestIsSuccessStatusCodeEvery2xxCodeReturnsTrue()
6028+
{
6029+
for (int i = 200; i <= 299; ++i)
6030+
{
6031+
var message = new HttpResponseMessage();
6032+
var response = new Response((HttpStatusCode)i, message.Content, message.Headers);
6033+
Assert.True(response.IsSuccessStatusCode);
6034+
}
6035+
}
60076036
}
60086037

60096038
public class FakeWebProxy : IWebProxy

0 commit comments

Comments
 (0)