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://") {
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 {
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.")
+ }
+}
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 {