mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 01:57:55 +08:00
update readme and add test case
This commit is contained in:
parent
06e37a02ba
commit
91e3335932
35
README.md
35
README.md
@ -39,6 +39,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
|
|||||||
- [Custom Validators](#custom-validators)
|
- [Custom Validators](#custom-validators)
|
||||||
- [Only Bind Query String](#only-bind-query-string)
|
- [Only Bind Query String](#only-bind-query-string)
|
||||||
- [Bind Query String or Post Data](#bind-query-string-or-post-data)
|
- [Bind Query String or Post Data](#bind-query-string-or-post-data)
|
||||||
|
- [Bind Uri](#bind-uri)
|
||||||
- [Bind HTML checkboxes](#bind-html-checkboxes)
|
- [Bind HTML checkboxes](#bind-html-checkboxes)
|
||||||
- [Multipart/Urlencoded binding](#multiparturlencoded-binding)
|
- [Multipart/Urlencoded binding](#multiparturlencoded-binding)
|
||||||
- [XML, JSON, YAML and ProtoBuf rendering](#xml-json-yaml-and-protobuf-rendering)
|
- [XML, JSON, YAML and ProtoBuf rendering](#xml-json-yaml-and-protobuf-rendering)
|
||||||
@ -793,6 +794,40 @@ Test it with:
|
|||||||
$ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz&birthday=1992-03-15"
|
$ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz&birthday=1992-03-15"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Bind Uri
|
||||||
|
|
||||||
|
See the [detail information](https://github.com/gin-gonic/gin/issues/846).
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Id string `uri:"id" binding:"required,uuid"`
|
||||||
|
Name string `uri:"name" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
route := gin.Default()
|
||||||
|
route.GET("/:name/:id", func(c *gin.Context) {
|
||||||
|
var person Person
|
||||||
|
if err := c.ShouldBindUri(&person); err != nil {
|
||||||
|
c.JSON(400, gin.H{"msg": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{"name": person.Name, "uuid": person.Id})
|
||||||
|
})
|
||||||
|
route.Run(":8088")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Test it with:
|
||||||
|
```sh
|
||||||
|
$ curl -v localhost:8088/thinkerou/987fbc97-4bed-5078-9f07-9141ba07c9f3
|
||||||
|
$ curl -v localhost:8088/thinkerou/not-uuid
|
||||||
|
```
|
||||||
|
|
||||||
### Bind HTML checkboxes
|
### Bind HTML checkboxes
|
||||||
|
|
||||||
See the [detail information](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092)
|
See the [detail information](https://github.com/gin-gonic/gin/issues/129#issuecomment-124260092)
|
||||||
|
@ -655,7 +655,7 @@ func TestUriBinding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
var tag Tag
|
var tag Tag
|
||||||
params := internal.Params{internal.Param{Key: "name", Value: "thinkerou"}}
|
params := internal.Params{internal.Param{Key: "name", Value: "thinkerou"}}
|
||||||
assert.Nil(t, b.BindUri(params, &tag))
|
assert.NoError(t, b.BindUri(params, &tag))
|
||||||
assert.Equal(t, "thinkerou", tag.Name)
|
assert.Equal(t, "thinkerou", tag.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,8 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func mapUri(ptr interface{}, ps internal.Params) error {
|
func mapUri(ptr interface{}, ps internal.Params) error {
|
||||||
var m map[string][]string
|
m := make(map[string][]string)
|
||||||
m = make(map[string][]string)
|
|
||||||
for _, v := range ps {
|
for _, v := range ps {
|
||||||
m[v.Key] = []string{v.Value}
|
m[v.Key] = []string{v.Value}
|
||||||
}
|
}
|
||||||
|
@ -287,6 +287,27 @@ var githubAPI = []route{
|
|||||||
{"DELETE", "/user/keys/:id"},
|
{"DELETE", "/user/keys/:id"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShouldBindUri(t *testing.T) {
|
||||||
|
DefaultWriter = os.Stdout
|
||||||
|
router := Default()
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
Name string `uri:"name"`
|
||||||
|
Id string `uri:"id"`
|
||||||
|
}
|
||||||
|
router.Handle("GET", "/rest/:name/:id", func(c *Context) {
|
||||||
|
var person Person
|
||||||
|
assert.NoError(t, c.ShouldBindUri(&person))
|
||||||
|
assert.True(t, "" != person.Name)
|
||||||
|
assert.True(t, "" != person.Id)
|
||||||
|
c.String(http.StatusOK, "ShouldBindUri test OK")
|
||||||
|
})
|
||||||
|
|
||||||
|
path, _ := exampleFromPath("/rest/:name/:id")
|
||||||
|
w := performRequest(router, "GET", path)
|
||||||
|
assert.Equal(t, "ShouldBindUri test OK", w.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
func githubConfigRouter(router *Engine) {
|
func githubConfigRouter(router *Engine) {
|
||||||
for _, route := range githubAPI {
|
for _, route := range githubAPI {
|
||||||
router.Handle(route.method, route.path, func(c *Context) {
|
router.Handle(route.method, route.path, func(c *Context) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user