Repository: knative/pkg
Pull request #1396 (2020-06-09 13:45:50)

Add SendRaw to websocket client
Allows sending non-gob-encoded data over the web socket, in service of https://github.com/knative/serving/pull/8266.


knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1396#" title="Author self-approved">julz</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **markusthoemmes**
You can assign the PR to them by writing `/assign @markusthoemmes` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[websocket/OWNERS](https://github.com/knative/pkg/blob/master/websocket/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["markusthoemmes"]} -->
knative-metrics-robot: The following is the coverage report on the affected files.
Say `/test pull-knative-pkg-go-coverage` to re-run this coverage report

File | Old Coverage | New Coverage | Delta
---- |:------------:|:------------:|:-----:
[websocket/connection.go](https://storage.cloud.google.com/knative-prow/pr-logs/pull/knative_pkg/1396/pull-knative-pkg-go-coverage/1270351542347632640/artifacts/line-cov.html#file0) | 94.9% | 94.9% | 0.1

knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/1396#" title="Author self-approved">julz</a>*, *<a href="https://github.com/knative/pkg/pull/1396#pullrequestreview-427178794" title="Approved">markusthoemmes</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[websocket/OWNERS](https://github.com/knative/pkg/blob/master/websocket/OWNERS)~~ [markusthoemmes]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/websocket/connection.go b/websocket/connection.go
index 914f62c0b..380469847 100644
--- a/websocket/connection.go
+++ b/websocket/connection.go
@@ -312,6 +312,11 @@ func (c *ManagedConnection) Send(msg interface{}) error {
 	return c.write(websocket.BinaryMessage, b.Bytes())
 }
 
+// SendRaw sends a message over the websocket connection without performing any encoding.
+func (c *ManagedConnection) SendRaw(messageType int, msg []byte) error {
+	return c.write(messageType, msg)
+}
+
 // Shutdown closes the websocket connection.
 func (c *ManagedConnection) Shutdown() error {
 	c.closeOnce.Do(func() {


Test patch:
diff --git a/websocket/connection_test.go b/websocket/connection_test.go
index 78afc3e19..db5d6795d 100644
--- a/websocket/connection_test.go
+++ b/websocket/connection_test.go
@@ -190,6 +190,25 @@ func TestSendMessage(t *testing.T) {
 	}
 }
 
+func TestSendRawMessage(t *testing.T) {
+	spy := &inspectableConnection{
+		writeMessageCalls: make(chan struct{}, 1),
+	}
+	conn := newConnection(staticConnFactory(spy), nil)
+	conn.connect()
+
+	if got := conn.Status(); got != nil {
+		t.Errorf("Status() = %v, wanted nil", got)
+	}
+
+	if got := conn.SendRaw(websocket.BinaryMessage, []byte("test")); got != nil {
+		t.Fatalf("Expected no error but got: %+v", got)
+	}
+	if len(spy.writeMessageCalls) != 1 {
+		t.Fatalf("Expected 'WriteMessage' to be called once, but was called %v times", spy.writeMessageCalls)
+	}
+}
+
 func TestReceiveMessage(t *testing.T) {
 	testMessage := "testmessage"