From cebe7facdbc32d0dacb9010b7536415f5354c6b0 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sun, 3 Nov 2024 14:18:33 +0800 Subject: [PATCH] test(body): improve request body handling and testing - 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 --- gofight_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/gofight_test.go b/gofight_test.go index 8f03dc0..babf9ca 100644 --- a/gofight_test.go +++ b/gofight_test.go @@ -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) + 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) + }) +}