| 10 | ) |
| 11 | |
| 12 | func TestConnectionToken(t *testing.T) { |
| 13 | t.Run("explicit token round-trips successfully", func(t *testing.T) { |
| 14 | ctx := testharness.NewTestContext(t) |
| 15 | client := ctx.NewClient(func(opts *copilot.ClientOptions) { |
| 16 | opts.Connection = copilot.TCPConnection{ |
| 17 | Path: ctx.CLIPath, |
| 18 | ConnectionToken: "right-token", |
| 19 | } |
| 20 | }) |
| 21 | t.Cleanup(func() { client.ForceStop() }) |
| 22 | |
| 23 | if err := client.Start(t.Context()); err != nil { |
| 24 | t.Fatalf("Start failed: %v", err) |
| 25 | } |
| 26 | |
| 27 | resp, err := client.Ping(t.Context(), "hi") |
| 28 | if err != nil { |
| 29 | t.Fatalf("Ping failed: %v", err) |
| 30 | } |
| 31 | if resp.Message != "pong: hi" { |
| 32 | t.Errorf("expected message 'pong: hi', got %q", resp.Message) |
| 33 | } |
| 34 | }) |
| 35 | |
| 36 | t.Run("auto-generated token round-trips successfully", func(t *testing.T) { |
| 37 | ctx := testharness.NewTestContext(t) |
| 38 | client := ctx.NewClient(func(opts *copilot.ClientOptions) { |
| 39 | opts.Connection = copilot.TCPConnection{Path: ctx.CLIPath} |
| 40 | }) |
| 41 | t.Cleanup(func() { client.ForceStop() }) |
| 42 | |
| 43 | if err := client.Start(t.Context()); err != nil { |
| 44 | t.Fatalf("Start failed: %v", err) |
| 45 | } |
| 46 | |
| 47 | resp, err := client.Ping(t.Context(), "hi") |
| 48 | if err != nil { |
| 49 | t.Fatalf("Ping failed: %v", err) |
| 50 | } |
| 51 | if resp.Message != "pong: hi" { |
| 52 | t.Errorf("expected message 'pong: hi', got %q", resp.Message) |
| 53 | } |
| 54 | }) |
| 55 | |
| 56 | t.Run("sibling client with wrong token is rejected", func(t *testing.T) { |
| 57 | ctx := testharness.NewTestContext(t) |
| 58 | good := ctx.NewClient(func(opts *copilot.ClientOptions) { |
| 59 | opts.Connection = copilot.TCPConnection{ |
| 60 | Path: ctx.CLIPath, |
| 61 | ConnectionToken: "right-token", |
| 62 | } |
| 63 | }) |
| 64 | t.Cleanup(func() { good.ForceStop() }) |
| 65 | |
| 66 | if err := good.Start(t.Context()); err != nil { |
| 67 | t.Fatalf("good client Start failed: %v", err) |
| 68 | } |
| 69 | port := good.RuntimePort() |