Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed target parsing #1137

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr

httpConfig := module.HTTP

target = strings.ToLower(target)
if !strings.HasPrefix(target, "http://") && !strings.HasPrefix(target, "https://") {
target = "http://" + target
}
Expand Down
90 changes: 90 additions & 0 deletions prober/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1554,3 +1554,93 @@ func TestBody(t *testing.T) {
}
}
}

func TestHTTPParsingScheme(t *testing.T) {
type serverCreator func(http.Handler) *httptest.Server

testcases := map[string]struct {
target string
serverCreator serverCreator
configHTTPProbe config.HTTPProbe
expectSuccess bool
}{
"lowercase http scheme": {
target: "http://example.com",
serverCreator: httptest.NewServer,
configHTTPProbe: config.HTTPProbe{IPProtocolFallback: true},
expectSuccess: true,
},
"lowercase https scheme": {
target: "https://example.com",
serverCreator: httptest.NewTLSServer,
configHTTPProbe: config.HTTPProbe{
IPProtocolFallback: true,
HTTPClientConfig: pconfig.HTTPClientConfig{
TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: true},
},
},
expectSuccess: true,
},
"uppercase http scheme": {
target: "HTTP://example.com",
serverCreator: httptest.NewServer,
configHTTPProbe: config.HTTPProbe{IPProtocolFallback: true},
expectSuccess: true,
},
"mixed case https scheme": {
target: "HtTpS://example.com",
serverCreator: httptest.NewTLSServer,
configHTTPProbe: config.HTTPProbe{
IPProtocolFallback: true,
HTTPClientConfig: pconfig.HTTPClientConfig{
TLSConfig: pconfig.TLSConfig{InsecureSkipVerify: true},
},
},
expectSuccess: true,
},
"missing scheme": {
target: "example.com",
serverCreator: httptest.NewServer,
configHTTPProbe: config.HTTPProbe{IPProtocolFallback: true},
expectSuccess: true,
},
"invalid scheme": {
target: "invalid://example.com",
serverCreator: httptest.NewServer,
configHTTPProbe: config.HTTPProbe{IPProtocolFallback: true},
expectSuccess: false,
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
ts := tc.serverCreator(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()

// Replace the host inside url to the local test server address
target := strings.Replace(tc.target, "example.com", ts.Listener.Addr().String(), 1)

registry := prometheus.NewRegistry()
testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

result := ProbeHTTP(
testCTX,
target,
config.Module{
Timeout: time.Second,
HTTP: tc.configHTTPProbe,
},

registry,
promslog.NewNopLogger(),
)

if result != tc.expectSuccess {
t.Fatalf("Test %q: expected success: %v, got: %v", name, tc.expectSuccess, result)
}
})
}
}