params[0]=url example:http://127.0.0.1:8080/index (cannot be empty) params[1]=response status (custom compare status) default:"200 OK" params[2]=response body (custom compare content) default:"it worked"
(t *testing.T, params ...string)
| 29 | // params[1]=response status (custom compare status) default:"200 OK" |
| 30 | // params[2]=response body (custom compare content) default:"it worked" |
| 31 | func testRequest(t *testing.T, params ...string) { |
| 32 | if len(params) == 0 { |
| 33 | t.Fatal("url cannot be empty") |
| 34 | } |
| 35 | |
| 36 | tr := &http.Transport{ |
| 37 | TLSClientConfig: &tls.Config{ |
| 38 | InsecureSkipVerify: true, |
| 39 | }, |
| 40 | } |
| 41 | client := &http.Client{Transport: tr} |
| 42 | |
| 43 | resp, err := client.Get(params[0]) |
| 44 | require.NoError(t, err) |
| 45 | defer resp.Body.Close() |
| 46 | |
| 47 | body, ioerr := io.ReadAll(resp.Body) |
| 48 | require.NoError(t, ioerr) |
| 49 | |
| 50 | responseStatus := "200 OK" |
| 51 | if len(params) > 1 && params[1] != "" { |
| 52 | responseStatus = params[1] |
| 53 | } |
| 54 | |
| 55 | responseBody := "it worked" |
| 56 | if len(params) > 2 && params[2] != "" { |
| 57 | responseBody = params[2] |
| 58 | } |
| 59 | |
| 60 | assert.Equal(t, responseStatus, resp.Status, "should get a "+responseStatus) |
| 61 | if responseStatus == "200 OK" { |
| 62 | assert.Equal(t, responseBody, string(body), "resp body should match") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestRunEmpty(t *testing.T) { |
| 67 | os.Setenv("PORT", "") |
no test coverage detected