Skip to content

Commit

Permalink
Add support for body_path in HTTP probe config.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel committed Dec 14, 2018
1 parent a75399e commit 4a326e2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
35 changes: 23 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package config
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"sync"
"time"
Expand Down Expand Up @@ -52,18 +54,20 @@ type Module struct {

type HTTPProbe struct {
// Defaults to 2xx.
ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"`
ValidHTTPVersions []string `yaml:"valid_http_versions,omitempty"`
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
NoFollowRedirects bool `yaml:"no_follow_redirects,omitempty"`
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp,omitempty"`
FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp,omitempty"`
Body string `yaml:"body,omitempty"`
ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"`
ValidHTTPVersions []string `yaml:"valid_http_versions,omitempty"`
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
NoFollowRedirects bool `yaml:"no_follow_redirects,omitempty"`
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp,omitempty"`
FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp,omitempty"`
Body string `yaml:"body,omitempty"`
BodyPath string `yaml:"body_path,omitempty"`
BodyFile io.Reader
HTTPClientConfig config.HTTPClientConfig `yaml:"http_client_config,inline"`
}

Expand Down Expand Up @@ -135,6 +139,13 @@ func (s *HTTPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := s.HTTPClientConfig.Validate(); err != nil {
return err
}
if s.BodyPath != "" {
file, err := os.Open(s.BodyPath)
if err != nil {
return err
}
s.BodyFile = file
}
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,14 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr

var body io.Reader

// If a body is configured, add it to the request.
// If a body string is configured, add it to the request.
if httpConfig.Body != "" {
body = strings.NewReader(httpConfig.Body)
}
// If a body path is configured, add it to the request.
if httpConfig.BodyFile != nil {
body = httpConfig.BodyFile
}

request, err := http.NewRequest(httpConfig.Method, targetURL.String(), body)
request.Host = origHost
Expand Down
27 changes: 27 additions & 0 deletions prober/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,33 @@ func TestPost(t *testing.T) {
}
}

func TestPostBody(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
}
}))
defer ts.Close()

recorder := httptest.NewRecorder()
registry := prometheus.NewRegistry()
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result := ProbeHTTP(testCTX, ts.URL,
config.Module{
Timeout: time.Second,
HTTP: config.HTTPProbe{
IPProtocolFallback: true,
Method: "POST",
BodyPath: "./http.go",
},
}, registry, log.NewNopLogger())
body := recorder.Body.String()
if !result {
t.Fatalf("Post test failed unexpectedly, got %s", body)
}
}

func TestBasicAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
Expand Down

0 comments on commit 4a326e2

Please sign in to comment.