|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "net/url" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + "github.com/hookdeck/hookdeck-cli/pkg/websocket" |
| 18 | +) |
| 19 | + |
| 20 | +type receivedRequest struct { |
| 21 | + body []byte |
| 22 | + contentType string |
| 23 | + contentLength int64 |
| 24 | +} |
| 25 | + |
| 26 | +// forwardAttempt runs one attempt through processAttempt against a local |
| 27 | +// server and returns what that server received. |
| 28 | +func forwardAttempt(t *testing.T, request websocket.AttemptRequest) receivedRequest { |
| 29 | + t.Helper() |
| 30 | + |
| 31 | + received := make(chan receivedRequest, 1) |
| 32 | + local := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 33 | + body, _ := io.ReadAll(r.Body) |
| 34 | + received <- receivedRequest{body: body, contentType: r.Header.Get("Content-Type"), contentLength: r.ContentLength} |
| 35 | + w.WriteHeader(http.StatusOK) |
| 36 | + })) |
| 37 | + t.Cleanup(local.Close) |
| 38 | + |
| 39 | + target, err := url.Parse(local.URL) |
| 40 | + require.NoError(t, err) |
| 41 | + p := New(&Config{URL: target, NoHealthcheck: true}, nil, newRecordingRenderer()) |
| 42 | + |
| 43 | + p.processAttempt(websocket.IncomingMessage{Attempt: &websocket.Attempt{ |
| 44 | + Body: websocket.AttemptBody{Path: "/webhooks", EventID: "evt_test", AttemptId: "evt_test", Request: request}, |
| 45 | + }}) |
| 46 | + |
| 47 | + select { |
| 48 | + case r := <-received: |
| 49 | + return r |
| 50 | + case <-time.After(5 * time.Second): |
| 51 | + t.Fatal("local server never received the request") |
| 52 | + return receivedRequest{} |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func headersJSON(t *testing.T, headers map[string]string) json.RawMessage { |
| 57 | + t.Helper() |
| 58 | + raw, err := json.Marshal(headers) |
| 59 | + require.NoError(t, err) |
| 60 | + return raw |
| 61 | +} |
| 62 | + |
| 63 | +// Every byte value, so any UTF-8 round trip along the way would show. |
| 64 | +func allBytes() []byte { |
| 65 | + b := make([]byte, 256) |
| 66 | + for i := range b { |
| 67 | + b[i] = byte(i) |
| 68 | + } |
| 69 | + return b |
| 70 | +} |
| 71 | + |
| 72 | +func TestProcessAttemptForwardsBinaryBodyByteExact(t *testing.T) { |
| 73 | + body := allBytes() |
| 74 | + |
| 75 | + got := forwardAttempt(t, websocket.AttemptRequest{ |
| 76 | + Method: http.MethodPost, |
| 77 | + BodyFormat: websocket.BodyFormatBinary, |
| 78 | + DataBase64: base64.StdEncoding.EncodeToString(body), |
| 79 | + // A stale Content-Length must not win over the decoded body's length. |
| 80 | + Headers: headersJSON(t, map[string]string{"content-type": "application/octet-stream", "content-length": "1"}), |
| 81 | + }) |
| 82 | + |
| 83 | + assert.Equal(t, body, got.body) |
| 84 | + assert.Equal(t, int64(len(body)), got.contentLength) |
| 85 | + assert.Equal(t, "application/octet-stream", got.contentType) |
| 86 | +} |
| 87 | + |
| 88 | +func TestProcessAttemptForwardsBinaryMultipartUnparsed(t *testing.T) { |
| 89 | + contentType := "multipart/form-data; boundary=hookdeck-boundary" |
| 90 | + var body bytes.Buffer |
| 91 | + body.WriteString("--hookdeck-boundary\r\nContent-Disposition: form-data; name=\"field\"\r\n\r\nvalue\r\n") |
| 92 | + body.WriteString("--hookdeck-boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"f.bin\"\r\nContent-Type: application/octet-stream\r\n\r\n") |
| 93 | + body.Write(allBytes()) |
| 94 | + body.WriteString("\r\n--hookdeck-boundary--\r\n") |
| 95 | + |
| 96 | + got := forwardAttempt(t, websocket.AttemptRequest{ |
| 97 | + Method: http.MethodPost, |
| 98 | + BodyFormat: websocket.BodyFormatBinary, |
| 99 | + DataBase64: base64.StdEncoding.EncodeToString(body.Bytes()), |
| 100 | + Headers: headersJSON(t, map[string]string{"content-type": contentType}), |
| 101 | + }) |
| 102 | + |
| 103 | + assert.Equal(t, body.Bytes(), got.body, "boundary and file part bytes must match the original") |
| 104 | + assert.Equal(t, contentType, got.contentType, "the boundary travels in the original Content-Type") |
| 105 | +} |
| 106 | + |
| 107 | +func TestProcessAttemptForwardsTextBodyFromDataString(t *testing.T) { |
| 108 | + // The shape every server sends today, and the only one older servers send. |
| 109 | + got := forwardAttempt(t, websocket.AttemptRequest{ |
| 110 | + Method: http.MethodPost, |
| 111 | + DataString: `{"hello":"wörld"}`, |
| 112 | + Headers: headersJSON(t, map[string]string{"content-type": "application/json"}), |
| 113 | + }) |
| 114 | + |
| 115 | + assert.Equal(t, `{"hello":"wörld"}`, string(got.body)) |
| 116 | + assert.Equal(t, int64(len(`{"hello":"wörld"}`)), got.contentLength) |
| 117 | +} |
| 118 | + |
| 119 | +func TestAttemptRequestDecodesFromTheServerWireFormat(t *testing.T) { |
| 120 | + var msg websocket.IncomingMessage |
| 121 | + require.NoError(t, json.Unmarshal([]byte(`{"event":"attempt","body":{"cli_path":"/","request":{"method":"POST","headers":{},"body_format":"binary","data_base64":"AP+A"}}}`), &msg)) |
| 122 | + require.NotNil(t, msg.Attempt) |
| 123 | + |
| 124 | + body, err := msg.Attempt.Body.Request.Body() |
| 125 | + require.NoError(t, err) |
| 126 | + assert.Equal(t, []byte{0x00, 0xff, 0x80}, body) |
| 127 | + assert.True(t, msg.Attempt.Body.Request.IsBinary()) |
| 128 | +} |
| 129 | + |
| 130 | +func TestAttemptRequestRejectsInvalidBase64(t *testing.T) { |
| 131 | + _, err := websocket.AttemptRequest{BodyFormat: websocket.BodyFormatBinary, DataBase64: "not base64!"}.Body() |
| 132 | + assert.Error(t, err) |
| 133 | +} |
0 commit comments