forked from qiangxue/go-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
36 lines (32 loc) · 1020 Bytes
/
mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package test
import (
"net/http"
"net/http/httptest"
routing "github.com/go-ozzo/ozzo-routing/v2"
"github.com/go-ozzo/ozzo-routing/v2/content"
"github.com/go-ozzo/ozzo-routing/v2/cors"
"github.com/omid9h/go-rest-api/internal/errors"
"github.com/omid9h/go-rest-api/pkg/accesslog"
"github.com/omid9h/go-rest-api/pkg/log"
)
// MockRoutingContext creates a routing.Conext for testing handlers.
func MockRoutingContext(req *http.Request) (*routing.Context, *httptest.ResponseRecorder) {
res := httptest.NewRecorder()
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
}
ctx := routing.NewContext(res, req)
ctx.SetDataWriter(&content.JSONDataWriter{})
return ctx, res
}
// MockRouter creates a routing.Router for testing APIs.
func MockRouter(logger log.Logger) *routing.Router {
router := routing.New()
router.Use(
accesslog.Handler(logger),
errors.Handler(logger),
content.TypeNegotiator(content.JSON),
cors.Handler(cors.AllowAll),
)
return router
}