Skip to content

Commit

Permalink
test(handlers): passing more arguments to handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vinyguedess committed Jun 6, 2022
1 parent 3922c0f commit 985af75
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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))
}
```

Expand Down
2 changes: 1 addition & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
25 changes: 25 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 985af75

Please sign in to comment.