From 9001ddf4244c6a1f641841f2f251d8e6de78f80b Mon Sep 17 00:00:00 2001 From: Hossein dinarvand Date: Fri, 16 Oct 2026 15:32:31 +0330 Subject: [PATCH] fix : add trailers property and implement test --- src/RestSharp/Response/RestResponse.cs | 3 ++ src/RestSharp/Response/RestResponseBase.cs | 5 ++++ .../ResponseTrailerTests.cs | 29 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 test/RestSharp.Tests.Integrated/ResponseTrailerTests.cs diff --git a/src/RestSharp/Response/RestResponse.cs b/src/RestSharp/Response/RestResponse.cs index d092271e6..d08119cf2 100644 --- a/src/RestSharp/Response/RestResponse.cs +++ b/src/RestSharp/Response/RestResponse.cs @@ -73,6 +73,9 @@ async Task GetDefaultResponse() { Server = httpResponse.Headers.Server.ToString(), StatusCode = httpResponse.StatusCode, StatusDescription = httpResponse.ReasonPhrase, +#if NET + TrailingHeaders = httpResponse.TrailingHeaders.GetHeaderParameters(), +#endif Version = httpResponse.RequestMessage?.Version }; } diff --git a/src/RestSharp/Response/RestResponseBase.cs b/src/RestSharp/Response/RestResponseBase.cs index 57b1f5c4a..7ff2d5ba0 100644 --- a/src/RestSharp/Response/RestResponseBase.cs +++ b/src/RestSharp/Response/RestResponseBase.cs @@ -111,6 +111,11 @@ protected RestResponseBase(RestRequest request) { /// public IReadOnlyCollection? ContentHeaders { get; set; } + /// + /// Trailer headers returned by server with the response + /// + public IReadOnlyCollection? TrailingHeaders { get; set; } + /// /// Status of the request. Will return Error for transport errors. /// HTTP errors will still return ResponseStatus.Completed, check StatusCode instead diff --git a/test/RestSharp.Tests.Integrated/ResponseTrailerTests.cs b/test/RestSharp.Tests.Integrated/ResponseTrailerTests.cs new file mode 100644 index 000000000..5ffcdd1d4 --- /dev/null +++ b/test/RestSharp.Tests.Integrated/ResponseTrailerTests.cs @@ -0,0 +1,29 @@ +#if NET8_0_OR_GREATER +using System.Net; +using System.Net.Http; + +namespace RestSharp.Tests.Integrated; + +public class ResponseTrailerTests { + [Fact] + public async Task Response_trailers_are_preserved_separately_from_headers() { + using var client = new RestClient(new TrailerResponseHandler()); + + var response = await client.ExecuteAsync(new RestRequest("https://example.test/")); + + response.TrailingHeaders.Should().ContainSingle(header => header.Name == "Checksum" && header.Value == "abc123"); + response.Headers.Should().NotContain(header => header.Name == "Checksum"); + response.ContentHeaders.Should().NotContain(header => header.Name == "Checksum"); + } + + sealed class TrailerResponseHandler : HttpMessageHandler { + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + var response = new HttpResponseMessage(HttpStatusCode.OK) { + Content = new StringContent("body") + }; + response.TrailingHeaders.TryAddWithoutValidation("Checksum", "abc123"); + return Task.FromResult(response); + } + } +} +#endif