From bcf7e8e94422403a93145c6ae7a8eb2224e0436b Mon Sep 17 00:00:00 2001 From: kinggo <1963359402@qq.com> Date: Sun, 18 Sep 2022 15:20:03 +0800 Subject: [PATCH] test: merge test in adaptor_test.go (#1381) * test: merge test in adaptor_test.go * Fix lint Co-authored-by: Erik Dubbelboer --- fasthttpadaptor/adaptor_test.go | 43 ++++++--------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/fasthttpadaptor/adaptor_test.go b/fasthttpadaptor/adaptor_test.go index ba23e60bec..23e2801458 100644 --- a/fasthttpadaptor/adaptor_test.go +++ b/fasthttpadaptor/adaptor_test.go @@ -1,7 +1,6 @@ package fasthttpadaptor import ( - "fmt" "io" "net" "net/http" @@ -20,7 +19,7 @@ func TestNewFastHTTPHandler(t *testing.T) { expectedProtoMajor := 1 expectedProtoMinor := 1 expectedRequestURI := "/foo/bar?baz=123" - expectedBody := "body 123 foo bar baz" + expectedBody := "" expectedContentLength := len(expectedBody) expectedHost := "foobar.com" expectedRemoteAddr := "1.2.3.4:6789" @@ -35,6 +34,7 @@ func TestNewFastHTTPHandler(t *testing.T) { } expectedContextKey := "contextKey" expectedContextValue := "contextValue" + expectedContentType := "text/html; charset=utf-8" callsCount := 0 nethttpH := func(w http.ResponseWriter, r *http.Request) { @@ -91,7 +91,7 @@ func TestNewFastHTTPHandler(t *testing.T) { w.Header().Set("Header1", "value1") w.Header().Set("Header2", "value2") w.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(w, "request body is %q", body) + w.Write(body) //nolint:errcheck } fasthttpH := NewFastHTTPHandler(http.HandlerFunc(nethttpH)) fasthttpH = setContextValueMiddleware(fasthttpH, expectedContextKey, expectedContextValue) @@ -129,9 +129,11 @@ func TestNewFastHTTPHandler(t *testing.T) { if string(resp.Header.Peek("Header2")) != "value2" { t.Fatalf("unexpected header value: %q. Expecting %q", resp.Header.Peek("Header2"), "value2") } - expectedResponseBody := fmt.Sprintf("request body is %q", expectedBody) - if string(resp.Body()) != expectedResponseBody { - t.Fatalf("unexpected response body %q. Expecting %q", resp.Body(), expectedResponseBody) + if string(resp.Body()) != expectedBody { + t.Fatalf("unexpected response body %q. Expecting %q", resp.Body(), expectedBody) + } + if string(resp.Header.Peek("Content-Type")) != expectedContentType { + t.Fatalf("unexpected response content-type %q. Expecting %q", string(resp.Header.Peek("Content-Type")), expectedBody) } } @@ -141,32 +143,3 @@ func setContextValueMiddleware(next fasthttp.RequestHandler, key string, value i next(ctx) } } - -func TestContentType(t *testing.T) { - t.Parallel() - - nethttpH := func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("")) //nolint:errcheck - } - fasthttpH := NewFastHTTPHandler(http.HandlerFunc(nethttpH)) - - var ctx fasthttp.RequestCtx - var req fasthttp.Request - - req.SetRequestURI("http://example.com") - - remoteAddr, err := net.ResolveTCPAddr("tcp", "1.2.3.4:80") - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - ctx.Init(&req, remoteAddr, nil) - - fasthttpH(&ctx) - - resp := &ctx.Response - got := string(resp.Header.Peek("Content-Type")) - expected := "text/html; charset=utf-8" - if got != expected { - t.Errorf("expected %q got %q", expected, got) - } -}