mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
add ServeHTTP to Server/Engin for doing Httptest (#2704)
This commit is contained in:
parent
cf6c349118
commit
3e9d0161bc
@ -307,3 +307,8 @@ func newCorsRouter(router httpx.Router, headerFn func(http.Header), origins ...s
|
||||
func (c *corsRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
c.middleware(c.Router.ServeHTTP)(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s.ngin.bindRoutes(s.router)
|
||||
s.router.ServeHTTP(w, r)
|
||||
}
|
||||
|
@ -535,3 +535,91 @@ func TestServer_WithCors(t *testing.T) {
|
||||
cr.ServeHTTP(httptest.NewRecorder(), req)
|
||||
assert.Equal(t, int32(0), atomic.LoadInt32(&called))
|
||||
}
|
||||
|
||||
func TestServer_ServeHTTP(t *testing.T) {
|
||||
const configYaml = `
|
||||
Name: foo
|
||||
Port: 54321
|
||||
`
|
||||
|
||||
var cnf RestConf
|
||||
assert.Nil(t, conf.LoadFromYamlBytes([]byte(configYaml), &cnf))
|
||||
|
||||
svr, err := NewServer(cnf)
|
||||
assert.Nil(t, err)
|
||||
|
||||
svr.AddRoutes([]Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/foo",
|
||||
Handler: func(writer http.ResponseWriter, request *http.Request) {
|
||||
_, _ = writer.Write([]byte("succeed"))
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
},
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/bar",
|
||||
Handler: func(writer http.ResponseWriter, request *http.Request) {
|
||||
_, _ = writer.Write([]byte("succeed"))
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
},
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/user/:name",
|
||||
Handler: func(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
var userInfo struct {
|
||||
Name string `path:"name"`
|
||||
}
|
||||
|
||||
err := httpx.Parse(request, &userInfo)
|
||||
if err != nil {
|
||||
_, _ = writer.Write([]byte("failed"))
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = writer.Write([]byte("succeed"))
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
testCase := []struct {
|
||||
name string
|
||||
path string
|
||||
code int
|
||||
}{
|
||||
{
|
||||
name: "URI : /foo",
|
||||
path: "/foo",
|
||||
code: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "URI : /bar",
|
||||
path: "/bar",
|
||||
code: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "URI : undefined path",
|
||||
path: "/test",
|
||||
code: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "URI : /user/:name",
|
||||
path: "/user/abc",
|
||||
code: http.StatusOK,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCase {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", test.path, nil)
|
||||
svr.ServeHTTP(w, req)
|
||||
assert.Equal(t, test.code, w.Code)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user