Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions src/RestSharp/Response/RestResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ public class RestResponse<T> : RestResponse {

public static RestResponse<T> FromResponse(RestResponse response)
=> new() {
Content = response.Content,
RawBytes = response.RawBytes,
ContentEncoding = response.ContentEncoding,
ContentLength = response.ContentLength,
ContentType = response.ContentType,
Cookies = response.Cookies,
ErrorMessage = response.ErrorMessage,
ErrorException = response.ErrorException,
Headers = response.Headers,
ContentHeaders = response.ContentHeaders,
IsSuccessful = response.IsSuccessful,
ResponseStatus = response.ResponseStatus,
ResponseUri = response.ResponseUri,
Server = response.Server,
StatusCode = response.StatusCode,
StatusDescription = response.StatusDescription,
Request = response.Request,
RootElement = response.RootElement
Content = response.Content,
RawBytes = response.RawBytes,
ContentEncoding = response.ContentEncoding,
ContentLength = response.ContentLength,
ContentType = response.ContentType,
Cookies = response.Cookies,
ErrorMessage = response.ErrorMessage,
ErrorException = response.ErrorException,
Headers = response.Headers,
ContentHeaders = response.ContentHeaders,
IsSuccessStatusCode = response.IsSuccessStatusCode,
ResponseStatus = response.ResponseStatus,
ResponseUri = response.ResponseUri,
Server = response.Server,
StatusCode = response.StatusCode,
StatusDescription = response.StatusDescription,
Request = response.Request,
RootElement = response.RootElement
};
}

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

return new RestResponse {
Content = content,
RawBytes = bytes,
ContentEncoding = httpResponse.Content.Headers.ContentEncoding,
Version = httpResponse.RequestMessage?.Version,
ContentLength = httpResponse.Content.Headers.ContentLength,
ContentType = httpResponse.Content.Headers.ContentType?.MediaType,
ResponseStatus = calculateResponseStatus(httpResponse),
ErrorException = MaybeException(),
ResponseUri = httpResponse.RequestMessage!.RequestUri,
Server = httpResponse.Headers.Server.ToString(),
StatusCode = httpResponse.StatusCode,
StatusDescription = httpResponse.ReasonPhrase,
IsSuccessful = httpResponse.IsSuccessStatusCode,
Request = request,
Headers = httpResponse.Headers.GetHeaderParameters(),
ContentHeaders = httpResponse.Content.Headers.GetHeaderParameters(),
Cookies = cookieCollection,
RootElement = request.RootElement
Content = content,
RawBytes = bytes,
ContentEncoding = httpResponse.Content.Headers.ContentEncoding,
Version = httpResponse.RequestMessage?.Version,
ContentLength = httpResponse.Content.Headers.ContentLength,
ContentType = httpResponse.Content.Headers.ContentType?.MediaType,
ResponseStatus = calculateResponseStatus(httpResponse),
ErrorException = MaybeException(),
ResponseUri = httpResponse.RequestMessage!.RequestUri,
Server = httpResponse.Headers.Server.ToString(),
StatusCode = httpResponse.StatusCode,
StatusDescription = httpResponse.ReasonPhrase,
IsSuccessStatusCode = httpResponse.IsSuccessStatusCode,
Request = request,
Headers = httpResponse.Headers.GetHeaderParameters(),
ContentHeaders = httpResponse.Content.Headers.GetHeaderParameters(),
Cookies = cookieCollection,
RootElement = request.RootElement
};

