| 17 | ) |
| 18 | |
| 19 | func TestWebsocket(t *testing.T) { |
| 20 | t.Parallel() |
| 21 | |
| 22 | t.Run("OK", func(t *testing.T) { |
| 23 | t.Parallel() |
| 24 | |
| 25 | srv := httptest.NewServer(&healthcheck.WebsocketEchoServer{}) |
| 26 | defer srv.Close() |
| 27 | |
| 28 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 29 | defer cancel() |
| 30 | |
| 31 | u, err := url.Parse(srv.URL) |
| 32 | require.NoError(t, err) |
| 33 | |
| 34 | wsReport := healthcheck.WebsocketReport{} |
| 35 | wsReport.Run(ctx, &healthcheck.WebsocketReportOptions{ |
| 36 | AccessURL: u, |
| 37 | HTTPClient: srv.Client(), |
| 38 | APIKey: "test", |
| 39 | }) |
| 40 | |
| 41 | require.Nil(t, wsReport.Error) |
| 42 | }) |
| 43 | |
| 44 | t.Run("Error", func(t *testing.T) { |
| 45 | t.Parallel() |
| 46 | |
| 47 | srv := httptest.NewServer(&healthcheck.WebsocketEchoServer{ |
| 48 | Error: xerrors.New("test error"), |
| 49 | Code: http.StatusBadRequest, |
| 50 | }) |
| 51 | defer srv.Close() |
| 52 | |
| 53 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 54 | defer cancel() |
| 55 | |
| 56 | u, err := url.Parse(srv.URL) |
| 57 | require.NoError(t, err) |
| 58 | |
| 59 | wsReport := healthcheck.WebsocketReport{} |
| 60 | wsReport.Run(ctx, &healthcheck.WebsocketReportOptions{ |
| 61 | AccessURL: u, |
| 62 | HTTPClient: srv.Client(), |
| 63 | APIKey: "test", |
| 64 | }) |
| 65 | |
| 66 | if assert.NotNil(t, wsReport.Error) { |
| 67 | assert.Contains(t, *wsReport.Error, health.CodeWebsocketDial) |
| 68 | } |
| 69 | require.Equal(t, health.SeverityError, wsReport.Severity) |
| 70 | assert.Equal(t, wsReport.Body, "test error") |
| 71 | assert.Equal(t, wsReport.Code, http.StatusBadRequest) |
| 72 | }) |
| 73 | |
| 74 | t.Run("DismissedError", func(t *testing.T) { |
| 75 | t.Parallel() |
| 76 | |