gohttptool

module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2024 License: MIT

README

🛠️ Go http tool

tag Go Version GoDoc Build Status Go report Coverage Contributors License

Features

  • Build http request in golang
  • Generate curl command for http request

Unittest with gonic


func CreateTestCtx(req *http.Request) (resp *httptest.ResponseRecorder, ctx *gin.Context) {
	resp = httptest.NewRecorder()
	ctx, _ = gin.CreateTestContext(resp)
	ctx.Request = req
	return
}

func TestGonicApi(t *testing.T) {
	// 1. build request
	req, _ := httpreq.R().
		SetQueryParams(map[string]string{
			"job_id":   "1234",
		}).
		SetReq("GET", "http://any/api/v1/spark/job").
		GenRequest()
	curl := httpreq.GenCurlCommand(req, nil)
	println(curl)
	resp, ctx := CreateTestCtx(req)

	// 2. execute
	sparkServer := GetGonicSparkServer()
	sparkServer.GetJobInfo(ctx)
	if resp.Code != http.StatusOK {
		errors := ctx.Errors.Errors()
		fmt.Println("output", errors)
		t.Errorf("Expect code 200, but get %d body:%v", resp.Code, resp.Body)
	} else {
        data := map[string]string{}
		httpreq.BuildResponse(resp.Result()).Json(&data)
		if data["status"] == "" {
			t.Fatalf("Bad response: %v", data)
		}
	}
}

Directories

Path Synopsis
Package shellescape provides the shellescape.Quote to escape arbitrary strings for a safe use as command line arguments in the most common POSIX shells.
Package shellescape provides the shellescape.Quote to escape arbitrary strings for a safe use as command line arguments in the most common POSIX shells.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL