Repository: gorilla/websocket (go)

Issue:
[feature] Add (*Conn) NetConn() net.Conn method
Go 1.18 [adds the NetConn method](https://github.com/golang/go/commit/f47335e653ac37f68ada2b265ce4a2c869a27cae) to *tls.Conn.  The new method is identical in purpose to the websocket connection's [UnderlyingConn method](https://pkg.go.dev/github.com/gorilla/websocket#Conn.UnderlyingConn).

To align the method names with the standard library, add a NetConn method to *Conn and deprecate UnderlyingConn:

```
// NetConn returns the underlying connection that is wrapped by c.
// Note that writing to or reading from this connection directly will corrupt the
// WebSocket connection.
func (c *Conn) NetConn() net.Conn {
	return c.conn
}

// UnderlyingConn returns the internal net.Conn. This can be used to further
// modifications to connection specific flags.
// Deprecated: Use the NetConn method.
func (c *Conn) UnderlyingConn() net.Conn {
	return c.conn
}
```


Pull request description:
Changed the method name UnderlyingConn to NetConn
Fixes #766

**Summary of Changes**

1. Changed the method name `UnderlyingConn` to `NetConn` and marked `UnderlyingConn` as deprecated
2. Changed the test name to `TestDeprecatedUnderlyingConn`
3. Added new test `TestNetConn` to test `NetConn` method

Patch:
diff --git a/conn.go b/conn.go
index 331eebc..5161ef8 100644
--- a/conn.go
+++ b/conn.go
@@ -1189,8 +1189,16 @@ func (c *Conn) SetPongHandler(h func(appData string) error) {
 	c.handlePong = h
 }
 
+// NetConn returns the underlying connection that is wrapped by c.
+// Note that writing to or reading from this connection directly will corrupt the
+// WebSocket connection.
+func (c *Conn) NetConn() net.Conn {
+	return c.conn
+}
+
 // UnderlyingConn returns the internal net.Conn. This can be used to further
 // modifications to connection specific flags.
+// Deprecated: Use the NetConn method.
 func (c *Conn) UnderlyingConn() net.Conn {
 	return c.conn
 }


Test patch:
diff --git a/conn_test.go b/conn_test.go
index bd96e0a..06e5184 100644
--- a/conn_test.go
+++ b/conn_test.go
@@ -562,7 +562,7 @@ func TestAddrs(t *testing.T) {
 	}
 }
 
-func TestUnderlyingConn(t *testing.T) {
+func TestDeprecatedUnderlyingConn(t *testing.T) {
 	var b1, b2 bytes.Buffer
 	fc := fakeNetConn{Reader: &b1, Writer: &b2}
 	c := newConn(fc, true, 1024, 1024, nil, nil, nil)
@@ -572,6 +572,16 @@ func TestUnderlyingConn(t *testing.T) {
 	}
 }
 
+func TestNetConn(t *testing.T) {
+	var b1, b2 bytes.Buffer
+	fc := fakeNetConn{Reader: &b1, Writer: &b2}
+	c := newConn(fc, true, 1024, 1024, nil, nil, nil)
+	ul := c.NetConn()
+	if ul != fc {
+		t.Fatalf("Underlying conn is not what it should be.")
+	}
+}
+
 func TestBufioReadBytes(t *testing.T) {
 	// Test calling bufio.ReadBytes for value longer than read buffer size.
 
diff --git a/server_test.go b/server_test.go
index 3029aa8..5804be1 100644
--- a/server_test.go
+++ b/server_test.go
@@ -111,7 +111,7 @@ func TestBufioReuse(t *testing.T) {
 		if reuse := c.br == br; reuse != tt.reuse {
 			t.Errorf("%d: buffered reader reuse=%v, want %v", i, reuse, tt.reuse)
 		}
-		writeBuf := bufioWriterBuffer(c.UnderlyingConn(), bw)
+		writeBuf := bufioWriterBuffer(c.NetConn(), bw)
 		if reuse := &c.writeBuf[0] == &writeBuf[0]; reuse != tt.reuse {
 			t.Errorf("%d: write buffer reuse=%v, want %v", i, reuse, tt.reuse)
 		}