Skip to content

Commit

Permalink
Fixes #179 Add test example for post (#223)
Browse files Browse the repository at this point in the history
* Fixes #179

* update other language for the change of test
  • Loading branch information
lawxen authored Sep 11, 2023
1 parent bcb6093 commit 0e864f3
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 16 deletions.
36 changes: 36 additions & 0 deletions content/en/docs/testing/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ package main

import "github.com/gin-gonic/gin"

type User struct {
Username string `json:"username"`
Gender string `json:"gender"`
}

func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
Expand All @@ -21,8 +26,18 @@ func setupRouter() *gin.Engine {
return r
}

func postUser(r *gin.Engine) *gin.Engine {
r.POST("/user/add", func(c *gin.Context) {
var user User
c.BindJSON(&user)
c.JSON(200, user)
})
return r
}

func main() {
r := setupRouter()
r = postUser(r)
r.Run(":8080")
}
```
Expand Down Expand Up @@ -50,4 +65,25 @@ func TestPingRoute(t *testing.T) {
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}

// Test for POST /user/add
func TestPostUser(t *testing.T) {
router := setupRouter()
router = postUser(router)

w := httptest.NewRecorder()

// Create an example user for testing
exampleUser := User{
Username: "test_name",
Gender: "male",
}
userJson, _ := json.Marshal(exampleUser)
req, _ := http.NewRequest("POST", "/user/add", strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
// Compare the response body with the json data of exampleUser
assert.Equal(t, string(userJson), w.Body.String())
}
```
43 changes: 40 additions & 3 deletions content/es/docs/testing/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ weight: 7

El paquete `net/http/httptest` es la forma preferible para las pruebas de HTTP

{{<highlight go>}}

```go
package main

import "github.com/gin-gonic/gin"

type User struct {
Username string `json:"username"`
Gender string `json:"gender"`
}

func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
Expand All @@ -20,11 +26,21 @@ func setupRouter() *gin.Engine {
return r
}

func postUser(r *gin.Engine) *gin.Engine {
r.POST("/user/add", func(c *gin.Context) {
var user User
c.BindJSON(&user)
c.JSON(200, user)
})
return r
}

func main() {
r := setupRouter()
r = postUser(r)
r.Run(":8080")
}
{{</highlight>}}
```

Test para el código anterior de ejemplo:

Expand All @@ -49,6 +65,27 @@ func TestPingRoute(t *testing.T) {
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}

