8000 Fixes "Misused header name" bug when using dynamic headers. by bennor · Pull Request #44 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixes "Misused header name" bug when using dynamic headers. #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Refit-Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Net.Http;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -228,6 +229,12 @@ public interface IDummyHttpApi
Task FetchSomeStuffWithVoid();

string SomeOtherMethod();

[Put("/foo/bar/{id}")]
Task PutSomeContentWithAuthorization(int id, [Body] object content, [Header("Authorization")] string authorization);

[Put("/foo/bar/{id}")]
Task<string> PutSomeStuffWithDynamicContentType(int id, [Body] string content, [Header("Content-Type")] string contentType);
}

public class TestHttpMessageHandler : HttpMessageHandler
Expand Down Expand Up @@ -423,5 +430,28 @@ public void HttpClientShouldNotPrefixEmptyAbsolutePathToTheRequestUri()

Assert.AreEqual("http://api/foo/bar/42", testHttpMessageHandler.RequestMessage.RequestUri.ToString());
}

[Test]
public void DontBlowUpWithDynamicAuthorizationHeaderAndContent()
{
var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi));
var factory = fixture.BuildRequestFactoryForMethod("PutSomeContentWithAuthorization");
var output = factory(new object[] { 7, new { Octocat = "Dunetocat" }, "Basic RnVjayB5ZWFoOmhlYWRlcnMh" });

Assert.IsNotNull(output.Headers.Authorization, "Headers include Authorization header");
Assert.AreEqual("RnVjayB5ZWFoOmhlYWRlcnMh", output.Headers.Authorization.Parameter);
}

[Test]
public void SuchFlexibleContentTypeWow()
{
var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi));
var factory = fixture.BuildRequestFactoryForMethod("PutSomeStuffWithDynamicContentType");
var output = factory(new object[] { 7, "such \"refit\" is \"amaze\" wow", "text/dson" });

Assert.IsNotNull(output.Content, "Request has content");
Assert.IsNotNull(output.Content.Headers.ContentType, "Headers include Content-Type header");
Assert.AreEqual("text/dson", output.Content.Headers.ContentType.MediaType, "Content-Type header has the expected value");
}
}
}
10 changes: 7 additions & 3 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ public Func<object[], HttpRequestMessage> BuildRequestFactoryForMethod(string me
void setHeader(HttpRequestMessage request, string name, object value)
{
// Clear any existing version of this header we may have set, because
// we want to allow removal/redefinition of headers.
request.Headers.Remove(name);
// we want to allow removal/redefinition of headers.

if (request.Content != null) {
// NB: We have to enumerate the header names to check existence because
// Contains throws if it's the wrong header type for the collection.
if (request.Headers.Any(x => x.Key == name)) {
request.Headers.Remove(name);
}
if (request.Content != null && request.Content.Headers.Any(x => x.Key == name)) {
request.Content.Headers.Remove(name);
}

Expand Down
0