@@ -0,0 +1,112 @@
1+package middleware
2+
3+import (
4+ "net/http"
5+ "net/http/httptest"
6+
7+ . "github.com/onsi/ginkgo"
8+ . "github.com/onsi/ginkgo/extensions/table"
9+ . "github.com/onsi/gomega"
10+)
11+
12+var _ = Describe("HealthCheck suite", func() {
13+ type requestTableInput struct {
14+ healthCheckPaths []string
15+ healthCheckUserAgents []string
16+ requestString string
17+ headers map[string]string
18+ expectedStatus int
19+ expectedBody string
20+ }
21+
22+ DescribeTable("when serving a request",
23+ func(in *requestTableInput) {
24+ req := httptest.NewRequest("", in.requestString, nil)
25+ for k, v := range in.headers {
26+ req.Header.Add(k, v)
27+ }
28+
29+ rw := httptest.NewRecorder()
30+
31+ handler := NewHealthCheck(in.healthCheckPaths, in.healthCheckUserAgents)(http.NotFoundHandler())
32+ handler.ServeHTTP(rw, req)
33+
34+ Expect(rw.Code).To(Equal(in.expectedStatus))
35+ Expect(rw.Body.String()).To(Equal(in.expectedBody))
36+ },
37+ Entry("when requesting the healthcheck path", &requestTableInput{
38+ healthCheckPaths: []string{"/ping"},
39+ healthCheckUserAgents: []string{"hc/1.0"},
40+ requestString: "http://example.com/ping",
41+ headers: map[string]string{},
42+ expectedStatus: 200,
43+ expectedBody: "OK",
44+ }),
45+ Entry("when requesting a different path", &requestTableInput{
46+ healthCheckPaths: []string{"/ping"},
47+ healthCheckUserAgents: []string{"hc/1.0"},
48+ requestString: "http://example.com/different",
49+ headers: map[string]string{},
50+ expectedStatus: 404,
51+ expectedBody: "404 page not found\n",
52+ }),
53+ Entry("with a request from the health check user agent", &requestTableInput{
54+ healthCheckPaths: []string{"/ping"},
55+ healthCheckUserAgents: []string{"hc/1.0"},
56+ requestString: "http://example.com/abc",
57+ headers: map[string]string{
58+ "User-Agent": "hc/1.0",
59+ },
60+ expectedStatus: 200,
61+ expectedBody: "OK",
62+ }),
63+ Entry("with a request from a different user agent", &requestTableInput{
64+ healthCheckPaths: []string{"/ping"},
65+ healthCheckUserAgents: []string{"hc/1.0"},
66+ requestString: "http://example.com/abc",
67+ headers: map[string]string{
68+ "User-Agent": "different",
69+ },
70+ expectedStatus: 404,
71+ expectedBody: "404 page not found\n",
72+ }),
73+ Entry("with multiple paths, request one of the healthcheck paths", &requestTableInput{
74+ healthCheckPaths: []string{"/ping", "/liveness_check", "/readiness_check"},
75+ healthCheckUserAgents: []string{"hc/1.0"},
76+ requestString: "http://example.com/readiness_check",
77+ headers: map[string]string{},
78+ expectedStatus: 200,
79+ expectedBody: "OK",
80+ }),
81+ Entry("with multiple paths, request none of the healthcheck paths", &requestTableInput{
82+ healthCheckPaths: []string{"/ping", "/liveness_check", "/readiness_check"},
83+ healthCheckUserAgents: []string{"hc/1.0"},
84+ requestString: "http://example.com/readiness",
85+ headers: map[string]string{
86+ "User-Agent": "user",
87+ },
88+ expectedStatus: 404,
89+ expectedBody: "404 page not found\n",
90+ }),
91+ Entry("with multiple user agents, request from a health check user agent", &requestTableInput{
92+ healthCheckPaths: []string{"/ping"},
93+ healthCheckUserAgents: []string{"hc/1.0", "GoogleHC/1.0"},
94+ requestString: "http://example.com/abc",
95+ headers: map[string]string{
96+ "User-Agent": "GoogleHC/1.0",
97+ },
98+ expectedStatus: 200,
99+ expectedBody: "OK",
100+ }),
101+ Entry("with multiple user agents, request from none of the health check user agents", &requestTableInput{
102+ healthCheckPaths: []string{"/ping"},
103+ healthCheckUserAgents: []string{"hc/1.0", "GoogleHC/1.0"},
104+ requestString: "http://example.com/abc",
105+ headers: map[string]string{
106+ "User-Agent": "user",
107+ },
108+ expectedStatus: 404,
109+ expectedBody: "404 page not found\n",
110+ }),
111+ )
112+})