Skip to content

Commit

Permalink
revert(tests): undo http.NoBody usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Feb 28, 2024
1 parent 4a1891b commit 7661fbb
Show file tree
Hide file tree
Showing 29 changed files with 433 additions and 456 deletions.
176 changes: 88 additions & 88 deletions app_test.go

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
Expand Down Expand Up @@ -1100,9 +1099,9 @@ func Test_Bind_URI(t *testing.T) {
require.Equal(t, uint(222), d.RoleID)
return nil
})
_, err := app.Test(httptest.NewRequest(MethodGet, "/test1/111/role/222", http.NoBody))
_, err := app.Test(httptest.NewRequest(MethodGet, "/test1/111/role/222", nil))
require.NoError(t, err)
_, err = app.Test(httptest.NewRequest(MethodGet, "/test2/111/role/222", http.NoBody))
_, err = app.Test(httptest.NewRequest(MethodGet, "/test2/111/role/222", nil))
require.NoError(t, err)
}

Expand All @@ -1121,9 +1120,9 @@ func Test_Bind_URI_Map(t *testing.T) {
require.Equal(t, uint(222), d["roleId"])
return nil
})
_, err := app.Test(httptest.NewRequest(MethodGet, "/test1/111/role/222", http.NoBody))
_, err := app.Test(httptest.NewRequest(MethodGet, "/test1/111/role/222", nil))
require.NoError(t, err)
_, err = app.Test(httptest.NewRequest(MethodGet, "/test2/111/role/222", http.NoBody))
_, err = app.Test(httptest.NewRequest(MethodGet, "/test2/111/role/222", nil))
require.NoError(t, err)
}