// Test for POST /user/add
func TestPostUser(t *testing.T) {
router := setupRouter()
router = postUser(router)

w := httptest.NewRecorder()

// Create an example user for testing
exampleUser := User{
Username: "test_name",
Gender: "male",
}
userJson, _ := json.Marshal(exampleUser)
req, _ := http.NewRequest("POST", "/user/add", strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
// Compare the response body with the json data of exampleUser
assert.Equal(t, string(userJson), w.Body.String())
}
```


Expand Down
36 changes: 36 additions & 0 deletions content/fa/docs/testing/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ package main

import "github.com/gin-gonic/gin"

type User struct {
Username string `json:"username"`
Gender string `json:"gender"`
}

func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
Expand All @@ -21,8 +26,18 @@ func setupRouter() *gin.Engine {
return r
}

func postUser(r *gin.Engine) *gin.Engine {
r.POST("/user/add", func(c *gin.Context) {
var user User
c.BindJSON(&user)
c.JSON(200, user)
})
return r
}

func main() {
r := setupRouter()
r = postUser(r)
r.Run(":8080")
}
```
Expand Down Expand Up @@ -50,4 +65,25 @@ func TestPingRoute(t *testing.T) {
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}

// Test for POST /user/add
func TestPostUser(t *testing.T) {
router := setupRouter()
router = postUser(router)

w := httptest.NewRecorder()

// Create an example user for testing
exampleUser := User{
Username: "test_name",
Gender: "male",
}
userJson, _ := json.Marshal(exampleUser)
req, _ := http.NewRequest("POST", "/user/add", strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
// Compare the response body with the json data of exampleUser
assert.Equal(t, string(userJson), w.Body.String())
}
```
43 changes: 40 additions & 3 deletions content/ja/docs/testing/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ weight: 7

`net/http/httptest` パッケージが、HTTP テストをするには好ましい方法です。

{{<highlight go>}}

```go
package main

import "github.com/gin-gonic/gin"

type User struct {
Username string `json:"username"`
Gender string `json:"gender"`
}

func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
Expand All @@ -20,11 +26,21 @@ func setupRouter() *gin.Engine {
return r
}

func postUser(r *gin.Engine) *gin.Engine {
r.POST("/user/add", func(c *gin.Context) {
var user User
c.BindJSON(&user)
c.JSON(200, user)
})
return r
}

func main() {
r := setupRouter()
r = postUser(r)
r.Run(":8080")
}
{{</highlight>}}
```

テストコードの例は以下のようになります。

Expand All @@ -49,4 +65,25 @@ func TestPingRoute(t *testing.T) {
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}

// Test for POST /user/add
func TestPostUser(t *testing.T) {
router := setupRouter()
router = postUser(router)

w := httptest.NewRecorder()

// Create an example user for testing
exampleUser := User{
Username: "test_name",
Gender: "male",
}
userJson, _ := json.Marshal(exampleUser)
req, _ := http.NewRequest("POST", "/user/add", strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
// Compare the response body with the json data of exampleUser
assert.Equal(t, string(userJson), w.Body.String())
}
```
43 changes: 40 additions & 3 deletions content/ko-kr/docs/testing/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ weight: 7

`net/http/httptest` 패키지 는 HTTP테스트에 선호되는 방법입니다.

{{<highlight go>}}

```go
package main

import "github.com/gin-gonic/gin"

type User struct {
Username string `json:"username"`
Gender string `json:"gender"`
}

func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
Expand All @@ -20,11 +26,21 @@ func setupRouter() *gin.Engine {
return r
}

func postUser(r *gin.Engine) *gin.Engine {
r.POST("/user/add", func(c *gin.Context) {
var user User
c.BindJSON(&user)
c.JSON(200, user)
})
return r
}

func main() {
r := setupRouter()
r = postUser(r)
r.Run(":8080")
}
{{</highlight>}}
```

위 예제의 테스트 코드:

Expand All @@ -49,6 +65,27 @@ func TestPingRoute(t *testing.T) {
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}

// Test for POST /user/add
func TestPostUser(t *testing.T) {
router := setupRouter()
router = postUser(router)

w := httptest.NewRecorder()

// Create an example user for testing
exampleUser := User{
Username: "test_name",
Gender: "male",
}
userJson, _ := json.Marshal(exampleUser)
req, _ := http.NewRequest("POST", "/user/add", strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
// Compare the response body with the json data of exampleUser
assert.Equal(t, string(userJson), w.Body.String())
}
```


Expand Down
36 changes: 36 additions & 0 deletions content/pt/docs/testing/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ package main

import "github.com/gin-gonic/gin"

type User struct {
Username string `json:"username"`
Gender string `json:"gender"`
}

func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
Expand All @@ -21,8 +26,18 @@ func setupRouter() *gin.Engine {
return r
}

func postUser(r *gin.Engine) *gin.Engine {
r.POST("/user/add", func(c *gin.Context) {
var user User
c.BindJSON(&user)
c.JSON(200, user)
})
return r
}

func main() {
r := setupRouter()
r = postUser(r)
r.Run(":8080")
}
```
Expand Down Expand Up @@ -50,4 +65,25 @@ func TestPingRoute(t *testing.T) {
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}

// Test for POST /user/add
func TestPostUser(t *testing.T) {
router := setupRouter()
router = postUser(router)

w := httptest.NewRecorder()

// Create an example user for testing
exampleUser := User{
Username: "test_name",
Gender: "male",
}
userJson, _ := json.Marshal(exampleUser)
req, _ := http.NewRequest("POST", "/user/add", strings.NewReader(string(userJson)))
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
// Compare the response body with the json data of exampleUser
assert.Equal(t, string(userJson), w.Body.String())
}
```
Loading

0 comments on commit 0e864f3

Please sign in to comment.