-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
87 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package skip 是否根据条件跳过路由的执行 | ||
package skip | ||
|
||
import "github.com/issue9/web" | ||
|
||
// New 只有在 cond 为 true 时才执行路由 | ||
// | ||
// problemID 表示在条件为 false 时返回的错误码。 | ||
func New(cond func(*web.Context) bool, problemID string) web.Middleware { | ||
return web.MiddlewareFunc(func(next web.HandlerFunc) web.HandlerFunc { | ||
return func(ctx *web.Context) web.Responser { | ||
if cond(ctx){ | ||
return next(ctx) | ||
} | ||
return ctx.Problem(problemID) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package skip | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/issue9/assert/v4" | ||
"github.com/issue9/web" | ||
"github.com/issue9/web/server" | ||
"github.com/issue9/web/server/servertest" | ||
) | ||
|
||
func TestSkip(t *testing.T) { | ||
a := assert.New(t, false) | ||
|
||
s, err := server.New("test", "1.0.0", &server.Options{ | ||
HTTPServer: &http.Server{Addr: ":8080"}, | ||
Mimetypes: server.JSONMimetypes(), | ||
Logs: &server.Logs{Handler: server.NewTermHandler(os.Stderr, nil)}, | ||
}) | ||
a.NotError(err).NotNil(s) | ||
|
||
next := func(ctx *web.Context) web.Responser { | ||
return web.Created(nil, "") | ||
} | ||
|
||
router := s.Routers().New("def", nil) | ||
router.Any("/test", New(func(ctx *web.Context) bool { return ctx.Request().Method != http.MethodHead }, web.ProblemBadRequest).Middleware(next)) | ||
|
||
defer servertest.Run(a, s)() | ||
defer s.Close(0) | ||
|
||
servertest.Get(a, "http://localhost:8080/test"). | ||
Header("X-Forwarded-For", "192.168.1.1"). | ||
Do(nil). | ||
Status(http.StatusCreated) | ||
|
||
servertest.NewRequest(a, http.MethodHead, "http://localhost:8080/test"). | ||
Header("X-Forwarded-For", "192.168.1.1"). | ||
Do(nil). | ||
Status(http.StatusBadRequest) | ||
} |