Skip to content

Commit a248fca

Browse files
alexeyzimarevclaude
andcommitted
Consolidate cross-host auth tests into parameterized Theory to reduce duplication
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 19af2d4 commit a248fca

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

test/RestSharp.Tests.Integrated/CookieRedirectTests.cs

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -183,45 +183,13 @@ public async Task ForwardAuthorization_Controls_Auth_Header_Forwarding(bool forw
183183
response.Content.Should().NotContain("Bearer test-token");
184184
}
185185

186-
[Fact]
187-
public async Task ForwardAuthorization_Should_Strip_Auth_On_Cross_Host_Redirect_By_Default() {
188-
// Create a second server (different host/port) that echoes request details
189-
using var externalServer = WireMockServer.Start();
190-
externalServer
191-
.Given(Request.Create().WithPath("/echo-request"))
192-
.RespondWith(Response.Create().WithCallback(request => {
193-
var headers = request.Headers?
194-
.ToDictionary(x => x.Key, x => string.Join(", ", x.Value))
195-
?? new Dictionary<string, string>();
196-
return WireMockTestServer.CreateJson(new { Method = request.Method, Headers = headers, Body = request.Body ?? "" });
197-
}));
198-
199-
// Configure the main server to redirect to the external server
200-
server.Given(Request.Create().WithPath("/redirect-external"))
201-
.RespondWith(Response.Create().WithCallback(_ => new ResponseMessage {
202-
StatusCode = 302,
203-
Headers = new Dictionary<string, WireMockList<string>> {
204-
["Location"] = new(externalServer.Url + "/echo-request")
205-
}
206-
}));
207-
208-
using var client = CreateClient(o =>
209-
o.RedirectOptions = new RedirectOptions { ForwardAuthorization = true }
210-
);
211-
212-
var request = new RestRequest("/redirect-external");
213-
request.AddHeader("Authorization", "Bearer secret-token");
214-
215-
var response = await client.ExecuteAsync(request);
216-
217-
response.StatusCode.Should().Be(HttpStatusCode.OK);
218-
response.Content.Should().NotContain("Bearer secret-token",
219-
"Authorization should be stripped on cross-host redirects by default");
220-
}
221-
222-
[Fact]
223-
public async Task ForwardAuthorizationToExternalHost_Allows_Auth_On_Cross_Host_Redirect() {
224-
// Create a second server (different host/port) that echoes request details
186+
[Theory]
187+
[InlineData(false, false)]
188+
[InlineData(true, true)]
189+
public async Task ForwardAuthorizationToExternalHost_Controls_Cross_Origin_Auth(
190+
bool allowExternal, bool expectAuth
191+
) {
192+
// Create a second server (different port = different origin) with echo endpoint
225193
using var externalServer = WireMockServer.Start();
226194
externalServer
227195
.Given(Request.Create().WithPath("/echo-request"))
@@ -232,8 +200,9 @@ public async Task ForwardAuthorizationToExternalHost_Allows_Auth_On_Cross_Host_R
232200
return WireMockTestServer.CreateJson(new { Method = request.Method, Headers = headers, Body = request.Body ?? "" });
233201
}));
234202

235-
// Configure the main server to redirect to the external server
236-
server.Given(Request.Create().WithPath("/redirect-external-auth"))
203+
// Main server redirects to the external server
204+
var redirectPath = $"/redirect-external-{allowExternal}";
205+
server.Given(Request.Create().WithPath(redirectPath))
237206
.RespondWith(Response.Create().WithCallback(_ => new ResponseMessage {
238207
StatusCode = 302,
239208
Headers = new Dictionary<string, WireMockList<string>> {
@@ -244,18 +213,21 @@ public async Task ForwardAuthorizationToExternalHost_Allows_Auth_On_Cross_Host_R
244213
using var client = CreateClient(o =>
245214
o.RedirectOptions = new RedirectOptions {
246215
ForwardAuthorization = true,
247-
ForwardAuthorizationToExternalHost = true
216+
ForwardAuthorizationToExternalHost = allowExternal
248217
}
249218
);
250219

251-
var request = new RestRequest("/redirect-external-auth");
220+
var request = new RestRequest(redirectPath);
252221
request.AddHeader("Authorization", "Bearer secret-token");
253222

254223
var response = await client.ExecuteAsync(request);
255224

256225
response.StatusCode.Should().Be(HttpStatusCode.OK);
257-
response.Content.Should().Contain("Bearer secret-token",
258-
"Authorization should be forwarded when ForwardAuthorizationToExternalHost is true");
226+
227+
if (expectAuth)
228+
response.Content.Should().Contain("Bearer secret-token");
229+
else
230+
response.Content.Should().NotContain("Bearer secret-token");
259231
}
260232

261233
[Fact]

0 commit comments

Comments
 (0)