Skip to content

Commit

Permalink
#623 trim header values
Browse files Browse the repository at this point in the history
  • Loading branch information
tmenier committed May 13, 2022
1 parent a03c8ba commit d6f3bcc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Test/Flurl.Test/Http/SettingsExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ public void header_names_are_case_insensitive() {
Assert.AreEqual("2", sc.Headers.Single().Value);
}

[Test] // #623
public async Task header_values_are_trimmed() {
var sc = GetSettingsContainer().WithHeader("a", " 1 \t\r\n");
sc.Headers.Add("b", " 2 ");

Assert.AreEqual(2, sc.Headers.Count);
Assert.AreEqual("1", sc.Headers[0].Value);
// Not trimmed when added directly to Headers collection (implementation seemed like overkill),
// but below we'll make sure it happens on HttpRequestMessage when request is sent.
Assert.AreEqual(" 2 ", sc.Headers[1].Value);

using (var test = new HttpTest()) {
await GetRequest(sc).GetAsync();
var sentHeaders = test.CallLog[0].HttpRequestMessage.Headers;
Assert.AreEqual("1", sentHeaders.GetValues("a").Single());
Assert.AreEqual("2", sentHeaders.GetValues("b").Single());
}
}

[Test]
public void can_setup_oauth_bearer_token() {
var sc = GetSettingsContainer().WithOAuthBearerToken("mytoken");
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http/FlurlRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void SyncHeaders(HttpRequestMessage request) {

// copy headers from FlurlRequest to HttpRequestMessage
foreach (var header in Headers)
request.SetHeader(header.Name, header.Value, false);
request.SetHeader(header.Name, header.Value.Trim(), false);

// copy headers from HttpContent to FlurlRequest
if (request.Content != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Flurl.Http/HeaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static T WithHeader<T>(this T clientOrRequest, string name, object value)
if (value == null)
clientOrRequest.Headers.Remove(name);
else
clientOrRequest.Headers.AddOrReplace(name, value.ToInvariantString());
clientOrRequest.Headers.AddOrReplace(name, value.ToInvariantString().Trim());
return clientOrRequest;
}

Expand Down

0 comments on commit d6f3bcc

Please sign in to comment.