|
1 | 1 | package server |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/netlify/netlify-commons/router" |
5 | | - "github.com/sirupsen/logrus" |
6 | | - "github.com/stretchr/testify/assert" |
7 | | - "github.com/stretchr/testify/require" |
8 | 4 | "net/http" |
9 | 5 | "net/http/httptest" |
10 | 6 | "os" |
11 | 7 | "strings" |
12 | 8 | "testing" |
| 9 | + |
| 10 | + "github.com/netlify/netlify-commons/router" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
13 | 14 | ) |
14 | 15 |
|
15 | 16 | func init() { |
@@ -42,6 +43,36 @@ func TestServerHealth(t *testing.T) { |
42 | 43 | assert.Equal(t, http.StatusOK, rsp.StatusCode) |
43 | 44 | } |
44 | 45 |
|
| 46 | +type testAPICustomHealth struct{} |
| 47 | + |
| 48 | +func (a *testAPICustomHealth) Start(r router.Router) error { |
| 49 | + r.Get("/", func(w http.ResponseWriter, r *http.Request) *router.HTTPError { |
| 50 | + return nil |
| 51 | + }) |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func (a *testAPICustomHealth) Stop() {} |
| 56 | + |
| 57 | +func (a *testAPICustomHealth) Healthy(w http.ResponseWriter, r *http.Request) *router.HTTPError { |
| 58 | + return router.InternalServerError("healthcheck failed") |
| 59 | +} |
| 60 | + |
| 61 | +func TestServerCustomHealth(t *testing.T) { |
| 62 | + apiDef := new(testAPICustomHealth) |
| 63 | + |
| 64 | + cfg := testConfig() |
| 65 | + svr, err := New(tl(t), "testing", cfg, apiDef) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + testSvr := httptest.NewServer(svr.svr.Handler) |
| 69 | + defer testSvr.Close() |
| 70 | + |
| 71 | + rsp, err := http.Get(testSvr.URL + cfg.HealthPath) |
| 72 | + require.NoError(t, err) |
| 73 | + assert.Equal(t, http.StatusInternalServerError, rsp.StatusCode) |
| 74 | +} |
| 75 | + |
45 | 76 | func tl(t *testing.T) *logrus.Entry { |
46 | 77 | return logrus.WithField("test", t.Name()) |
47 | 78 | } |
|
0 commit comments