Go Supertest is minimalize HTTP Client Testing only for Gin Framework, inspired by Supertest package library HTTP Client Testing for Express.js Framework.
$ go get -u github.com/restuwahyu13/go-supertest
Important if you use http request using any method and you not returning payload, you must be use Send
with nil value and Send
must be added before Set
or Auth
please check example usage about this package is working.
-
Method Description NewSuperTest instance method for create unit testing -
Method Description Get for handling request http client using GET method -
Method Description Post for handling request http client using POST method -
Method Description Delete for handling request http client using Delete method -
Method Description Put for handling request http client using Put method -
Method Description Patch for handling request http client using Patch method -
Method Description Head for handling request http client using Head method -
Method Description Options for handling request http client using Options method -
Method Description Send send request data needed by client -
Method Description End receive responses from http requests -
Method Description Set set your headers before sending http request into client -
Method Description Auth pass the username or password if you are using HTTP Basic authentication -
Method Description Timeout set delay before sending request into client
-
package main import ( "net/http" "github.com/gin-gonic/gin" ) type User struct { Name string `json:"name"` } func GetMethod(ctx *gin.Context) { ctx.JSON(http.StatusOK, gin.H{ "statusCode": 200, "method": http.MethodGet, "message": "fetch request using get method", "data": nil, }) } func PostMethod(ctx *gin.Context) { var input User ctx.ShouldBindJSON(&input) ctx.JSON(http.StatusOK, gin.H{ "statusCode": 200, "method": http.MethodPost, "message": "fetch request using post method", "data": input, }) } func DeleteMethod(ctx *gin.Context) { userId := ctx.Param("id") userData := make(map[string]string, 5) userData["name-1"] = "john doe" userData["name-2"] = "jane doe" userData["name-3"] = "james bond" userData["name-4"] = "curt cobain" userData["name-5"] = "rorona zoro" delete(userData, "name-"+userId) ctx.JSON(http.StatusOK, gin.H{ "statusCode": 200, "method": http.MethodPost, "message": "fetch request using delete method", "data": userData, }) } func PutMethod(ctx *gin.Context) { var input User userId := ctx.Param("id") ctx.ShouldBindJSON(&input) userData := make(map[string]string, 5) userData["name-1"] = "john doe" userData["name-2"] = "jane doe" userData["name-3"] = "james bond" userData["name-4"] = "curt cobain" userData["name-5"] = "rorona zoro" userData["name-"+userId] = input.Name ctx.JSON(http.StatusOK, gin.H{ "statusCode": 200, "method": http.MethodPost, "message": "fetch request using put method", "data": userData, }) } func main() { router := SetupRouter() router.Run() } func SetupRouter() *gin.Engine { router := gin.Default() gin.SetMode(gin.TestMode) router.GET("/", GetMethod) router.POST("/", PostMethod) router.DELETE("/:id", DeleteMethod) router.PUT("/:id", PutMethod) return router }
-
package main import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/go-playground/assert/v2" "github.com/restuwahyu13/go-supertest/supertest" ) type Response struct { StatusCode int `json:"statusCode"` Method string `json:"method"` Message string `json:"message"` Data interface{} `json:"data"` } var router = SetupRouter() func TestGetMethod(t *testing.T) { test := supertest.NewSuperTest(router, t) test.Get("/") test.Send(nil) test.Set("Content-Type", "application/json") test.End(func(req *http.Request, rr *httptest.ResponseRecorder) { var response Response json.Unmarshal(rr.Body.Bytes(), &response) assert.Equal(t, http.StatusOK, rr.Code) assert.Equal(t, req.Method, req.Method) assert.Equal(t, "fetch request using get method", response.Message) }) } func TestPostMethod(t *testing.T) { test := supertest.NewSuperTest(router, t) payload := gin.H{ "name": "restu wahyu saputra", } test.Post("/") test.Send(payload) test.Set("Content-Type", "application/json") test.End(func(req *http.Request, rr *httptest.ResponseRecorder) { var response Response json.Unmarshal(rr.Body.Bytes(), &response) assert.Equal(t, http.StatusOK, rr.Code) assert.Equal(t, req.Method, req.Method) assert.Equal(t, "fetch request using post method", response.Message) }) } func TestDeleteMethod(t *testing.T) { test := supertest.NewSuperTest(router, t) test.Delete("/" + fmt.Sprintf("%v", 5)) test.Send(nil) test.Set("Content-Type", "application/json") test.End(func(req *http.Request, rr *httptest.ResponseRecorder) { var response Response json.Unmarshal(rr.Body.Bytes(), &response) assert.Equal(t, http.StatusOK, rr.Code) assert.Equal(t, req.Method, req.Method) assert.Equal(t, "fetch request using delete method", response.Message) encoded, _ := json.Marshal(response.Data) var mapping map[string]interface{} json.Unmarshal(encoded, &mapping) assert.Equal(t, 4, len(mapping)) }) } func TestPutMethod(t *testing.T) { test := supertest.NewSuperTest(router, t) payload := gin.H{ "name": "restu wahyu saputra", } test.Put("/" + fmt.Sprintf("%v", 1)) test.Send(payload) test.Set("Content-Type", "application/json") test.End(func(req *http.Request, rr *httptest.ResponseRecorder) { var response Response json.Unmarshal(rr.Body.Bytes(), &response) assert.Equal(t, http.StatusOK, rr.Code) assert.Equal(t, req.Method, req.Method) assert.Equal(t, "fetch request using put method", response.Message) encoded, _ := json.Marshal(response.Data) var mapping map[string]interface{} json.Unmarshal(encoded, &mapping) assert.Equal(t, "restu wahyu saputra", mapping["name-1"]) }) }
Name | Ready | Description |
---|---|---|
Get | yes | for handling request http client using GET method |
Post | yes | for handling request http client using POST method |
Delete | yes | for handling request http client using Delete method |
Put | yes | for handling request http client using Put method |
Patch | yes | for handling request http client using Patch method |
Head | yes | for handling request http client using Head method |
Options | yes | for handling request http client using Options method |
Send | yes | send request data needed by client |
End | yes | receive responses from http requests |
Set | yes | set your headers before sending http request into client |
Auth | yes | pass the username or password if you are using HTTP Basic authentication |
Timeout | yes | set delay before sending request into client |
Attach | no | handle requests from files or image uploads if you are using multipart/form-data |
Field | no | handle data submitted from form/field if you are using multipart/form-data |
$ go test ./supertest/... || make gotest
For information on bugs related to package libraries, please visit here
Want to make Go Supertest more perfect ? Let's contribute and follow the contribution guide.