Skip to content

Commit 6764396

Browse files
committed
Start the process of testing the new RedirectOptions..
1 parent 9d8baf5 commit 6764396

File tree

5 files changed

+115
-11
lines changed

5 files changed

+115
-11
lines changed

src/RestSharp/Options/RestClientRedirectionOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public class RestClientRedirectionOptions {
3636

3737
/// <summary>
3838
/// Set to true (default), when you want to include cookies from the
39-
/// CookieContainer on the redirected URL.
39+
/// <see cref="CookieContainer"/> on the redirected URL.
4040
/// </summary>
4141
/// <remarks>
4242
/// NOTE: The exact cookies sent to the redirected url DEPENDS directly
4343
/// on the redirected url. A redirection to a completly differnet FQDN
4444
/// for example is unlikely to actually propagate any cookies from the
45-
/// CookieContqainer.
45+
/// <see cref="CookieContainer"/>.
4646
/// </remarks>
4747
public bool ForwardCookies { get; set; } = true;
4848

src/RestSharp/RestClient.Async.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,24 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
210210

211211
url = location;
212212

213-
if (responseMessage.Headers.TryGetValues(KnownHeaders.SetCookie, out var cookiesHeader1)) {
214-
foundCookies = true;
215-
// ReSharper disable once PossibleMultipleEnumeration
216-
cookieContainer.AddCookies(url, cookiesHeader1);
217-
// ReSharper disable once PossibleMultipleEnumeration
218-
Options.CookieContainer?.AddCookies(url, cookiesHeader1);
213+
if (Options.RedirectOptions.ForwardHeaders) {
214+
if (!Options.RedirectOptions.ForwardAuthorization) {
215+
headers.Parameters.RemoveParameter("Authorization");
216+
}
217+
if (!Options.RedirectOptions.ForwardCookies) {
218+
headers.Parameters.RemoveParameter("Cookie");
219+
}
220+
else {
221+
if (responseMessage.Headers.TryGetValues(KnownHeaders.SetCookie, out var cookiesHeader1)) {
222+
foundCookies = true;
223+
// ReSharper disable once PossibleMultipleEnumeration
224+
cookieContainer.AddCookies(url, cookiesHeader1);
225+
// ReSharper disable once PossibleMultipleEnumeration
226+
Options.CookieContainer?.AddCookies(url, cookiesHeader1);
227+
}
228+
}
219229
}
230+
220231
} while (true);
221232

222233
// Parse all the cookies from the response and update the cookie jar with cookies
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using RestSharp.Tests.Integrated.Server;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace RestSharp.Tests.Integrated {
9+
[Collection(nameof(TestServerCollection))]
10+
public class RedirectOptionsTest {
11+
readonly string _host;
12+
readonly Uri _baseUri;
13+
14+
public RedirectOptionsTest(TestServerFixture fixture) {
15+
_baseUri = fixture.Server.Url;
16+
_host = _baseUri.Host;
17+
}
18+
19+
RestClientOptions NewOptions() {
20+
return new RestClientOptions(_baseUri);
21+
}
22+
23+
[Fact]
24+
public async Task Can_RedirectForwardHeadersFalse_DropHeaders() {
25+
var options = NewOptions();
26+
options.RedirectOptions.ForwardHeaders = false;
27+
var client = new RestClient(options);
28+
29+
// This request sets cookies and redirects to url param value
30+
// if supplied, otherwise redirects to /get-cookies
31+
var request = new RestRequest("/get-cookies-redirect") {
32+
Method = Method.Get,
33+
};
34+
request.AddQueryParameter("url", "/dump-headers");
35+
36+
var response = await client.ExecuteAsync(request);
37+
response.ResponseUri.Should().Be($"{_baseUri}dump-headers");
38+
var content = response.Content;
39+
content.Should().NotContain("'Accept':");
40+
content.Should().NotContain("'Host': ");
41+
content.Should().NotContain("'User-Agent':");
42+
content.Should().NotContain("'Accept-Encoding':");
43+
content.Should().NotContain("'Cookie':");
44+
45+
// Verify the cookie exists from the redirected get:
46+
response.Cookies.Count.Should().BeGreaterThan(0).And.Be(1);
47+
response.Cookies[0].Name.Should().Be("redirectCookie");
48+
response.Cookies[0].Value.Should().Be("value1");
49+
}
50+
51+
}
52+
}

test/RestSharp.Tests.Integrated/RedirectTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//
1515

1616
using System.Net;
17+
using RestSharp.Extensions;
1718
using RestSharp.Tests.Integrated.Server;
1819

1920
namespace RestSharp.Tests.Integrated;
@@ -99,6 +100,30 @@ public async Task Can_Perform_PUT_Async_With_RedirectionResponse_Cookies() {
99100
response.StatusCode.Should().Be(HttpStatusCode.MethodNotAllowed);
100101
}
101102

103+
[Fact]
104+
public async Task Can_ForwardHeadersTrue_OnRedirect() {
105+
// This request sets cookies and redirects to url param value
106+
// if supplied, otherwise redirects to /get-cookies
107+
var request = new RestRequest("/get-cookies-redirect") {
108+
Method = Method.Get,
109+
};
110+
request.AddQueryParameter("url", "/dump-headers");
111+
112+
var response = await _client.ExecuteAsync(request);
113+
response.ResponseUri.Should().Be($"{_client.Options.BaseUrl}dump-headers");
114+
var content = response.Content;
115+
content.Should().Contain("'Accept':");
116+
content.Should().Contain($"'Host': {_client.Options.BaseHost}");
117+
content.Should().Contain("'User-Agent':");
118+
content.Should().Contain("'Accept-Encoding':");
119+
content.Should().Contain("'Cookie':");
120+
121+
// Verify the cookie exists from the redirected get:
122+
response.Cookies.Count.Should().BeGreaterThan(0).And.Be(1);
123+
response.Cookies[0].Name.Should().Be("redirectCookie");
124+
response.Cookies[0].Value.Should().Be("value1");
125+
}
126+
102127
// Needed tests:
103128
//Test: ForwardHeaders = false
104129
//Test: ForwardHeaders = true (default) might not need separate test
@@ -112,7 +137,7 @@ public async Task Can_Perform_PUT_Async_With_RedirectionResponse_Cookies() {
112137
//Test: ForwardQuery = true (default, might not need test)
113138
//Test: ForwardQuery = false
114139
//Test: MaxRedirects
115-
//Test: ForwardFragment = true
140+
//Test: ForwardFragment = true (default)
116141
//Test: ForwardFragment = false
117142
//Test: AllowRedirectMethodStatusCodeToAlterVerb = true (default, might not need test)
118143
//Test: AllowRedirectMethodStatusCodeToAlterVerb = false

test/RestSharp.Tests.Integrated/Server/TestServer.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using RestSharp.Tests.Integrated.Server.Handlers;
77
using RestSharp.Tests.Shared.Extensions;
88
using System.Net;
9+
using System.Text;
10+
using System.Web;
911

1012
// ReSharper disable ConvertClosureToMethodGroup
1113

@@ -38,6 +40,15 @@ public HttpServer(ITestOutputHelper? output = null) {
3840
_app.MapGet("headers", HeaderHandlers.HandleHeaders);
3941
_app.MapGet("request-echo", async context => await context.Request.BodyReader.AsStream().CopyToAsync(context.Response.BodyWriter.AsStream()));
4042
_app.MapDelete("delete", () => new TestResponse { Message = "Works!" });
43+
_app.MapGet("dump-headers",
44+
(HttpContext ctx) => {
45+
var headers = ctx.Request.Headers;
46+
StringBuilder sb = new StringBuilder();
47+
foreach (var kvp in headers) {
48+
sb.Append($"'{kvp.Key}': '{kvp.Value}',");
49+
}
50+
return new TestResponse { Message = sb.ToString() };
51+
});
4152

4253
// Cookies
4354
_app.MapGet("get-cookies", CookieHandlers.HandleCookies);
@@ -48,12 +59,17 @@ public HttpServer(ITestOutputHelper? output = null) {
4859
});
4960
_app.MapGet("set-cookies", CookieHandlers.HandleSetCookies);
5061
_app.MapGet("redirect", () => Results.Redirect("/success", false, true));
51-
5262
_app.MapGet(
5363
"get-cookies-redirect",
5464
(HttpContext ctx) => {
5565
ctx.Response.Cookies.Append("redirectCookie", "value1");
56-
return Results.Redirect("/get-cookies", false, true);
66+
string redirectDestination = "/get-cookies";
67+
var queryString = HttpUtility.ParseQueryString(ctx.Request.QueryString.Value);
68+
var urlParameter = queryString.Get("url");
69+
if (!string.IsNullOrEmpty(urlParameter)) {
70+
redirectDestination = urlParameter;
71+
}
72+
return Results.Redirect(redirectDestination, false, true);
5773
}
5874
);
5975

0 commit comments

Comments
 (0)