Commits

Constantin Veretennicov committed ca320f7

Dispatch on empty value the same as on "*/*", to conform to RFC 2616.

Comments (0)

Files changed (3)

                     http.Response.Code = 406; // "Not Acceptable"
                 })
             .Build()
-            .Dispatch(http.Request.Headers["Accept"] ?? "*/*");
+            .Dispatch(http.Request.Headers["Accept"]);
     }
 
 
         public void Dispatch(string acceptHeaderValue)
         {
+            // RFC 2616 states that "If no Accept header field is present,
+            // then it is assumed that the client accepts all media types".
+            var v = acceptHeaderValue == string.Empty
+                ? "*/*"
+                : acceptHeaderValue;
+            this.doDispatch(v);
+        }
+
+        void doDispatch(string acceptHeaderValue)
+        {
             // Parse media type preferences from client request.
             var specifiedMediaRanges = AcceptHeader.Parse(acceptHeaderValue);
             // Prepare available media types for scoring based on client's

tests/CsConneg.DispatcherTests.cs

             Assert.IsTrue(noMatch);
         }
 
+        [Test] public void Match_first_handler_due_to_empty_accept_header_being_interpreted_as_wildcard_accept()
+        {
+            var matchedCorrectHandler = false;
+            new CsConneg.Builder()
+                .MediaHandler(
+                    "application/atom+xml;type=feed", mr =>
+                    {
+                        matchedCorrectHandler = true;
+                    })
+                .MediaHandler(
+                    "application/atom+xml", mr =>
+                    {
+                        Assert.Fail("Wrong handler selected");
+                    })
+                .NoMatchHandler(
+                    delegate
+                    {
+                        Assert.Fail("No-match handler incorrectly selected");
+                    })
+                .Build()
+                .Dispatch("");
+
+            Assert.That(matchedCorrectHandler);
+        }
+
         [Test] public void Match_one_of_handlers_by_exact_match_given_single_choice()
         {
             MediaRange matchedMediaRange = null;