|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/hookdeck/hookdeck-cli/pkg/listen/tui" |
| 12 | + "github.com/hookdeck/hookdeck-cli/pkg/websocket" |
| 13 | +) |
| 14 | + |
| 15 | +// The request is sent to a URL built in processAttempt, and each renderer |
| 16 | +// prints the URL it believes was requested. These tests pin both renderers to |
| 17 | +// the single-slash join, so a forwarding URL ending in "/" does not print |
| 18 | +// "/base//webhook" for a request that went to "/base/webhook". |
| 19 | + |
| 20 | +func forwardPathRendererConfig(t *testing.T, forwardTo string) *RendererConfig { |
| 21 | + t.Helper() |
| 22 | + cfg := linksRendererConfig(t, "gateway") |
| 23 | + target, err := url.Parse(forwardTo) |
| 24 | + require.NoError(t, err) |
| 25 | + cfg.TargetURL = target |
| 26 | + return cfg |
| 27 | +} |
| 28 | + |
| 29 | +func forwardPathAttempt(eventPath string) *websocket.Attempt { |
| 30 | + return &websocket.Attempt{Body: websocket.AttemptBody{ |
| 31 | + Path: eventPath, |
| 32 | + EventID: "evt_1", |
| 33 | + AttemptId: "atm_1", |
| 34 | + ConnectionId: "web_1", |
| 35 | + Request: websocket.AttemptRequest{Method: "POST"}, |
| 36 | + }} |
| 37 | +} |
| 38 | + |
| 39 | +var forwardPathRendererCases = []struct { |
| 40 | + name string |
| 41 | + forwardTo string |
| 42 | + eventPath string |
| 43 | + want string |
| 44 | +}{ |
| 45 | + {"trailing slash on the forwarding URL", "http://localhost:3000/base/", "/webhook", "POST http://localhost:3000/base/webhook "}, |
| 46 | + {"no trailing slash on the forwarding URL", "http://localhost:3000/base", "/webhook", "POST http://localhost:3000/base/webhook "}, |
| 47 | + {"forwarding URL is the root", "http://localhost:3000/", "/webhook", "POST http://localhost:3000/webhook "}, |
| 48 | + {"event path keeps its own trailing slash", "http://localhost:3000/base/", "/webhook/", "POST http://localhost:3000/base/webhook/ "}, |
| 49 | +} |
| 50 | + |
| 51 | +func TestSimpleRendererJoinsForwardingURLAndEventPath(t *testing.T) { |
| 52 | + response := &EventResponse{StatusCode: 200, Duration: 5 * time.Millisecond} |
| 53 | + |
| 54 | + for _, tc := range forwardPathRendererCases { |
| 55 | + t.Run(tc.name, func(t *testing.T) { |
| 56 | + r := NewSimpleRenderer(forwardPathRendererConfig(t, tc.forwardTo), false) |
| 57 | + out := captureStdout(t, func() { |
| 58 | + r.OnEventComplete("evt_1", forwardPathAttempt(tc.eventPath), response, time.Now()) |
| 59 | + }) |
| 60 | + assert.Contains(t, out, tc.want) |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestInteractiveRendererJoinsForwardingURLAndEventPath(t *testing.T) { |
| 66 | + response := &EventResponse{StatusCode: 200, Duration: 5 * time.Millisecond} |
| 67 | + |
| 68 | + for _, tc := range forwardPathRendererCases { |
| 69 | + t.Run(tc.name, func(t *testing.T) { |
| 70 | + r, sent := recordingInteractiveRenderer() |
| 71 | + r.cfg = forwardPathRendererConfig(t, tc.forwardTo) |
| 72 | + |
| 73 | + r.OnEventComplete("evt_1", forwardPathAttempt(tc.eventPath), response, time.Now()) |
| 74 | + |
| 75 | + require.Len(t, *sent, 1) |
| 76 | + update, ok := (*sent)[0].(tui.UpdateEventMsg) |
| 77 | + require.True(t, ok, "unexpected message %T", (*sent)[0]) |
| 78 | + assert.Contains(t, update.LogLine, tc.want) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
0 commit comments