|
|
@@ -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)
|
|
|
|
|
|
Awesome project lists using [Gin](https://github.com/gin-gonic/gin) web framework.
|