Fix redirection when scheme is missing

This commit is contained in:
AeonLucid 2021-01-24 22:50:07 +01:00
parent 188999bca4
commit ec6c9ea2ea
2 changed files with 6 additions and 1 deletions

View File

@ -18,12 +18,13 @@ namespace Flurl.Test.Http
.RespondWith("", 302, new { Location = "http://redir.com/foo" })
.RespondWith("", 302, new { Location = "/redir2" })
.RespondWith("", 302, new { Location = "redir3?x=1&y=2#foo" })
.RespondWith("", 302, new { Location = "//otherredir.com/bar/?a=b" })
.RespondWith("done!");
var resp = await "http://start.com".PostStringAsync("foo!").ReceiveString();
Assert.AreEqual("done!", resp);
HttpTest.ShouldHaveMadeACall().Times(4);
HttpTest.ShouldHaveMadeACall().Times(5);
HttpTest.ShouldHaveCalled("http://start.com").WithVerb(HttpMethod.Post).WithRequestBody("foo!")
.With(call => call.RedirectedFrom == null);
HttpTest.ShouldHaveCalled("http://redir.com/foo").WithVerb(HttpMethod.Get).WithRequestBody("")
@ -32,6 +33,8 @@ namespace Flurl.Test.Http
.With(call => call.RedirectedFrom.Request.Url.ToString() == "http://redir.com/foo");
HttpTest.ShouldHaveCalled("http://redir.com/redir2/redir3?x=1&y=2#foo").WithVerb(HttpMethod.Get).WithRequestBody("")
.With(call => call.RedirectedFrom.Request.Url.ToString() == "http://redir.com/redir2");
HttpTest.ShouldHaveCalled("http://otherredir.com/bar/?a=b").WithVerb(HttpMethod.Get).WithRequestBody("")
.With(call => call.RedirectedFrom.Request.Url.ToString() == "http://redir.com/redir2/redir3?x=1&y=2#foo");
}
[Test]

View File

@ -282,6 +282,8 @@ namespace Flurl.Http
if (Url.IsValid(location))
redir.Url = new Url(location);
else if (location.OrdinalStartsWith("//"))
redir.Url = new Url(this.Url.Scheme + ":" + location);
else if (location.OrdinalStartsWith("/"))
redir.Url = Url.Combine(this.Url.Root, location);
else