Skip to content

Commit

Permalink
test: router handler & handler func (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
abemedia authored May 7, 2023
1 parent 0bf9383 commit fe7c7eb
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions internal/test/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"net/http"
"strings"
"testing"

Expand All @@ -26,14 +27,23 @@ func Router(t *testing.T, r don.Router, handler fasthttp.RequestHandler, basePat
{"Handle", fasthttp.MethodGet, func(path string, handle httprouter.Handle) {
r.Handle(fasthttp.MethodGet, path, handle)
}},
{"Handler", fasthttp.MethodGet, func(path string, handle httprouter.Handle) {
r.Handler(fasthttp.MethodGet, path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Handler"))
}))
}},
{"HandleFunc", fasthttp.MethodGet, func(path string, handle httprouter.Handle) {
r.HandleFunc(fasthttp.MethodGet, path, func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("HandleFunc"))
})
}},
}

for _, test := range tests {
path := "/" + strings.ToLower(test.desc)

var ok bool
test.fn(path, func(ctx *fasthttp.RequestCtx, p httprouter.Params) {
ok = true
_, _ = ctx.WriteString(test.desc)
})

ctx := httptest.NewRequest(test.method, basePath+path, "", nil)
Expand All @@ -43,7 +53,7 @@ func Router(t *testing.T, r don.Router, handler fasthttp.RequestHandler, basePat
t.Errorf("%s request should return success status: %s", test.desc, fasthttp.StatusMessage(code))
}

if !ok {
if string(ctx.Response.Body()) != test.desc {
t.Errorf("%s request should reach handler", test.desc)
}
}
Expand Down

0 comments on commit fe7c7eb

Please sign in to comment.