-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from sorenmat/echo
adding echo integration
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package integration | ||
|
||
import ( | ||
"mime/multipart" | ||
|
||
"github.com/ggicci/httpin/core" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
// UseEchoRouter registers a new directive executor which can extract values | ||
// from path variables. | ||
// https://ggicci.github.io/httpin/integrations/echo | ||
// | ||
// Usage: | ||
// | ||
// import httpin_integration "github.com/ggicci/httpin/integration" | ||
// | ||
// func init() { | ||
// e := echo.New() | ||
// httpin_integration.UseEchoRouter("path", e) | ||
// | ||
// // or | ||
// | ||
// httpin_integration.UseEchoPathRouter(e) | ||
// } | ||
func UseEchoRouter(name string, e *echo.Echo) { | ||
core.RegisterDirective( | ||
name, | ||
core.NewDirectivePath((&echoRouterExtractor{e}).Execute), | ||
true, | ||
) | ||
} | ||
|
||
func UseEchoPathRouter(e *echo.Echo) { | ||
UseEchoRouter("path", e) | ||
} | ||
|
||
// echoRouterExtractor is an extractor for mux.Vars | ||
type echoRouterExtractor struct { | ||
e *echo.Echo | ||
} | ||
|
||
func (mux *echoRouterExtractor) Execute(rtm *core.DirectiveRuntime) error { | ||
req := rtm.GetRequest() | ||
kvs := make(map[string][]string) | ||
|
||
c := mux.e.NewContext(req, nil) | ||
c.SetRequest(req) | ||
|
||
mux.e.Router().Find(req.Method, req.URL.Path, c) | ||
|
||
for _, key := range c.ParamNames() { | ||
kvs[key] = []string{c.Param(key)} | ||
} | ||
|
||
extractor := &core.FormExtractor{ | ||
Runtime: rtm, | ||
Form: multipart.Form{ | ||
Value: kvs, | ||
}, | ||
} | ||
return extractor.Extract() | ||
} |
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,49 @@ | ||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ggicci/httpin" | ||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type GetPostOfUserInput struct { | ||
Username string `in:"path=username"` | ||
PostID int64 `in:"path=pid"` | ||
} | ||
|
||
func TestUseEchoMux(t *testing.T) { | ||
|
||
e := echo.New() | ||
UseEchoPathRouter(e) | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/users/ggicci/posts/123", nil) | ||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
rec := httptest.NewRecorder() | ||
|
||
c := e.NewContext(req, rec) | ||
|
||
handler := func(ctx echo.Context) error { | ||
param := &GetPostOfUserInput{} | ||
core, err := httpin.New(param) | ||
if err != nil { | ||
return err | ||
} | ||
v, err := core.Decode(ctx.Request()) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(param) | ||
return c.JSON(http.StatusOK, v) | ||
} | ||
e.GET("/users/:username/posts/:pid", handler) | ||
err := handler(c) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, rec.Code) | ||
assert.Equal(t, `{"Username":"ggicci","PostID":123}`, strings.TrimSpace(rec.Body.String())) | ||
} |