HTTP handler testing for golang web framework. The benchmark features used fasthttp.
Will support another framework for the future
go get github.com/KodepandaID/ujihttp
This is an example of the main program using Gin, but you can also use net/http, Gorilla Mux, or Echo.
package main
import (
"net/http"
"github.com/KodepandaID/ujihttp"
"github.com/gin-gonic/gin"
)
func GinEngine() *gin.Engine {
r := gin.New()
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World!!")
})
return r
}
This is an example for testing the main program.
package main
import (
"net/http"
"testing"
"github.com/KodepandaID/ujihttp"
"github.com/stretchr/testify/assert"
)
func TestGinGET(t *testing.T) {
r := ujihttp.New()
r.
GET("/").
Run(GinEngine(), func(req *http.Request, rec *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusOK, rec.Code)
})
}
To enable debug mode.
func main() {
r := ujihttp.New()
r.
SetDebug(true).
GET("/").
Run(GinEngine())
}
You can use the GET, POST, PUT, DELETE, PATCH, and HEAD method.
func main() {
r := ujihttp.New()
r.
GET("/").
Run(GinEngine())
}
If you use the POST or PUT method, you can send a JSON body.
func main() {
r := ujihttp.New()
r.
POST("/add-json").
SendJSON(ujihttp.JSON{
"user": "test",
"password": "password",
}).
Run(GinEngine())
}
If you want to send data with header multipart/form-data
func main() {
r := ujihttp.New()
r.
POST("/add-json").
SendJSON(ujihttp.JSON{
"user": "test",
"password": "password",
}).
Run(GinEngine())
}
You can combine this method with SendFormData method.
func main() {
r := ujihttp.New()
path, _ := os.Getwd()
r.
SetDebug(true).
POST("/post-file").
SendFile("file", path+"/assets/sample.jpg").
Run(GinEngine())
}
If you want to upload multiple files, use this method. You can combine this method with SendFormData method.
func main() {
r := ujihttp.New()
path, _ := os.Getwd()
r.
SetDebug(true).
POST("/post-file").
SendMultipleFile("file", path+"/assets/sample.jpg").
Run(GinEngine())
}
You can benchmark your API using this library. But, if you want to benchmark your API, make sure your API already run.
func main() {
r := benchmark.New()
r.
Duration(10).
Concurrent(500).
Pipeline(1).
GET("http://localhost:3000").
Run()
}
See below the methods you can use on the Benchmark features.
You can set how long the benchmark run in the second.
r := benchmark.New()
r.Duration(10)
You can set how many the concurrent connection.
r := benchmark.New()
r.Concurrent(500)
You can set how many used the HTTP pipeline request.
r := benchmark.New()
r.Pipeline(500)
Same with the API testing handler, you can use GET, POST, PUT, DELETE, PATCH, and HEAD method.
r := benchmark.New()
r.GET("/").Run()
Copyright Yudha Pratama. Licensed under MIT.