Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix for "Why net.Addr is fiber.testAddr, not *net.TCPAddr?🤗 #1574" #1784

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,24 @@ func Test_Ctx_Port(t *testing.T) {
utils.AssertEqual(t, "0", c.Port())
}

// go test -run Test_Ctx_PortInHandler
func Test_Ctx_PortInHandler(t *testing.T) {
t.Parallel()
app := New()

app.Get("/port", func(c *Ctx) error {
return c.SendString(c.Port())
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "/port", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")

body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "0", string(body))
}

// go test -run Test_Ctx_IP
func Test_Ctx_IP(t *testing.T) {
t.Parallel()
Expand Down
16 changes: 2 additions & 14 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,6 @@ func isNoCache(cacheControl string) bool {
return true
}

// https://golang.org/src/net/net.go#L113
// Helper methods for application#test
type testAddr string

func (a testAddr) Network() string {
return string(a)
}

func (a testAddr) String() string {
return string(a)
}

type testConn struct {
r bytes.Buffer
w bytes.Buffer
Expand All @@ -359,8 +347,8 @@ func (c *testConn) Read(b []byte) (int, error) { return c.r.Read(b) }
func (c *testConn) Write(b []byte) (int, error) { return c.w.Write(b) }
func (c *testConn) Close() error { return nil }

func (c *testConn) LocalAddr() net.Addr { return testAddr("local-addr") }
func (c *testConn) RemoteAddr() net.Addr { return testAddr("remote-addr") }
func (c *testConn) LocalAddr() net.Addr { return &net.TCPAddr{Port: 0, Zone: "", IP: net.IPv4zero} }
func (c *testConn) RemoteAddr() net.Addr { return &net.TCPAddr{Port: 0, Zone: "", IP: net.IPv4zero} }
func (c *testConn) SetDeadline(_ time.Time) error { return nil }
func (c *testConn) SetReadDeadline(_ time.Time) error { return nil }
func (c *testConn) SetWriteDeadline(_ time.Time) error { return nil }
Expand Down
5 changes: 0 additions & 5 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,6 @@ func Test_Utils_GetOffset(t *testing.T) {
utils.AssertEqual(t, "", getOffer("2", "1"))
}

func Test_Utils_TestAddr_Network(t *testing.T) {
var addr testAddr = "addr"
utils.AssertEqual(t, "addr", addr.Network())
}

func Test_Utils_TestConn_Deadline(t *testing.T) {
conn := &testConn{}
utils.AssertEqual(t, nil, conn.SetDeadline(time.Time{}))
Expand Down