httpclientmdl_test.go 6.52 KiB
package httpclientmdl
import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"testing"
	"time"
	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/assert"
func TestGethttpclientwithconfigNotPanic(t *testing.T) {
	httpTransport := HTTPTransport{}
	httpTransport.MaxIdleConns = 10
	httpTransport.MaxIdleConnsPerHost = 20
	httpTransport.IdleConnTimeout = 90
	assert.NotPanics(t, func() { GetHTTPClientWithConfig(httpTransport) }, "The code did  panic")
func TestGethttpclientwithconfigDefaults(t *testing.T) {
	httpTransport := HTTPTransport{}
	httpTransport.MaxIdleConns = 0
	httpTransport.MaxIdleConnsPerHost = 0
	httpTransport.IdleConnTimeout = 0
	assert.NotPanics(t, func() { GetHTTPClientWithConfig(httpTransport) }, "The code did  panic")
func TestGethttpclientwithconfigNil(t *testing.T) {
	httpTransport := HTTPTransport{}
	httpTransport.MaxIdleConns = 10
	httpTransport.MaxIdleConnsPerHost = 20
	httpTransport.IdleConnTimeout = 90
	assert.NotNil(t, func() { GetHTTPClientWithConfig(httpTransport) }, "Do not return nil")
func TestGethttpclientNil(t *testing.T) {
	assert.NotNil(t, func() { GetHTTPClient() }, "Do not return nil")
func TestGethttpclientnotpanic(t *testing.T) {
	assert.NotPanics(t, func() { GetHTTPClient() }, "The code did  panic")
func BenchmarkGethttpclient(b *testing.B) {
	for i := 0; i < b.N; i++ {
		GetHTTPClient()
func BenchmarkGethttpclientDefault(b *testing.B) {
	for i := 0; i < b.N; i++ {
		httpTransport := HTTPTransport{}
		httpTransport.MaxIdleConns = 10
		httpTransport.MaxIdleConnsPerHost = 20
		httpTransport.IdleConnTimeout = 90
		GetHTTPClientWithConfig(httpTransport)
var (
	attempts  int
	sleeptime time.Duration
func TestDoHttpWithRetryGET200(t *testing.T) {
	attempts = 2
	sleeptime = 2 * time.Second
	request, _ := http.NewRequest("GET", "http://www.mkcl.org/about-mkcl", nil)
	resp, err := DoHTTPWithRetry(attempts, sleeptime, request)
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
assert.NoError(t, err, "This should not return error") assert.Equal(t, resp.StatusCode, 200) } func TestDoHttpWithRetryGETError(t *testing.T) { attempts = 2 sleeptime = 2 * time.Second request, _ := http.NewRequest("GET", "http://www.servernotrunning.org", nil) _, err := DoHTTPWithRetry(attempts, sleeptime, request) assert.Error(t, err, "This should return error") } func TestDoHttpWithRetryGETAttempt(t *testing.T) { attempts = 2 sleeptime = 2 * time.Second request, _ := http.NewRequest("GET", "http://www.mkcl.org/about-mkcl-error", nil) resp, err := DoHTTPWithRetry(attempts, sleeptime, request) assert.NoError(t, err, "This should not return error") assert.Equal(t, resp.StatusCode, 404) } func TestDoHttpWithRetryGETAttempt2(t *testing.T) { attempts = 2 sleeptime = 2 * time.Second request, _ := http.NewRequest("GET", "http://www.mkcl.org/about-mkcl", nil) errormdl.IsTestingNegetiveCaseOnCheckBool = true resp, err := DoHTTPWithRetry(attempts, sleeptime, request) errormdl.IsTestingNegetiveCaseOnCheckBool = false assert.NoError(t, err, "This should not return error") assert.Equal(t, resp.StatusCode, 200) } // Person struct type Person struct { Name string `json:"name"` Age string `json:"age"` } func addPerson(c *gin.Context) { person := Person{} defer c.Request.Body.Close() byteArr, readErr := ioutil.ReadAll(c.Request.Body) if readErr != nil { c.String(http.StatusInternalServerError, "") return } unmarshallErr := json.Unmarshal(byteArr, &person) if unmarshallErr != nil { c.String(http.StatusInternalServerError, "") return } c.String(http.StatusOK, "person1 is added successfully!") return } var router *gin.Engine func Init(o *gin.RouterGroup) { o.POST("/addPerson", addPerson) } func init() { router = gin.New() o := router.Group("/o") Init(o) router.Run("localhost:8081") } func TestDoHttpWithRetryPOST200(t *testing.T) { attempts = 2 sleeptime = 2 * time.Second var jsonStr = []byte(`{"name":"Arnav","age":"4"}`)
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
// request, _ := http.NewRequest("POST", "http://10.4.0.104:3001/o/addPerson", bytes.NewBuffer(jsonStr)) request, _ := http.NewRequest("POST", "http://localhost:8081/o/addPerson", bytes.NewBuffer(jsonStr)) resp, err := DoHTTPWithRetry(attempts, sleeptime, request) assert.NoError(t, err, "This should not return error") assert.Equal(t, resp.StatusCode, 200) } func TestDoHttpWithRetryPOSTError(t *testing.T) { attempts = 2 sleeptime = 2 * time.Second var jsonStr = []byte(`{"name":"Arnav","age":"4"}`) request, _ := http.NewRequest("POST", "http://www.servernotrunning.org", bytes.NewBuffer(jsonStr)) _, err := DoHTTPWithRetry(attempts, sleeptime, request) assert.Error(t, err, "This should return error") } func TestDoHttpWithRetryPOSTAttempt(t *testing.T) { attempts = 2 sleeptime = 2 * time.Second var jsonStr = []byte(`{"name":"Arnav","age":"4"}`) request, _ := http.NewRequest("POST", "http://www.mkcl.org/about-mkcl-error", bytes.NewBuffer(jsonStr)) resp, _ := DoHTTPWithRetry(attempts, sleeptime, request) assert.Equal(t, resp.StatusCode, 404) } // func TestDoHttpWithRetryPOSTAttempt2(t *testing.T) { // attempts = 2 // sleeptime = 2 * time.Second // var jsonStr = []byte(`{"name":"Arnav","age":"4"}`) // request, _ := http.NewRequest("POST", "http://10.4.0.104:3001/o/addPerson", bytes.NewBuffer(jsonStr)) // errormdl.IsTestingNegetiveCaseOnCheckBool = true // resp, err := DoHTTPWithRetry(attempts, sleeptime, request) // errormdl.IsTestingNegetiveCaseOnCheckBool = false // assert.NoError(t, err, "This should not return error") // assert.Equal(t, resp.StatusCode, 200) // } func TestDoHttpWithRetryPOSTReadAllError(t *testing.T) { attempts = 2 sleeptime = 2 * time.Second var jsonStr = []byte(`{"name":"Arnav","age":"4"}`) request, _ := http.NewRequest("POST", "http://www.mkcl.org/about-mkcl-error", bytes.NewBuffer(jsonStr)) errormdl.IsTestingNegetiveCaseOnCheckBool = true _, err := DoHTTPWithRetry(attempts, sleeptime, request) errormdl.IsTestingNegetiveCaseOnCheckBool = false assert.Error(t, err, "This should return error") } func BenchmarkDoHttpWithRetry(b *testing.B) { for i := 0; i < b.N; i++ { attempts := 2 sleeptime := 2 * time.Second request, _ := http.NewRequest("GET", "http://www.mkcl.org/about-mkcl-error", nil) DoHTTPWithRetry(attempts, sleeptime, request) } } func BenchmarkDoHttpWithRetryPOST(b *testing.B) { for i := 0; i < b.N; i++ { attempts := 2 sleeptime := 2 * time.Second var jsonStr = []byte(`{"name":"Arnav","age":"4"}`) request, _ := http.NewRequest("POST", "http://www.mkcl.org/about-mkcl-error", bytes.NewBuffer(jsonStr)) DoHTTPWithRetry(attempts, sleeptime, request) } }