feat: add t.Helper to request helper

This commit is contained in:
Muyao CHEN 2024-10-12 17:09:03 +02:00
parent 9b6282a101
commit ce3076047a
2 changed files with 5 additions and 1 deletions

View File

@ -50,6 +50,7 @@ func TestRequestID(t *testing.T) {
// Test with Request ID // Test with Request ID
_ = test.PerformRequest( _ = test.PerformRequest(
t,
r, r,
"GET", "GET",
"/example?a=100", "/example?a=100",
@ -58,7 +59,7 @@ func TestRequestID(t *testing.T) {
) )
assert.Equal(t, "123", got) assert.Equal(t, "123", got)
res := test.PerformRequest(r, "GET", "/example?a=100", nil) res := test.PerformRequest(t, r, "GET", "/example?a=100", nil)
assert.NotEqual(t, "", got) assert.NotEqual(t, "", got)
assert.NoError(t, uuid.Validate(got)) assert.NoError(t, uuid.Validate(got))
assert.Equal(t, res.Header()[XRequestID][0], got) assert.Equal(t, res.Header()[XRequestID][0], got)

View File

@ -4,6 +4,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing"
) )
type Header struct { type Header struct {
@ -12,11 +13,13 @@ type Header struct {
} }
func PerformRequest( func PerformRequest(
t testing.TB,
r http.Handler, r http.Handler,
method, path string, method, path string,
body io.Reader, body io.Reader,
headers ...Header, headers ...Header,
) *httptest.ResponseRecorder { ) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(method, path, body) req := httptest.NewRequest(method, path, body)
for _, h := range headers { for _, h := range headers {
req.Header.Add(h.Key, h.Value) req.Header.Add(h.Key, h.Value)