|
|
@ -3,6 +3,7 @@ package rest
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync/atomic"
|
|
|
|
"sync/atomic"
|
|
|
@ -17,18 +18,21 @@ import (
|
|
|
|
func TestNewEngine(t *testing.T) {
|
|
|
|
func TestNewEngine(t *testing.T) {
|
|
|
|
yamls := []string{
|
|
|
|
yamls := []string{
|
|
|
|
`Name: foo
|
|
|
|
`Name: foo
|
|
|
|
Port: 54321
|
|
|
|
Host: localhost
|
|
|
|
|
|
|
|
Port: 0
|
|
|
|
Middlewares:
|
|
|
|
Middlewares:
|
|
|
|
Log: false
|
|
|
|
Log: false
|
|
|
|
`,
|
|
|
|
`,
|
|
|
|
`Name: foo
|
|
|
|
`Name: foo
|
|
|
|
Port: 54321
|
|
|
|
Host: localhost
|
|
|
|
|
|
|
|
Port: 0
|
|
|
|
CpuThreshold: 500
|
|
|
|
CpuThreshold: 500
|
|
|
|
Middlewares:
|
|
|
|
Middlewares:
|
|
|
|
Log: false
|
|
|
|
Log: false
|
|
|
|
`,
|
|
|
|
`,
|
|
|
|
`Name: foo
|
|
|
|
`Name: foo
|
|
|
|
Port: 54321
|
|
|
|
Host: localhost
|
|
|
|
|
|
|
|
Port: 0
|
|
|
|
CpuThreshold: 500
|
|
|
|
CpuThreshold: 500
|
|
|
|
Verbose: true
|
|
|
|
Verbose: true
|
|
|
|
`,
|
|
|
|
`,
|
|
|
@ -150,22 +154,29 @@ Verbose: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, yaml := range yamls {
|
|
|
|
for _, yaml := range yamls {
|
|
|
|
|
|
|
|
yaml := yaml
|
|
|
|
for _, route := range routes {
|
|
|
|
for _, route := range routes {
|
|
|
|
var cnf RestConf
|
|
|
|
route := route
|
|
|
|
assert.Nil(t, conf.LoadFromYamlBytes([]byte(yaml), &cnf))
|
|
|
|
t.Run(fmt.Sprintf("%s-%v", yaml, route.routes), func(t *testing.T) {
|
|
|
|
ng := newEngine(cnf)
|
|
|
|
var cnf RestConf
|
|
|
|
ng.addRoutes(route)
|
|
|
|
assert.Nil(t, conf.LoadFromYamlBytes([]byte(yaml), &cnf))
|
|
|
|
ng.use(func(next http.HandlerFunc) http.HandlerFunc {
|
|
|
|
ng := newEngine(cnf)
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ng.addRoutes(route)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
ng.use(func(next http.HandlerFunc) http.HandlerFunc {
|
|
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert.NotNil(t, ng.start(mockedRouter{}, func(svr *http.Server) {
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
timeout := time.Second * 3
|
|
|
|
|
|
|
|
if route.timeout > timeout {
|
|
|
|
|
|
|
|
timeout = route.timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, timeout, ng.timeout)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
assert.NotNil(t, ng.start(mockedRouter{}))
|
|
|
|
|
|
|
|
timeout := time.Second * 3
|
|
|
|
|
|
|
|
if route.timeout > timeout {
|
|
|
|
|
|
|
|
timeout = route.timeout
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, timeout, ng.timeout)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -340,7 +351,8 @@ func TestEngine_withTimeout(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type mockedRouter struct{}
|
|
|
|
type mockedRouter struct {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m mockedRouter) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {
|
|
|
|
func (m mockedRouter) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|