Skip to content

Commit

Permalink
test(body): improve request body handling and testing
Browse files Browse the repository at this point in the history
- Add a test function `TestSetBody` to verify setting the body in a POST request
- Ensure the body content is correctly read and matches the expected string
- Validate the response body and status code in the new test function

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Nov 3, 2024
1 parent d2f7f14 commit cebe7fa
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions gofight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,24 @@ func TestGetHeaderFromResponse(t *testing.T) {
assert.Equal(t, "Hello World", r.Body.String())
})
}

func TestSetBody(t *testing.T) {
r := New()
body := "a=1&b=2"

r.POST("/").
SetBody(body).
Run(basicEngine(), func(r HTTPResponse, rq HTTPRequest) {
// Read the content of the io.Reader
bodyBytes, err := io.ReadAll(rq.Body)

Check failure on line 113 in gofight_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: io.ReadAll (typecheck)
if err != nil {
t.Fatalf("Failed to read body: %v", err)
}

// Convert the byte slice to a string
bodyString := string(bodyBytes)
assert.Equal(t, body, bodyString)
assert.Equal(t, "Hello World", r.Body.String())
assert.Equal(t, http.StatusOK, r.Code)
})
}

0 comments on commit cebe7fa

Please sign in to comment.