53 lines
2.0 KiB
Go
53 lines
2.0 KiB
Go
package coreconfig
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
const omniTestSecret = "omnilogger-fields-test-secret-0123456789"
|
||
|
|
|
||
|
|
func TestOmniLoggerFieldsKeepTheirValueAcrossPartialPushes(t *testing.T) {
|
||
|
|
store := NewStore(Info{})
|
||
|
|
store.Set(Info{OmniLoggerURL: "https://logs.example.com/", OmniLoggerIngestToken: "token-one"})
|
||
|
|
if store.OmniLoggerURL() != "https://logs.example.com" || store.OmniLoggerIngestToken() != "token-one" {
|
||
|
|
t.Fatalf("got url=%q token=%q", store.OmniLoggerURL(), store.OmniLoggerIngestToken())
|
||
|
|
}
|
||
|
|
store.Set(Info{AuthGuardURL: "https://auth.example.com"})
|
||
|
|
if store.OmniLoggerIngestToken() != "token-one" {
|
||
|
|
t.Fatal("a push without a token cleared the stored token")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestHandlerNeverEchoesTheOmniLoggerIngestToken(t *testing.T) {
|
||
|
|
store := NewStore(Info{})
|
||
|
|
handler := Handler(omniTestSecret, store)
|
||
|
|
request := httptest.NewRequest(http.MethodPut, "/api/core-services/internal",
|
||
|
|
strings.NewReader(`{"omniLoggerUrl":"https://logs.example.com","omniLoggerIngestToken":"token-two"}`))
|
||
|
|
request.Header.Set("X-Internal-Auth", omniTestSecret)
|
||
|
|
response := httptest.NewRecorder()
|
||
|
|
handler.ServeHTTP(response, request)
|
||
|
|
if response.Code != http.StatusOK {
|
||
|
|
t.Fatalf("status = %d: %s", response.Code, response.Body.String())
|
||
|
|
}
|
||
|
|
if strings.Contains(response.Body.String(), "token-two") {
|
||
|
|
t.Fatalf("response echoed the ingest token: %s", response.Body.String())
|
||
|
|
}
|
||
|
|
if store.OmniLoggerIngestToken() != "token-two" {
|
||
|
|
t.Fatal("the pushed token was not stored")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestOmniLoggerURLMustBeHTTPWithAHost(t *testing.T) {
|
||
|
|
if err := validateServiceURLs(Info{OmniLoggerURL: "ftp://logs.example.com"}); err == nil {
|
||
|
|
t.Fatal("accepted a non-http OmniLogger URL")
|
||
|
|
}
|
||
|
|
if err := validateServiceURLs(Info{OmniLoggerURL: "https://"}); err == nil {
|
||
|
|
t.Fatal("accepted an OmniLogger URL without a host")
|
||
|
|
}
|
||
|
|
if err := validateServiceURLs(Info{OmniLoggerURL: "http://omnilogger:8080"}); err != nil {
|
||
|
|
t.Fatalf("rejected a valid OmniLogger URL: %v", err)
|
||
|
|
}
|
||
|
|
}
|