Skip to content

Commit

Permalink
Fix for "Why net.Addr is fiber.testAddr, not *net.TCPAddr?🤗 #1574" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 authored Feb 19, 2022
1 parent cf47f06 commit 1bbcb4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
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

0 comments on commit 1bbcb4b

Please # to comment.