Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/RestSharp/Response/RestResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ async Task<RestResponse> GetDefaultResponse() {
Server = httpResponse.Headers.Server.ToString(),
StatusCode = httpResponse.StatusCode,
StatusDescription = httpResponse.ReasonPhrase,
#if NET
TrailingHeaders = httpResponse.TrailingHeaders.GetHeaderParameters(),
#endif
Version = httpResponse.RequestMessage?.Version
};
}
Expand All @@ -94,4 +97,4 @@ public RestResponse() : this(new()) { }
}
}

public delegate ResponseStatus CalculateResponseStatus(HttpResponseMessage httpResponse);
public delegate ResponseStatus CalculateResponseStatus(HttpResponseMessage httpResponse);
5 changes: 5 additions & 0 deletions src/RestSharp/Response/RestResponseBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ protected RestResponseBase(RestRequest request) {
/// </summary>
public IReadOnlyCollection<HeaderParameter>? ContentHeaders { get; set; }

/// <summary>
/// Trailer headers returned by server after the response content
/// </summary>
public IReadOnlyCollection<HeaderParameter> TrailingHeaders { get; set; } = Array.Empty<HeaderParameter>();

/// <summary>
/// Status of the request. Will return Error for transport errors.
/// HTTP errors will still return ResponseStatus.Completed, check StatusCode instead
Expand Down
37 changes: 37 additions & 0 deletions test/RestSharp.Tests/ResponseTrailerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Net;

namespace RestSharp.Tests;

public class ResponseTrailerTests {
#if NET
[Fact]
public async Task Should_map_trailing_headers_separately_from_regular_headers() {
using var client = new RestClient(
new RestClientOptions("https://dummy.org") { ConfigureMessageHandler = _ => new TrailerResponseHandler() }
);

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

response.TrailingHeaders.Should().BeEquivalentTo(
new[] {
new HeaderParameter("Digest", "sha-256=abc"),
new HeaderParameter("Digest", "sha-256=def"),
new HeaderParameter("X-Processing-Status", "complete")
}
);
response.Headers.Should().ContainSingle(x => x.Name == "X-Regular-Header" && x.Value == "regular");
response.Headers.Should().NotContain(x => x.Name == "Digest" || x.Name == "X-Processing-Status");
Comment thread
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
response.ContentHeaders.Should().NotContain(x => x.Name == "Digest" || x.Name == "X-Processing-Status");
}

class TrailerResponseHandler : HttpMessageHandler {
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}") };
response.Headers.TryAddWithoutValidation("X-Regular-Header", "regular");
response.TrailingHeaders.TryAddWithoutValidation("Digest", ["sha-256=abc", "sha-256=def"]);
response.TrailingHeaders.TryAddWithoutValidation("X-Processing-Status", "complete");
return Task.FromResult(response);
}
}
#endif
}