mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
Merge branch 'master' into patch-1
This commit is contained in:
commit
7adf0fb04e
46
README.md
46
README.md
@ -1344,6 +1344,52 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
The `net/http/httptest` package is preferable way for HTTP testing.
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
func setupRouter() *gin.Engine {
|
||||||
|
r := gin.Default()
|
||||||
|
r.GET("/ping", func(c *gin.Context) {
|
||||||
|
c.String(200, "pong")
|
||||||
|
})
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := setupRouter()
|
||||||
|
r.Run(":8080")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Test for code example above:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPingRoute(t *testing.T) {
|
||||||
|
router := setupRouter()
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("GET", "/ping", nil)
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
assert.Equal(t, 200, w.Code)
|
||||||
|
assert.Equal(t, "pong", w.Body.String())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Users [](https://sourcegraph.com/github.com/gin-gonic/gin?badge)
|
## Users [](https://sourcegraph.com/github.com/gin-gonic/gin?badge)
|
||||||
|
|
||||||
Awesome project lists using [Gin](https://github.com/gin-gonic/gin) web framework.
|
Awesome project lists using [Gin](https://github.com/gin-gonic/gin) web framework.
|
||||||
|
13
context.go
13
context.go
@ -33,9 +33,7 @@ const (
|
|||||||
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm
|
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const abortIndex int8 = math.MaxInt8 / 2
|
||||||
abortIndex int8 = math.MaxInt8 / 2
|
|
||||||
)
|
|
||||||
|
|
||||||
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
||||||
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
||||||
@ -105,8 +103,7 @@ func (c *Context) Handler() HandlerFunc {
|
|||||||
// See example in GitHub.
|
// See example in GitHub.
|
||||||
func (c *Context) Next() {
|
func (c *Context) Next() {
|
||||||
c.index++
|
c.index++
|
||||||
s := int8(len(c.handlers))
|
for s := int8(len(c.handlers)); c.index < s; c.index++ {
|
||||||
for ; c.index < s; c.index++ {
|
|
||||||
c.handlers[c.index](c)
|
c.handlers[c.index](c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -521,11 +518,11 @@ func (c *Context) ClientIP() string {
|
|||||||
clientIP = clientIP[0:index]
|
clientIP = clientIP[0:index]
|
||||||
}
|
}
|
||||||
clientIP = strings.TrimSpace(clientIP)
|
clientIP = strings.TrimSpace(clientIP)
|
||||||
if len(clientIP) > 0 {
|
if clientIP != "" {
|
||||||
return clientIP
|
return clientIP
|
||||||
}
|
}
|
||||||
clientIP = strings.TrimSpace(c.requestHeader("X-Real-Ip"))
|
clientIP = strings.TrimSpace(c.requestHeader("X-Real-Ip"))
|
||||||
if len(clientIP) > 0 {
|
if clientIP != "" {
|
||||||
return clientIP
|
return clientIP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -588,7 +585,7 @@ func (c *Context) Status(code int) {
|
|||||||
// It writes a header in the response.
|
// It writes a header in the response.
|
||||||
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
|
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
|
||||||
func (c *Context) Header(key, value string) {
|
func (c *Context) Header(key, value string) {
|
||||||
if len(value) == 0 {
|
if value == "" {
|
||||||
c.Writer.Header().Del(key)
|
c.Writer.Header().Del(key)
|
||||||
} else {
|
} else {
|
||||||
c.Writer.Header().Set(key, value)
|
c.Writer.Header().Set(key, value)
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
var DB = make(map[string]string)
|
var DB = make(map[string]string)
|
||||||
|
|
||||||
func main() {
|
func setupRouter() *gin.Engine {
|
||||||
// Disable Console Color
|
// Disable Console Color
|
||||||
// gin.DisableConsoleColor()
|
// gin.DisableConsoleColor()
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
@ -53,6 +53,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := setupRouter()
|
||||||
// Listen and Server in 0.0.0.0:8080
|
// Listen and Server in 0.0.0.0:8080
|
||||||
r.Run(":8080")
|
r.Run(":8080")
|
||||||
}
|
}
|
||||||
|
20
examples/basic/main_test.go
Normal file
20
examples/basic/main_test.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPingRoute(t *testing.T) {
|
||||||
|
router := setupRouter()
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
req, _ := http.NewRequest("GET", "/ping", nil)
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
assert.Equal(t, 200, w.Code)
|
||||||
|
assert.Equal(t, "pong", w.Body.String())
|
||||||
|
}
|
4
gin.go
4
gin.go
@ -403,8 +403,8 @@ func redirectTrailingSlash(c *Context) {
|
|||||||
code = 307
|
code = 307
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(path) > 1 && path[len(path)-1] == '/' {
|
if length := len(path); length > 1 && path[length-1] == '/' {
|
||||||
req.URL.Path = path[:len(path)-1]
|
req.URL.Path = path[:length-1]
|
||||||
} else {
|
} else {
|
||||||
req.URL.Path = path + "/"
|
req.URL.Path = path + "/"
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,7 @@
|
|||||||
|
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import "encoding/json"
|
||||||
"encoding/json"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Marshal = json.Marshal
|
Marshal = json.Marshal
|
||||||
|
@ -6,9 +6,7 @@
|
|||||||
|
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import "github.com/json-iterator/go"
|
||||||
"github.com/json-iterator/go"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
Loading…
x
Reference in New Issue
Block a user