configureable http headers for the http probe
authorChristoph Hack <christoph@tux21b.org>
Thu, 31 Mar 2016 09:29:18 +0000 (11:29 +0200)
committerChristoph Hack <christoph@tux21b.org>
Thu, 31 Mar 2016 09:29:18 +0000 (11:29 +0200)
http.go
http_test.go
main.go

diff --git a/http.go b/http.go
index 7253d5dde657db8894dee83ecbfad75644f1dc60..3f13fd54bac2a2e5ee9d20b2eeb38aeb79b0c440 100644 (file)
--- a/http.go
+++ b/http.go
@@ -53,9 +53,8 @@ func probeHTTP(target string, w http.ResponseWriter, module Module) (success boo
                redirects = len(via)
                if redirects > 10 || config.NoFollowRedirects {
                        return errors.New("Don't follow redirects")
-               } else {
-                       return nil
                }
+               return nil
        }
 
        if !strings.HasPrefix(target, "http://") && !strings.HasPrefix(target, "https://") {
@@ -71,6 +70,14 @@ func probeHTTP(target string, w http.ResponseWriter, module Module) (success boo
                return
        }
 
+       for key, value := range config.Headers {
+               if strings.Title(key) == "Host" {
+                       request.Host = value
+                       continue
+               }
+               request.Header.Set(key, value)
+       }
+
        resp, err := client.Do(request)
        // Err won't be nil if redirects were turned off. See https://github.com/golang/go/issues/3795
        if err != nil && resp == nil {
index ce1b21f685ce2ba7ad9b2e8b205ce6efd3e9f5c9..cddfe2fb6cdf5c95f048e8d6c309019f6b301364 100644 (file)
@@ -238,3 +238,33 @@ func TestFailIfNotMatchesRegexp(t *testing.T) {
                t.Fatalf("Regexp test failed unexpectedly, got %s", body)
        }
 }
+
+func TestHTTPHeaders(t *testing.T) {
+       headers := map[string]string{
+               "Host":            "my-secret-vhost.com",
+               "User-Agent":      "unsuspicious user",
+               "Accept-Language": "en-US",
+       }
+       ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               for key, value := range headers {
+                       if strings.Title(key) == "Host" {
+                               if r.Host != value {
+                                       t.Errorf("Unexpected host: expected %q, got %q.", value, r.Host)
+                               }
+                               continue
+                       }
+                       if got := r.Header.Get(key); got != value {
+                               t.Errorf("Unexpected value of header %q: expected %q, got %q", key, value, got)
+                       }
+               }
+               w.WriteHeader(http.StatusOK)
+       }))
+       defer ts.Close()
+       recorder := httptest.NewRecorder()
+       result := probeHTTP(ts.URL, recorder, Module{Timeout: time.Second, HTTP: HTTPProbe{
+               Headers: headers,
+       }})
+       if !result {
+               t.Fatalf("Probe failed unexpectedly.")
+       }
+}
diff --git a/main.go b/main.go
index b41678b5400955d306025278638e6a435042bee6..d22bcc19697b7523fc52558e95553d0d7a4ed798 100644 (file)
--- a/main.go
+++ b/main.go
@@ -31,13 +31,14 @@ type Module struct {
 
 type HTTPProbe struct {
        // Defaults to 2xx.
-       ValidStatusCodes       []int    `yaml:"valid_status_codes"`
-       NoFollowRedirects      bool     `yaml:"no_follow_redirects"`
-       FailIfSSL              bool     `yaml:"fail_if_ssl"`
-       FailIfNotSSL           bool     `yaml:"fail_if_not_ssl"`
-       Method                 string   `yaml:"method"`
-       FailIfMatchesRegexp    []string `yaml:"fail_if_matches_regexp"`
-       FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp"`
+       ValidStatusCodes       []int             `yaml:"valid_status_codes"`
+       NoFollowRedirects      bool              `yaml:"no_follow_redirects"`
+       FailIfSSL              bool              `yaml:"fail_if_ssl"`
+       FailIfNotSSL           bool              `yaml:"fail_if_not_ssl"`
+       Method                 string            `yaml:"method"`
+       Headers                map[string]string `yaml:"headers"`
+       FailIfMatchesRegexp    []string          `yaml:"fail_if_matches_regexp"`
+       FailIfNotMatchesRegexp []string          `yaml:"fail_if_not_matches_regexp"`
 }
 
 type QueryResponse struct {