Skip to content

Commit

Permalink
Merge pull request #2 from go-spring-projects/test-testcase
Browse files Browse the repository at this point in the history
Export `Routes` interface on `Router`
  • Loading branch information
limpo1989 authored Dec 22, 2023
2 parents 2967416 + a33ae78 commit b1eea3d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
4 changes: 2 additions & 2 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ func JsonRender() RendererFunc {
}
}

type response struct {
type JsonResponse struct {
Code int `json:"code"`
Message string `json:"message,omitempty"`
Data interface{} `json:"data"`
}

ctx.JSON(http.StatusOK, response{Code: code, Message: message, Data: result})
_ = ctx.JSON(http.StatusOK, JsonResponse{Code: code, Message: message, Data: result})
}
}
8 changes: 8 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ type RouteContext struct {
methodsAllowed []methodTyp
}

// AllowedMethods report allowed http methods.
func (c *RouteContext) AllowedMethods() (methods []string) {
for _, m := range c.methodsAllowed {
methods = append(methods, methodTypString(m))
}
return
}

// Reset context to initial state
func (c *RouteContext) Reset() {
c.Routes = nil
Expand Down
12 changes: 8 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ type Middlewares []MiddlewareFunc
// Handler builds and returns a http.Handler from the chain of middlewares,
// with `h http.Handler` as the final handler.
func (mws Middlewares) Handler(h http.Handler) http.Handler {
return &chainHandler{Endpoint: h, chain: mws.chain(h), Middlewares: mws}
return &ChainHandler{Endpoint: h, chain: mws.chain(h), Middlewares: mws}
}

// HandlerFunc builds and returns a http.Handler from the chain of middlewares,
// with `h http.Handler` as the final handler.
func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler {
return &chainHandler{Endpoint: h, chain: mws.chain(h), Middlewares: mws}
return &ChainHandler{Endpoint: h, chain: mws.chain(h), Middlewares: mws}
}

// Build a http.Handler composed of an inline middlewares.
Expand All @@ -51,12 +51,16 @@ func (mws Middlewares) chain(handler http.Handler) http.Handler {
return handler
}

type chainHandler struct {
type ChainHandler struct {
Endpoint http.Handler
chain http.Handler
Middlewares Middlewares
}

func (c *chainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (c *ChainHandler) Unwrap() any {
return c.Endpoint
}

func (c *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.chain.ServeHTTP(w, r)
}
16 changes: 7 additions & 9 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,12 @@ func (options Options) TlsConfig() *tls.Config {
}

return &tls.Config{
GetCertificate: options.GetCertificate,
GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
if err != nil {
return nil, err
}
return &cert, nil
},
}
}

func (options Options) GetCertificate(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
if err != nil {
return nil, err
}
return &cert, nil
}
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import (
// http.ListenAndServe(":8080", router)
// }
type Router interface {
// Handler dispatches the handler registered in the matched route.
Routes
http.Handler

// Use appends a MiddlewareFunc to the chain.
Expand Down
15 changes: 15 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package web

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewServer(t *testing.T) {
svr := NewServer(Options{})
assert.NotNil(t, svr)
assert.Equal(t, ":8080", svr.Addr())
assert.Equal(t, false, svr.options.IsTls())
assert.Nil(t, svr.options.TlsConfig())
}
2 changes: 1 addition & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ func walk(r Routes, walkFn WalkFunc, parentRoute string, parentMw ...func(http.H
fullRoute := parentRoute + route.Pattern
fullRoute = strings.Replace(fullRoute, "/*/", "/", -1)

if chain, ok := handler.(*chainHandler); ok {
if chain, ok := handler.(*ChainHandler); ok {
if err := walkFn(method, fullRoute, chain.Endpoint, append(mws, chain.Middlewares...)...); err != nil {
return err
}
Expand Down

0 comments on commit b1eea3d

Please sign in to comment.