Constantin Veretennicov avatar Constantin Veretennicov committed ca320f7 Draft

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;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.