Skip to content

Commit a866fe2

Browse files
alexeyzimarevclaude
andcommitted
Reduce code duplication: consolidate ForwardBody tests and reuse shared EchoRequest
- Merge ForwardBody_False and ForwardBody_True into a single parameterized Theory - Replace inline echo-request callback with shared WireMockTestServer.EchoRequest - Make EchoRequest public for cross-project reuse Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent a248fca commit a866fe2

2 files changed

Lines changed: 12 additions & 28 deletions

File tree

test/RestSharp.Tests.Integrated/CookieRedirectTests.cs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ public async Task ForwardAuthorizationToExternalHost_Controls_Cross_Origin_Auth(
193193
using var externalServer = WireMockServer.Start();
194194
externalServer
195195
.Given(Request.Create().WithPath("/echo-request"))
196-
.RespondWith(Response.Create().WithCallback(request => {
197-
var headers = request.Headers?
198-
.ToDictionary(x => x.Key, x => string.Join(", ", x.Value))
199-
?? new Dictionary<string, string>();
200-
return WireMockTestServer.CreateJson(new { Method = request.Method, Headers = headers, Body = request.Body ?? "" });
201-
}));
196+
.RespondWith(Response.Create().WithCallback(WireMockTestServer.EchoRequest));
202197

203198
// Main server redirects to the external server
204199
var redirectPath = $"/redirect-external-{allowExternal}";
@@ -256,38 +251,27 @@ public async Task ForwardCookies_False_Should_Not_Send_Cookies_On_Redirect() {
256251

257252
// ─── ForwardBody ────────────────────────────────────────────────────
258253

259-
[Fact]
260-
public async Task ForwardBody_False_Should_Drop_Body_Even_When_Verb_Preserved() {
254+
[Theory]
255+
[InlineData(false)]
256+
[InlineData(true)]
257+
public async Task ForwardBody_Controls_Body_Forwarding_When_Verb_Preserved(bool forwardBody) {
261258
using var client = CreateClient(o =>
262-
o.RedirectOptions = new RedirectOptions { ForwardBody = false }
259+
o.RedirectOptions = new RedirectOptions { ForwardBody = forwardBody }
263260
);
264261

265262
var request = new RestRequest("/redirect-with-status?status=307", Method.Post);
266-
request.AddJsonBody(new { data = "should-be-dropped" });
263+
request.AddJsonBody(new { data = "test-body" });
267264

268265
var response = await client.ExecuteAsync(request);
269266

270267
response.StatusCode.Should().Be(HttpStatusCode.OK);
271268
var doc = JsonDocument.Parse(response.Content!);
272269
doc.RootElement.GetProperty("Method").GetString().Should().Be("POST");
273-
doc.RootElement.GetProperty("Body").GetString().Should().BeEmpty();
274-
}
275270

276-
[Fact]
277-
public async Task ForwardBody_True_Should_Forward_Body_When_Verb_Preserved() {
278-
using var client = CreateClient(o =>
279-
o.RedirectOptions = new RedirectOptions { ForwardBody = true }
280-
);
281-
282-
var request = new RestRequest("/redirect-with-status?status=307", Method.Post);
283-
request.AddJsonBody(new { data = "keep-me" });
284-
285-
var response = await client.ExecuteAsync(request);
286-
287-
response.StatusCode.Should().Be(HttpStatusCode.OK);
288-
var doc = JsonDocument.Parse(response.Content!);
289-
doc.RootElement.GetProperty("Method").GetString().Should().Be("POST");
290-
doc.RootElement.GetProperty("Body").GetString().Should().Contain("keep-me");
271+
if (forwardBody)
272+
doc.RootElement.GetProperty("Body").GetString().Should().Contain("test-body");
273+
else
274+
doc.RootElement.GetProperty("Body").GetString().Should().BeEmpty();
291275
}
292276

293277
// ─── ForwardQuery ───────────────────────────────────────────────────

test/RestSharp.Tests.Shared/Server/WireMockTestServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static ResponseMessage RedirectWithStatus(IRequestMessage request) {
161161
};
162162
}
163163

164-
static ResponseMessage EchoRequest(IRequestMessage request) {
164+
public static ResponseMessage EchoRequest(IRequestMessage request) {
165165
var headers = request.Headers?
166166
.ToDictionary(x => x.Key, x => string.Join(", ", x.Value))
167167
?? new Dictionary<string, string>();

0 commit comments

Comments
 (0)