Expand Down
67 changes: 33 additions & 34 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"io"
"mime/multipart"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
Expand Down Expand Up @@ -856,7 +855,7 @@ func Test_Ctx_UserContext_Multiple_Requests(t *testing.T) {
i := i
t.Run(fmt.Sprintf("request_%d", i), func(t *testing.T) {
t.Parallel()
resp, err := app.Test(httptest.NewRequest(MethodGet, fmt.Sprintf("/?input=%d", i), http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, fmt.Sprintf("/?input=%d", i), nil))

require.NoError(t, err, "Unexpected error from response")
require.Equal(t, StatusOK, resp.StatusCode, "context.Context returned from c.UserContext() is reused")
Expand Down Expand Up @@ -1740,7 +1739,7 @@ func Test_Ctx_PortInHandler(t *testing.T) {
return c.SendString(c.Port())
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "/port", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/port", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

Expand Down Expand Up @@ -2077,7 +2076,7 @@ func Test_Ctx_Locals(t *testing.T) {
require.Equal(t, "doe", c.Locals("john"))
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
Expand All @@ -2099,7 +2098,7 @@ func Test_Ctx_Locals_Generic(t *testing.T) {
require.Equal(t, 0, Locals[int](c, "isHuman"))
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
Expand All @@ -2122,7 +2121,7 @@ func Test_Ctx_Locals_GenericCustomStruct(t *testing.T) {
require.Equal(t, User{"john", 18}, Locals[User](c, "user"))
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
Expand Down Expand Up @@ -2173,7 +2172,7 @@ func Test_Ctx_ClientHelloInfo(t *testing.T) {
})

// Test without TLS handler
resp, err := app.Test(httptest.NewRequest(MethodGet, "/ServerName", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/ServerName", nil))
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
Expand All @@ -2191,23 +2190,23 @@ func Test_Ctx_ClientHelloInfo(t *testing.T) {
}}

// Test ServerName
resp, err = app.Test(httptest.NewRequest(MethodGet, "/ServerName", http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/ServerName", nil))
require.NoError(t, err)

body, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, []byte("example.golang"), body)

// Test SignatureSchemes
resp, err = app.Test(httptest.NewRequest(MethodGet, "/SignatureSchemes", http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/SignatureSchemes", nil))
require.NoError(t, err)

body, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, "["+strconv.Itoa(pssWithSHA256)+"]", string(body))

// Test SupportedVersions
resp, err = app.Test(httptest.NewRequest(MethodGet, "/SupportedVersions", http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/SupportedVersions", nil))
require.NoError(t, err)
body, err = io.ReadAll(resp.Body)
require.NoError(t, err)
Expand Down Expand Up @@ -2322,23 +2321,23 @@ func Test_Ctx_Params(t *testing.T) {
require.Equal(t, "first", c.Params("Id"))
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test/john", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test/john", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/test2/im/a/cookie", http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test2/im/a/cookie", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/test3/1111/blafasel/2222", http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test3/1111/blafasel/2222", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/test4", http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test4", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/test5/first/second", http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test5/first/second", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
Expand All @@ -2356,11 +2355,11 @@ func Test_Ctx_Params_Case_Sensitive(t *testing.T) {
require.Equal(t, "second", c.Params("Id"))
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test/john", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test/john", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/test2/first/second", http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test2/first/second", nil))
require.NoError(t, err)
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
Expand Down Expand Up @@ -2409,7 +2408,7 @@ func Test_Ctx_Path(t *testing.T) {
require.Equal(t, "/اختبار/", c.Path("/%D8%A7%D8%AE%D8%AA%D8%A8%D8%A7%D8%B1/"))
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "/specialChars/cr%C3%A9er", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/specialChars/cr%C3%A9er", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
Expand Down Expand Up @@ -3128,7 +3127,7 @@ func Test_Ctx_Route(t *testing.T) {
require.Equal(t, "/test", c.Route().Path)
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

Expand All @@ -3147,7 +3146,7 @@ func Test_Ctx_RouteNormalized(t *testing.T) {
require.Equal(t, "/test", c.Route().Path)
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "//test", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "//test", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusNotFound, resp.StatusCode, "Status code")
}
Expand Down Expand Up @@ -3384,7 +3383,7 @@ func Test_Ctx_SendFile_404(t *testing.T) {
return err
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "/", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "/", nil))
require.NoError(t, err)
require.Equal(t, StatusNotFound, resp.StatusCode)
}
Expand Down Expand Up @@ -3424,11 +3423,11 @@ func Test_Ctx_SendFile_Immutable(t *testing.T) {
t.Run(endpoint, func(t *testing.T) {
t.Parallel()
// 1st try
resp, err := app.Test(httptest.NewRequest(MethodGet, endpoint, http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, endpoint, nil))
require.NoError(t, err)
require.Equal(t, StatusOK, resp.StatusCode)
// 2nd try
resp, err = app.Test(httptest.NewRequest(MethodGet, endpoint, http.NoBody))
resp, err = app.Test(httptest.NewRequest(MethodGet, endpoint, nil))
require.NoError(t, err)
require.Equal(t, StatusOK, resp.StatusCode)
})
Expand All @@ -3446,9 +3445,9 @@ func Test_Ctx_SendFile_RestoreOriginalURL(t *testing.T) {
return err
})

_, err1 := app.Test(httptest.NewRequest(MethodGet, "/?test=true", http.NoBody))
_, err1 := app.Test(httptest.NewRequest(MethodGet, "/?test=true", nil))
// second request required to confirm with zero allocation
_, err2 := app.Test(httptest.NewRequest(MethodGet, "/?test=true", http.NoBody))
_, err2 := app.Test(httptest.NewRequest(MethodGet, "/?test=true", nil))

require.NoError(t, err1)
require.NoError(t, err2)
Expand Down Expand Up @@ -3761,7 +3760,7 @@ func Test_Ctx_Next(t *testing.T) {
c.Set("X-Next-Result", "Works")
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/test", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/test", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
require.Equal(t, "Works", resp.Header.Get("X-Next-Result"))
Expand All @@ -3776,7 +3775,7 @@ func Test_Ctx_Next_Error(t *testing.T) {
return ErrNotFound
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/test", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/test", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusNotFound, resp.StatusCode, "Status code")
require.Equal(t, "Works", resp.Header.Get("X-Next-Result"))
Expand Down Expand Up @@ -4018,7 +4017,7 @@ func Test_Ctx_RestartRouting(t *testing.T) {
}
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
require.Equal(t, 3, calls, "Number of calls")
Expand All @@ -4043,7 +4042,7 @@ func Test_Ctx_RestartRoutingWithChangedPath(t *testing.T) {
return nil
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/old", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/old", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
require.False(t, executedOldHandler, "Executed old handler")
Expand All @@ -4066,7 +4065,7 @@ func Test_Ctx_RestartRoutingWithChangedPathAndCatchAll(t *testing.T) {
return ErrNotFound
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/old", http.NoBody))
resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/old", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}
Expand Down Expand Up @@ -4813,16 +4812,16 @@ func TestCtx_ParamsInt(t *testing.T) {
return nil
})

_, err := app.Test(httptest.NewRequest(MethodGet, "/test/1111", http.NoBody))
_, err := app.Test(httptest.NewRequest(MethodGet, "/test/1111", nil))
require.NoError(t, err)

_, err = app.Test(httptest.NewRequest(MethodGet, "/testnoint/xd", http.NoBody))
_, err = app.Test(httptest.NewRequest(MethodGet, "/testnoint/xd", nil))
require.NoError(t, err)

_, err = app.Test(httptest.NewRequest(MethodGet, "/testignoredefault/2222", http.NoBody))
_, err = app.Test(httptest.NewRequest(MethodGet, "/testignoredefault/2222", nil))
require.NoError(t, err)

_, err = app.Test(httptest.NewRequest(MethodGet, "/testdefault/xd", http.NoBody))
_, err = app.Test(httptest.NewRequest(MethodGet, "/testdefault/xd", nil))
require.NoError(t, err)
}

Expand Down
6 changes: 3 additions & 3 deletions middleware/adaptor/adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func Test_ConvertRequest(t *testing.T) {
return c.SendString("Request URL: " + httpReq.URL.String())
})

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test?hello=world&another=test", http.NoBody))
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test?hello=world&another=test", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, http.StatusOK, resp.StatusCode, "Status code")

Expand Down Expand Up @@ -453,7 +453,7 @@ func Benchmark_FiberHandlerFunc(b *testing.B) {

r := http.Request{
Method: http.MethodPost,
Body: http.NoBody,
Body: nil,
}

// Replace the empty Body with our buffer
Expand Down Expand Up @@ -520,7 +520,7 @@ func Benchmark_FiberHandlerFunc_Parallel(b *testing.B) {
w := httptest.NewRecorder()
r := http.Request{
Method: http.MethodPost,
Body: http.NoBody,
Body: nil,
}

// Replace the empty Body with our buffer
Expand Down
5 changes: 2 additions & 3 deletions middleware/basicauth/basicauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

Expand All @@ -23,7 +22,7 @@ func Test_BasicAuth_Next(t *testing.T) {
},
}))

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody))
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
}
Expand Down Expand Up @@ -76,7 +75,7 @@ func Test_Middleware_BasicAuth(t *testing.T) {
// Base64 encode credentials for http auth header
creds := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", tt.username, tt.password)))

req := httptest.NewRequest(fiber.MethodGet, "/testauth", http.NoBody)
req := httptest.NewRequest(fiber.MethodGet, "/testauth", nil)
req.Header.Add("Authorization", "Basic "+creds)
resp, err := app.Test(req)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 7661fbb

Please sign in to comment.