doakan's People
doakan's Issues
Example code testing
Main
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type LoginInput struct {
Username string `json:"username"`
Password string `json:"password"`
}
func Login(c *gin.Context) {
body := LoginInput{}
c.ShouldBindJSON(&body)
c.Set("content-type", "application/json")
c.JSON(http.StatusOK, gin.H{
"data": body,
})
}
func main() {
router := SetupRouter()
router.Run()
}
func SetupRouter() *gin.Engine {
router := gin.Default()
router.POST("/api/v1/login", Login)
return router
}
Test
// main_test.go
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
type Response struct {
Data LoginInput `json:"data"`
}
var router = SetupRouter()
func TestLogin(t *testing.T) {
t.Run("Example login", func(t *testing.T) {
payload := LoginInput{}
payload.Username = "[email protected]"
payload.Password = "qwerty12"
encoded, err := json.Marshal(payload)
if err != nil {
t.FailNow()
}
rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodPost, "/api/v1/login", bytes.NewBuffer(encoded))
req.Header.Set("Content-Type", "application/json")
if err != nil {
t.FailNow()
}
router.ServeHTTP(rr, req)
// not with struct
// res := make(map[string]interface{})
// json.NewDecoder(rr.Body).Decode(&res)
// parse := res["data"].(map[string]interface{})
// assert.Equal(t, rr.Code, 200)
// assert.Equal(t, parse["username"], "[email protected]")
// assert.Equal(t, parse["password"], "qwerty12")
// with struct
resStruct := Response{}
json.NewDecoder(rr.Body).Decode(&resStruct)
assert.Equal(t, rr.Code, 200)
assert.Equal(t, resStruct.Data.Username, "[email protected]")
assert.Equal(t, resStruct.Data.Password, "qwerty12")
})
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.