feat: make the request test helper public
This commit is contained in:
parent
a3c2ade9fb
commit
9b6282a101
@ -24,33 +24,14 @@ package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/test"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type header struct {
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
|
||||
func performRequest(
|
||||
r http.Handler,
|
||||
method, path string,
|
||||
headers ...header,
|
||||
) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(method, path, nil)
|
||||
for _, h := range headers {
|
||||
req.Header.Add(h.Key, h.Value)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
func TestRequestID(t *testing.T) {
|
||||
r := gin.New()
|
||||
r.Use(RequestID())
|
||||
@ -68,10 +49,16 @@ func TestRequestID(t *testing.T) {
|
||||
})
|
||||
|
||||
// Test with Request ID
|
||||
_ = performRequest(r, "GET", "/example?a=100", header{XRequestID, wanted})
|
||||
_ = test.PerformRequest(
|
||||
r,
|
||||
"GET",
|
||||
"/example?a=100",
|
||||
nil,
|
||||
test.Header{Key: XRequestID, Value: wanted},
|
||||
)
|
||||
assert.Equal(t, "123", got)
|
||||
|
||||
res := performRequest(r, "GET", "/example?a=100")
|
||||
res := test.PerformRequest(r, "GET", "/example?a=100", nil)
|
||||
assert.NotEqual(t, "", got)
|
||||
assert.NoError(t, uuid.Validate(got))
|
||||
assert.Equal(t, res.Header()[XRequestID][0], got)
|
||||
|
27
internal/pkg/test/request.go
Normal file
27
internal/pkg/test/request.go
Normal file
@ -0,0 +1,27 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
type Header struct {
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
|
||||
func PerformRequest(
|
||||
r http.Handler,
|
||||
method, path string,
|
||||
body io.Reader,
|
||||
headers ...Header,
|
||||
) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(method, path, body)
|
||||
for _, h := range headers {
|
||||
req.Header.Add(h.Key, h.Value)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
Loading…
Reference in New Issue
Block a user