Exception? MaybeException()
Expand Down
9 changes: 7 additions & 2 deletions src/RestSharp/Response/RestResponseBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ public abstract class RestResponseBase {
public HttpStatusCode StatusCode { get; set; }

/// <summary>
/// Whether or not the response status code indicates success
/// Whether or not the HTTP response status code indicates success
/// </summary>
public bool IsSuccessful { get; set; }
public bool IsSuccessStatusCode { get; set; }

/// <summary>
/// Whether or not the HTTP response status code indicates success and no other error occurred (deserialization, timeout, ...)
/// </summary>
public bool IsSuccessful { get => IsSuccessStatusCode && ResponseStatus == ResponseStatus.Completed; }

/// <summary>
/// Description of HTTP status returned
Expand Down
6 changes: 6 additions & 0 deletions test/RestSharp.Tests.Integrated/StatusCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public async Task ContentType_Additional_Information() {

response.StatusCode.Should().Be(HttpStatusCode.OK);
response.IsSuccessful.Should().BeTrue();
response.IsSuccessStatusCode.Should().BeTrue();
}

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

response.IsSuccessful.Should().BeFalse();
response.IsSuccessStatusCode.Should().BeFalse();
}

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

response.IsSuccessful.Should().BeTrue();
response.IsSuccessStatusCode.Should().BeTrue();
}

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

response.IsSuccessful.Should().BeFalse();
response.IsSuccessStatusCode.Should().BeFalse();
}

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

response.IsSuccessful.Should().BeFalse();
response.IsSuccessStatusCode.Should().BeFalse();
}

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

response.IsSuccessful.Should().BeFalse();
response.IsSuccessStatusCode.Should().BeFalse();
}
}

Expand Down
43 changes: 43 additions & 0 deletions test/RestSharp.Tests.Serializers.Json/NewtonsoftJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,47 @@ public async Task Use_JsonNet_For_Response() {

actual.Should().BeEquivalentTo(expected);
}

[Fact]
public async Task DeserilizationFails_IsSuccessfull_Should_BeFalse()
{
using var server = HttpServerFixture.StartServer(
(_, response) => {
response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "application/json";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8("invalid json");
}
);

var client = new RestClient(server.Url).UseNewtonsoftJson();

var response = await client.ExecuteAsync<TestClass>(new RestRequest());

response.IsSuccessStatusCode.Should().BeTrue();
response.IsSuccessful.Should().BeFalse();
}

[Fact]
public async Task DeserilizationSucceeds_IsSuccessfull_Should_BeTrue() {
var item = Fixture.Create<TestClass>();

using var server = HttpServerFixture.StartServer(
(_, response) => {
var serializer = new JsonNetSerializer();

response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "application/json";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8(serializer.Serialize(item)!);
}
);

var client = new RestClient(server.Url).UseNewtonsoftJson();

var response = await client.ExecuteAsync<TestClass>(new RestRequest());

response.IsSuccessStatusCode.Should().BeTrue();
response.IsSuccessful.Should().BeTrue();
}
}
42 changes: 42 additions & 0 deletions test/RestSharp.Tests.Serializers.Json/SystemTextJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,46 @@ public async Task Use_JsonNet_For_Response() {

actual.Should().BeEquivalentTo(expected);
}

[Fact]
public async Task DeserilizationFails_IsSuccessfull_Should_BeFalse() {
using var server = HttpServerFixture.StartServer(
(_, response) => {
response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "application/json";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8("invalid json");
}
);

var client = new RestClient(server.Url).UseSystemTextJson();

var response = await client.ExecuteAsync<TestClass>(new RestRequest());

response.IsSuccessStatusCode.Should().BeTrue();
response.IsSuccessful.Should().BeFalse();
}

[Fact]
public async Task DeserilizationSucceeds_IsSuccessfull_Should_BeTrue() {
var item = Fixture.Create<TestClass>();

using var server = HttpServerFixture.StartServer(
(_, response) => {
var serializer = new SystemTextJsonSerializer();

response.StatusCode = (int)HttpStatusCode.OK;
response.ContentType = "application/json";
response.ContentEncoding = Encoding.UTF8;
response.OutputStream.WriteStringUtf8(serializer.Serialize(item)!);
}
);

var client = new RestClient(server.Url).UseSystemTextJson();

var response = await client.ExecuteAsync<TestClass>(new RestRequest());

response.IsSuccessStatusCode.Should().BeTrue();
response.IsSuccessful.Should().BeTrue();
}
}