Skip to content

Commit

Permalink
Merge pull request #53 from joegasewicz/Tests_#3
Browse files Browse the repository at this point in the history
Tests #3
  • Loading branch information
joegasewicz authored May 1, 2023
2 parents 5fe2770 + 00d15b1 commit 96c4e0f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,30 @@ app := gomek.New(gomek.Config{})
publicFiles := http.FileServer(http.Dir("public"))
app.Handle("/public/", http.StripPrefix("/public/", publicFiles))
```
# Testing
Gomek provides a testing utility that returns a regular `HandlerFunc`
```go
c := Config{}
mockApp := NewTestApp(c)
mockApp.Route("/blogs").Methods("POST").Resource(&Notice{})
mockApp.Start()

notice := Notice{}

handler := CreateTestHandler(mockApp, notice.Post)

req := httptest.NewRequest(http.MethodPost, "/blogs", nil)
w := httptest.NewRecorder()
handler(w, req) // regular HandlerFunc
resp := w.Result()

defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error: %v", err)
}
expected := `{"name":"Joe"}`
if string(data) != expected {
t.Errorf("Expected %s got '%v'", expected, string(data))
```

0 comments on commit 96c4e0f

Please sign in to comment.