Skip to content
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

HTTP server NoOpRequestDecoder #659

Merged
merged 1 commit into from
Feb 5, 2018
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
6 changes: 6 additions & 0 deletions transport/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ type ErrorEncoder func(ctx context.Context, err error, w http.ResponseWriter)
// provided in the context under keys with the ContextKeyResponse prefix.
type ServerFinalizerFunc func(ctx context.Context, code int, r *http.Request)

// NopRequestDecoder is a DecodeRequestFunc that can be used for requests that do not
// need to be decoded, and simply returns nil, nil.
func NopRequestDecoder(ctx context.Context, r *http.Request) (interface{}, error) {
return nil, nil
}

// EncodeJSONResponse is a EncodeResponseFunc that serializes the response as a
// JSON object to the ResponseWriter. Many JSON-over-HTTP services can use it as
// a sensible default. If the response implements Headerer, the provided headers
Expand Down
22 changes: 22 additions & 0 deletions transport/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,28 @@ func TestEnhancedError(t *testing.T) {
}
}

func TestNoOpRequestDecoder(t *testing.T) {
resw := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Error("Failed to create request")
}
handler := httptransport.NewServer(
func(ctx context.Context, request interface{}) (interface{}, error) {
if request != nil {
t.Error("Expected nil request in endpoint when using NopRequestDecoder")
}
return nil, nil
},
httptransport.NopRequestDecoder,
httptransport.EncodeJSONResponse,
)
handler.ServeHTTP(resw, req)
if resw.Code != http.StatusOK {
t.Errorf("Expected status code %d but got %d", http.StatusOK, resw.Code)
}
}

func testServer(t *testing.T) (step func(), resp <-chan *http.Response) {
var (
stepch = make(chan bool)
Expand Down