Skip to content

Commit

Permalink
List available representations in 406 body (#437)
Browse files Browse the repository at this point in the history
Becasue RFC7231 says we should.
  • Loading branch information
brackendawson authored Aug 19, 2020
1 parent b42b5d7 commit d9ee3c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion jsr311.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions web_service_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package restful

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit d9ee3c6

Please sign in to comment.