client_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package wsclient
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/gorilla/websocket"
  9. )
  10. // startWSServer brings up a minimal WS test server. The server
  11. // reads the auth frame, replies with `{"ready": true}`, then
  12. // echoes every subsequent frame back as a "ready" ack. We use
  13. // this to assert Connect's frame-level behavior without
  14. // pulling in the full ingestd path.
  15. func startWSServer(t *testing.T, accept func(auth []byte) (reply []byte, ok bool)) (string, *httptest.Server) {
  16. t.Helper()
  17. upgrader := websocket.Upgrader{}
  18. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. c, err := upgrader.Upgrade(w, r, nil)
  20. if err != nil {
  21. t.Log("upgrade:", err)
  22. return
  23. }
  24. defer c.Close()
  25. // auth frame
  26. _ = c.SetReadDeadline(time.Now().Add(2 * time.Second))
  27. _, msg, err := c.ReadMessage()
  28. if err != nil {
  29. return
  30. }
  31. reply, ok := accept(msg)
  32. if !ok {
  33. return
  34. }
  35. _ = c.SetWriteDeadline(time.Now().Add(2 * time.Second))
  36. _ = c.WriteMessage(websocket.TextMessage, reply)
  37. // echo loop
  38. for {
  39. _ = c.SetReadDeadline(time.Now().Add(2 * time.Second))
  40. _, body, err := c.ReadMessage()
  41. if err != nil {
  42. return
  43. }
  44. _ = c.SetWriteDeadline(time.Now().Add(2 * time.Second))
  45. _ = c.WriteMessage(websocket.TextMessage, []byte(`{"echo":`+string(body)+`}`))
  46. }
  47. }))
  48. wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "/ws"
  49. return wsURL, srv
  50. }
  51. func TestConnect_RoundTrip(t *testing.T) {
  52. wsURL, srv := startWSServer(t, func(auth []byte) ([]byte, bool) {
  53. // Expect {"api_key":"..."} and reply with ready.
  54. if !strings.Contains(string(auth), `"api_key"`) {
  55. return []byte(`{"error":"bad auth"}`), false
  56. }
  57. return []byte(`{"ready":true}`), true
  58. })
  59. defer srv.Close()
  60. c, err := Connect(Config{URL: wsURL, APIKey: "acme-001:prom-prod:s3cret-acme"})
  61. if err != nil {
  62. t.Fatal("connect:", err)
  63. }
  64. defer c.Close()
  65. if got := string(c.AuthReply()); got != `{"ready":true}` {
  66. t.Fatalf("auth reply=%q, want ready", got)
  67. }
  68. ack, err := c.SendAlert([]byte(`{"hello":"world"}`))
  69. if err != nil {
  70. t.Fatal("send:", err)
  71. }
  72. if !strings.Contains(string(ack), `"echo":{"hello":"world"}`) {
  73. t.Fatalf("echo ack=%q, want echo", ack)
  74. }
  75. }
  76. func TestConnect_BadAuth(t *testing.T) {
  77. // Server accepts the dial, reads the auth frame, then
  78. // closes. Connect should see the close on the read and
  79. // return the error.
  80. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  81. c, err := (&websocket.Upgrader{}).Upgrade(w, r, nil)
  82. if err != nil {
  83. return
  84. }
  85. _ = c.SetReadDeadline(time.Now().Add(2 * time.Second))
  86. _, _, _ = c.ReadMessage()
  87. _ = c.WriteMessage(websocket.TextMessage, []byte(`{"error":"unauthorized"}`))
  88. _ = c.Close()
  89. }))
  90. defer srv.Close()
  91. wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "/ws"
  92. c, err := Connect(Config{URL: wsURL, APIKey: "x"})
  93. if err != nil {
  94. t.Fatal("connect (first frame should still round-trip):", err)
  95. }
  96. defer c.Close()
  97. if !strings.Contains(string(c.AuthReply()), `"error"`) {
  98. t.Fatalf("expected error in auth reply, got %q", c.AuthReply())
  99. }
  100. }
  101. func TestConnect_RejectsEmptyConfig(t *testing.T) {
  102. if _, err := Connect(Config{URL: "", APIKey: "x"}); err == nil {
  103. t.Fatal("empty URL should error")
  104. }
  105. if _, err := Connect(Config{URL: "ws://x", APIKey: ""}); err == nil {
  106. t.Fatal("empty APIKey should error")
  107. }
  108. }