diff --git a/README.md b/README.md index e15be02..f5f2bc0 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,14 @@ and don't require developer to worry about some things. First, we create a basic Gorrila/Mux API. ```go -func HomeHandler(request geh.Request, response geh.Response) { +func HomeHandler( + request geh.Request, + response geh.Response, + arguments ...interface{}, +) { + printSomething := arguments[0].(func (string)) + printSomething("print something") + geh.Status(http.StatusOk). Json(map[string]interface{}{ "hello": "I", @@ -23,8 +30,12 @@ func HomeHandler(request geh.Request, response geh.Response) { } func main() { + func printSomething(something string) { + print(something) + } + r := mux.NewRouter() - r.HandleFunc("/", geh.Handler(HomeHandler)) + r.HandleFunc("/", geh.Handler(HomeHandler, printSomething)) } ``` diff --git a/handler.go b/handler.go index 8206d12..1d4e06b 100644 --- a/handler.go +++ b/handler.go @@ -17,7 +17,7 @@ func Handler(handler EasyHandler, arguments ...interface{}) MuxHandler { request := NewRequest(httpRequest) response := NewResponse(responseWriter) - receivedResponse := handler(request, response, arguments) + receivedResponse := handler(request, response, arguments...) for key, value := range receivedResponse.GetHeaders() { responseWriter.Header().Set(key, value) } diff --git a/handler_test.go b/handler_test.go index 41e6804..9b304c7 100644 --- a/handler_test.go +++ b/handler_test.go @@ -29,3 +29,28 @@ func TestHandler(t *testing.T) { assert.Equal(t, "application/json", responseWriter.Header().Get("Content-Type")) assert.Equal(t, 200, responseWriter.Code) } + +func TestHandlerPassingArguments(t *testing.T) { + handler := Handler( + func( + request Request, response Response, arguments ...interface{}, + ) *Response { + returnsSameString := arguments[0].(func(string) string) + + return response.Json( + map[string]string{ + "hello": returnsSameString("world"), + }, + ) + }, + func(something string) string { return something }, + ) + + httpRequest, _ := http.NewRequest("GET", "/resource", nil) + responseWriter := httptest.NewRecorder() + handler(responseWriter, httpRequest) + + assert.Equal(t, "{\"hello\":\"world\"}", responseWriter.Body.String()) + assert.Equal(t, "application/json", responseWriter.Header().Get("Content-Type")) + assert.Equal(t, 200, responseWriter.Code) +}