(t *testing.T)
| 16 | ) |
| 17 | |
| 18 | func TestAccessURL(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | |
| 21 | t.Run("OK", func(t *testing.T) { |
| 22 | t.Parallel() |
| 23 | |
| 24 | var ( |
| 25 | ctx, cancel = context.WithCancel(context.Background()) |
| 26 | report healthcheck.AccessURLReport |
| 27 | resp = []byte("OK") |
| 28 | srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 29 | w.WriteHeader(http.StatusOK) |
| 30 | _, _ = w.Write(resp) |
| 31 | })) |
| 32 | ) |
| 33 | defer cancel() |
| 34 | |
| 35 | report.Run(ctx, &healthcheck.AccessURLReportOptions{ |
| 36 | Client: srv.Client(), |
| 37 | AccessURL: mustURL(t, srv.URL), |
| 38 | }) |
| 39 | |
| 40 | assert.True(t, report.Healthy) |
| 41 | assert.True(t, report.Reachable) |
| 42 | assert.Equal(t, health.SeverityOK, report.Severity) |
| 43 | assert.Equal(t, http.StatusOK, report.StatusCode) |
| 44 | assert.Equal(t, "OK", report.HealthzResponse) |
| 45 | assert.Nil(t, report.Error) |
| 46 | }) |
| 47 | |
| 48 | t.Run("NotSet", func(t *testing.T) { |
| 49 | t.Parallel() |
| 50 | |
| 51 | var ( |
| 52 | ctx, cancel = context.WithCancel(context.Background()) |
| 53 | report healthcheck.AccessURLReport |
| 54 | ) |
| 55 | defer cancel() |
| 56 | |
| 57 | report.Run(ctx, &healthcheck.AccessURLReportOptions{ |
| 58 | Client: &http.Client{}, |
| 59 | AccessURL: nil, |
| 60 | }) |
| 61 | |
| 62 | assert.False(t, report.Healthy) |
| 63 | assert.False(t, report.Reachable) |
| 64 | assert.Equal(t, health.SeverityError, report.Severity) |
| 65 | assert.Equal(t, 0, report.StatusCode) |
| 66 | assert.Equal(t, "", report.HealthzResponse) |
| 67 | require.NotNil(t, report.Error) |
| 68 | assert.Contains(t, *report.Error, health.CodeAccessURLNotSet) |
| 69 | }) |
| 70 | |
| 71 | t.Run("ClientErr", func(t *testing.T) { |
| 72 | t.Parallel() |
| 73 | |
| 74 | var ( |
| 75 | ctx, cancel = context.WithCancel(context.Background()) |
nothing calls this directly
no test coverage detected