From 76eeab15bed979b29ab5a26ef897452c76ab29a8 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Tue, 28 Dec 2021 06:00:04 +0500 Subject: [PATCH] :truck: Moved http testing util to testing/httptest package --- internal/testing/httptest/.gitignore | 1 + .../httptest/httptest.go} | 19 ++++++++++++++----- internal/util/cert.pem | 11 ----------- internal/util/key.pem | 5 ----- 4 files changed, 15 insertions(+), 21 deletions(-) create mode 100644 internal/testing/httptest/.gitignore rename internal/{util/test_serve.go => testing/httptest/httptest.go} (69%) delete mode 100644 internal/util/cert.pem delete mode 100644 internal/util/key.pem diff --git a/internal/testing/httptest/.gitignore b/internal/testing/httptest/.gitignore new file mode 100644 index 0000000..612424a --- /dev/null +++ b/internal/testing/httptest/.gitignore @@ -0,0 +1 @@ +*.pem \ No newline at end of file diff --git a/internal/util/test_serve.go b/internal/testing/httptest/httptest.go similarity index 69% rename from internal/util/test_serve.go rename to internal/testing/httptest/httptest.go index 75595ab..162ba23 100644 --- a/internal/util/test_serve.go +++ b/internal/testing/httptest/httptest.go @@ -1,5 +1,5 @@ //go:generate go run "$GOROOT/src/crypto/tls/generate_cert.go" --host 127.0.0.1,::1,localhost --start-date "Jan 1 00:00:00 1970" --duration=1000000h --ca --rsa-bits 1024 --ecdsa-curve P256 -package util +package httptest import ( "crypto/tls" @@ -12,7 +12,6 @@ import ( httputil "github.com/valyala/fasthttp/fasthttputil" ) -//nolint: gochecknoglobals var ( //go:embed cert.pem certData []byte @@ -20,9 +19,9 @@ var ( keyData []byte ) -// TestServe returns the InMemory HTTP-server and the client connected to it -// with the specified handler. -func TestServe(tb testing.TB, handler http.RequestHandler) (*http.Client, *http.Server, func()) { +// New returns the InMemory Server and the Client connected to it with the +// specified handler. +func New(tb testing.TB, handler http.RequestHandler) (*http.Client, *http.Server, func()) { tb.Helper() //nolint: exhaustivestruct @@ -53,3 +52,13 @@ func TestServe(tb testing.TB, handler http.RequestHandler) (*http.Client, *http. server.Shutdown() } } + +// NewRequest returns a new incoming server Request and cleanup function. +func NewRequest(method, target string, body []byte) *http.Request { + req := http.AcquireRequest() + req.Header.SetMethod(method) + req.SetRequestURI(target) + req.SetBody(body) + + return req +} diff --git a/internal/util/cert.pem b/internal/util/cert.pem deleted file mode 100644 index 5cb59fb..0000000 --- a/internal/util/cert.pem +++ /dev/null @@ -1,11 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIBqzCCAVGgAwIBAgIQf/XioLO++M9gcgIEzC1mrjAKBggqhkjOPQQDAjASMRAw -DgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYwMDAw -WjASMRAwDgYDVQQKEwdBY21lIENvMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE -kJnGbMm+QmPdvGFSFKxydVf0A+eKUesaoKa3rJ7ypHKxdizgA6uYumpopHaojf7i -4ms3AJmtLZvFvVDnYTz1yKOBhjCBgzAOBgNVHQ8BAf8EBAMCAoQwEwYDVR0lBAww -CgYIKwYBBQUHAwEwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUpEj1X34Xtjrm -F/4SrqwZcPbo7cMwLAYDVR0RBCUwI4IJbG9jYWxob3N0hwR/AAABhxAAAAAAAAAA -AAAAAAAAAAABMAoGCCqGSM49BAMCA0gAMEUCIQCoffjdhPWP+jdZQZvdUBLXSmQQ -hbvCvbqE+zCAEnFdGQIgA0JE3HVWJqI0OOiDMNYUF0d5Hz9tTSozkQcmVKQQZJA= ------END CERTIFICATE----- diff --git a/internal/util/key.pem b/internal/util/key.pem deleted file mode 100644 index 556dc40..0000000 --- a/internal/util/key.pem +++ /dev/null @@ -1,5 +0,0 @@ ------BEGIN PRIVATE KEY----- -MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrZ6k0Iipuq5JLos6 -Y6uPFJLbywvTULzPt1x9EG8SWJ6hRANCAASQmcZsyb5CY928YVIUrHJ1V/QD54pR -6xqgpresnvKkcrF2LOADq5i6amikdqiN/uLiazcAma0tm8W9UOdhPPXI ------END PRIVATE KEY-----