Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: merge test in adaptor_test.go #1381

Merged
merged 2 commits into from Sep 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 8 additions & 35 deletions fasthttpadaptor/adaptor_test.go
@@ -1,7 +1,6 @@
package fasthttpadaptor

import (
"fmt"
"io"
"net"
"net/http"
Expand All @@ -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 := "<!doctype html><html>"
expectedContentLength := len(expectedBody)
expectedHost := "foobar.com"
expectedRemoteAddr := "1.2.3.4:6789"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
erikdubbelboer marked this conversation as resolved.
Show resolved Hide resolved
}
fasthttpH := NewFastHTTPHandler(http.HandlerFunc(nethttpH))
fasthttpH = setContextValueMiddleware(fasthttpH, expectedContextKey, expectedContextValue)
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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("<!doctype html><html>")) //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)
}
}