43 lines
1.5 KiB
Go
43 lines
1.5 KiB
Go
package configsdk
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type roundTripFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (fn roundTripFunc) RoundTrip(request *http.Request) (*http.Response, error) { return fn(request) }
|
|
|
|
func TestLoadSendsAuthenticationAndGrayTarget(t *testing.T) {
|
|
transport := roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
|
if got := r.Header.Get("Authorization"); got != "Bearer sdk-token" {
|
|
t.Errorf("unexpected authorization header %q", got)
|
|
}
|
|
query := r.URL.Query()
|
|
if query.Get("env") != "PROD" || query.Get("app") != "orders" || query.Get("namespace") != "application" || query.Get("ip") != "10.0.0.8" || query.Get("instance") != "orders-3" {
|
|
t.Errorf("unexpected query: %s", r.URL.RawQuery)
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(`{"data":{"items":{"feature.gray":"on"},"revision":12,"releaseVersion":3,"grayRuleIds":[7]}}`)),
|
|
Request: r,
|
|
}, nil
|
|
})
|
|
|
|
client, err := New(Options{BaseURL: "https://config.test", Env: "PROD", App: "orders", Token: "sdk-token", IP: "10.0.0.8", Instance: "orders-3", HTTPClient: &http.Client{Transport: transport}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := client.Load(context.Background(), "application"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if value := client.GetString("application", "feature.gray", "off"); value != "on" {
|
|
t.Fatalf("unexpected synchronized value %q", value)
|
|
}
|
|
}
|