Skip to content

Commit 9e57655

Browse files
authored
Fix IsSuccessful in RestResponse (consider ResponseStatus) (#1893)
* Added IsSuccessStatusCode, fixed IsSuccessful so that ResponseStatus is considered * Also check IsSuccessStatusCode in StatusCodeTests * Added tests to check IsSuccessful if deserilization fails
1 parent cc74f44 commit 9e57655

File tree

5 files changed

+134
-38
lines changed

5 files changed

+134
-38
lines changed

src/RestSharp/Response/RestResponse.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,24 @@ public class RestResponse<T> : RestResponse {
3434

3535
public static RestResponse<T> FromResponse(RestResponse response)
3636
=> new() {
37-
Content = response.Content,
38-
RawBytes = response.RawBytes,
39-
ContentEncoding = response.ContentEncoding,
40-
ContentLength = response.ContentLength,
41-
ContentType = response.ContentType,
42-
Cookies = response.Cookies,
43-
ErrorMessage = response.ErrorMessage,
44-
ErrorException = response.ErrorException,
45-
Headers = response.Headers,
46-
ContentHeaders = response.ContentHeaders,
47-
IsSuccessful = response.IsSuccessful,
48-
ResponseStatus = response.ResponseStatus,
49-
ResponseUri = response.ResponseUri,
50-
Server = response.Server,
51-
StatusCode = response.StatusCode,
52-
StatusDescription = response.StatusDescription,
53-
Request = response.Request,
54-
RootElement = response.RootElement
37+
Content = response.Content,
38+
RawBytes = response.RawBytes,
39+
ContentEncoding = response.ContentEncoding,
40+
ContentLength = response.ContentLength,
41+
ContentType = response.ContentType,
42+
Cookies = response.Cookies,
43+
ErrorMessage = response.ErrorMessage,
44+
ErrorException = response.ErrorException,
45+
Headers = response.Headers,
46+
ContentHeaders = response.ContentHeaders,
47+
IsSuccessStatusCode = response.IsSuccessStatusCode,
48+
ResponseStatus = response.ResponseStatus,
49+
ResponseUri = response.ResponseUri,
50+
Server = response.Server,
51+
StatusCode = response.StatusCode,
52+
StatusDescription = response.StatusDescription,
53+
Request = response.Request,
54+
RootElement = response.RootElement
5555
};
5656
}
5757

@@ -82,24 +82,24 @@ async Task<RestResponse> GetDefaultResponse() {
8282
var content = bytes == null ? null : httpResponse.GetResponseString(bytes, encoding);
8383

8484
return new RestResponse {
85-
Content = content,
86-
RawBytes = bytes,
87-
ContentEncoding = httpResponse.Content.Headers.ContentEncoding,
88-
Version = httpResponse.RequestMessage?.Version,
89-
ContentLength = httpResponse.Content.Headers.ContentLength,
90-
ContentType = httpResponse.Content.Headers.ContentType?.MediaType,
91-
ResponseStatus = calculateResponseStatus(httpResponse),
92-
ErrorException = MaybeException(),
93-
ResponseUri = httpResponse.RequestMessage!.RequestUri,
94-
Server = httpResponse.Headers.Server.ToString(),
95-
StatusCode = httpResponse.StatusCode,
96-
StatusDescription = httpResponse.ReasonPhrase,
97-
IsSuccessful = httpResponse.IsSuccessStatusCode,
98-
Request = request,
99-
Headers = httpResponse.Headers.GetHeaderParameters(),
100-
ContentHeaders = httpResponse.Content.Headers.GetHeaderParameters(),
101-
Cookies = cookieCollection,
102-
RootElement = request.RootElement
85+
Content = content,
86+
RawBytes = bytes,
87+
ContentEncoding = httpResponse.Content.Headers.ContentEncoding,
88+
Version = httpResponse.RequestMessage?.Version,
89+
ContentLength = httpResponse.Content.Headers.ContentLength,
90+
ContentType = httpResponse.Content.Headers.ContentType?.MediaType,
91+
ResponseStatus = calculateResponseStatus(httpResponse),
92+
ErrorException = MaybeException(),
93+
ResponseUri = httpResponse.RequestMessage!.RequestUri,
94+
Server = httpResponse.Headers.Server.ToString(),
95+
StatusCode = httpResponse.StatusCode,
96+
StatusDescription = httpResponse.ReasonPhrase,
97+
IsSuccessStatusCode = httpResponse.IsSuccessStatusCode,
98+
Request = request,
99+
Headers = httpResponse.Headers.GetHeaderParameters(),
100+
ContentHeaders = httpResponse.Content.Headers.GetHeaderParameters(),
101+
Cookies = cookieCollection,
102+
RootElement = request.RootElement
103103
};
104104

105105
Exception? MaybeException()

src/RestSharp/Response/RestResponseBase.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ public abstract class RestResponseBase {
6161
public HttpStatusCode StatusCode { get; set; }
6262

6363
/// <summary>
64-
/// Whether or not the response status code indicates success
64+
/// Whether or not the HTTP response status code indicates success
6565
/// </summary>
66-
public bool IsSuccessful { get; set; }
66+
public bool IsSuccessStatusCode { get; set; }
67+
68+
/// <summary>
69+
/// Whether or not the HTTP response status code indicates success and no other error occurred (deserialization, timeout, ...)
70+
/// </summary>
71+
public bool IsSuccessful { get => IsSuccessStatusCode && ResponseStatus == ResponseStatus.Completed; }
6772

6873
/// <summary>
6974
/// Description of HTTP status returned

test/RestSharp.Tests.Integrated/StatusCodeTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public async Task ContentType_Additional_Information() {
3737

3838
response.StatusCode.Should().Be(HttpStatusCode.OK);
3939
response.IsSuccessful.Should().BeTrue();
40+
response.IsSuccessStatusCode.Should().BeTrue();
4041
}
4142

4243
[Fact]
@@ -88,6 +89,7 @@ public async Task Reports_1xx_Status_Code_Success_Accurately() {
8889
var response = await _client.ExecuteAsync(request);
8990

9091
response.IsSuccessful.Should().BeFalse();
92+
response.IsSuccessStatusCode.Should().BeFalse();
9193
}
9294

9395
[Fact]
@@ -96,6 +98,7 @@ public async Task Reports_2xx_Status_Code_Success_Accurately() {
9698
var response = await _client.ExecuteAsync(request);
9799

98100
response.IsSuccessful.Should().BeTrue();
101+
response.IsSuccessStatusCode.Should().BeTrue();
99102
}
100103

101104
[Fact]
@@ -104,6 +107,7 @@ public async Task Reports_3xx_Status_Code_Success_Accurately() {
104107
var response = await _client.ExecuteAsync(request);
105108

106109
response.IsSuccessful.Should().BeFalse();
110+
response.IsSuccessStatusCode.Should().BeFalse();
107111
}
108112

109113
[Fact]
@@ -112,6 +116,7 @@ public async Task Reports_4xx_Status_Code_Success_Accurately() {
112116
var response = await _client.ExecuteAsync(request);
113117

114118
response.IsSuccessful.Should().BeFalse();
119+
response.IsSuccessStatusCode.Should().BeFalse();
115120
}
116121

117122
[Fact]
@@ -120,6 +125,7 @@ public async Task Reports_5xx_Status_Code_Success_Accurately() {
120125
var response = await _client.ExecuteAsync(request);
121126

122127
response.IsSuccessful.Should().BeFalse();
128+
response.IsSuccessStatusCode.Should().BeFalse();
123129
}
124130
}
125131

test/RestSharp.Tests.Serializers.Json/NewtonsoftJsonTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,47 @@ public async Task Use_JsonNet_For_Response() {
9696

9797
actual.Should().BeEquivalentTo(expected);
9898
}
99+
100+
[Fact]
101+
public async Task DeserilizationFails_IsSuccessfull_Should_BeFalse()
102+
{
103+
using var server = HttpServerFixture.StartServer(
104+
(_, response) => {
105+
response.StatusCode = (int)HttpStatusCode.OK;
106+
response.ContentType = "application/json";
107+
response.ContentEncoding = Encoding.UTF8;
108+
response.OutputStream.WriteStringUtf8("invalid json");
109+
}
110+
);
111+
112+
var client = new RestClient(server.Url).UseNewtonsoftJson();
113+
114+
var response = await client.ExecuteAsync<TestClass>(new RestRequest());
115+
116+
response.IsSuccessStatusCode.Should().BeTrue();
117+
response.IsSuccessful.Should().BeFalse();
118+
}
119+
120+
[Fact]
121+
public async Task DeserilizationSucceeds_IsSuccessfull_Should_BeTrue() {
122+
var item = Fixture.Create<TestClass>();
123+
124+
using var server = HttpServerFixture.StartServer(
125+
(_, response) => {
126+
var serializer = new JsonNetSerializer();
127+
128+
response.StatusCode = (int)HttpStatusCode.OK;
129+
response.ContentType = "application/json";
130+
response.ContentEncoding = Encoding.UTF8;
131+
response.OutputStream.WriteStringUtf8(serializer.Serialize(item)!);
132+
}
133+
);
134+
135+
var client = new RestClient(server.Url).UseNewtonsoftJson();
136+
137+
var response = await client.ExecuteAsync<TestClass>(new RestRequest());
138+
139+
response.IsSuccessStatusCode.Should().BeTrue();
140+
response.IsSuccessful.Should().BeTrue();
141+
}
99142
}

test/RestSharp.Tests.Serializers.Json/SystemTextJsonTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,46 @@ public async Task Use_JsonNet_For_Response() {
5151

5252
actual.Should().BeEquivalentTo(expected);
5353
}
54+
55+
[Fact]
56+
public async Task DeserilizationFails_IsSuccessfull_Should_BeFalse() {
57+
using var server = HttpServerFixture.StartServer(
58+
(_, response) => {
59+
response.StatusCode = (int)HttpStatusCode.OK;
60+
response.ContentType = "application/json";
61+
response.ContentEncoding = Encoding.UTF8;
62+
response.OutputStream.WriteStringUtf8("invalid json");
63+
}
64+
);
65+
66+
var client = new RestClient(server.Url).UseSystemTextJson();
67+
68+
var response = await client.ExecuteAsync<TestClass>(new RestRequest());
69+
70+
response.IsSuccessStatusCode.Should().BeTrue();
71+
response.IsSuccessful.Should().BeFalse();
72+
}
73+
74+
[Fact]
75+
public async Task DeserilizationSucceeds_IsSuccessfull_Should_BeTrue() {
76+
var item = Fixture.Create<TestClass>();
77+
78+
using var server = HttpServerFixture.StartServer(
79+
(_, response) => {
80+
var serializer = new SystemTextJsonSerializer();
81+
82+
response.StatusCode = (int)HttpStatusCode.OK;
83+
response.ContentType = "application/json";
84+
response.ContentEncoding = Encoding.UTF8;
85+
response.OutputStream.WriteStringUtf8(serializer.Serialize(item)!);
86+
}
87+
);
88+
89+
var client = new RestClient(server.Url).UseSystemTextJson();
90+
91+
var response = await client.ExecuteAsync<TestClass>(new RestRequest());
92+
93+
response.IsSuccessStatusCode.Should().BeTrue();
94+
response.IsSuccessful.Should().BeTrue();
95+
}
5496
}

0 commit comments

Comments
 (0)