diff --git a/jsr311.go b/jsr311.go index ee2d0bed..9cfd59a1 100644 --- a/jsr311.go +++ b/jsr311.go @@ -147,7 +147,14 @@ func (r RouterJSR311) detectRoute(routes []Route, httpRequest *http.Request) (*R if trace { traceLogger.Printf("no Route found (from %d) that matches HTTP Accept: %s\n", len(previous), accept) } - return nil, NewError(http.StatusNotAcceptable, "406: Not Acceptable") + available := []string{} + for _, candidate := range previous { + available = append(available, candidate.Produces...) + } + return nil, NewError( + http.StatusNotAcceptable, + fmt.Sprintf("406: Not Acceptable\n\nAvailable representations: %s", strings.Join(available, ", ")), + ) } // return r.bestMatchByMedia(outputMediaOk, contentType, accept), nil return candidates[0], nil diff --git a/web_service_test.go b/web_service_test.go index 3cd8d03a..dcd56ca1 100644 --- a/web_service_test.go +++ b/web_service_test.go @@ -1,6 +1,7 @@ package restful import ( + "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -104,6 +105,25 @@ func TestMethodNotAllowed_Issue435(t *testing.T) { } } +func TestNotAcceptable_Issue434(t *testing.T) { + tearDown() + Add(newGetPlainTextOrJsonService()) + httpRequest, _ := http.NewRequest("GET", "http://here.com/get", nil) + httpRequest.Header.Set("Accept", "application/toml") + httpWriter := httptest.NewRecorder() + DefaultContainer.dispatch(httpWriter, httpRequest) + if 406 != httpWriter.Code { + t.Error("406 expected not acceptable", httpWriter.Code) + } + expected := `406: Not Acceptable + +Available representations: text/plain, application/json` + body, _ := ioutil.ReadAll(httpWriter.Body) + if expected != string(body) { + t.Errorf("Expected body:\n%s\ngot:\n%s\n", expected, string(body)) + } +} + func TestSelectedRoutePath_Issue100(t *testing.T) { tearDown() Add(newSelectedRouteTestingService())