103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package proxy
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNormalizeChunkBasicProgression(t *testing.T) {
|
|
prev := ""
|
|
|
|
if got := normalizeChunk("abc", &prev); got != "abc" {
|
|
t.Fatalf("expected first chunk to pass through, got %q", got)
|
|
}
|
|
if got := normalizeChunk("abcde", &prev); got != "de" {
|
|
t.Fatalf("expected appended delta, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeChunkPrefixRewindDoesNotReplay(t *testing.T) {
|
|
prev := ""
|
|
|
|
_ = normalizeChunk("abcde", &prev)
|
|
if got := normalizeChunk("abc", &prev); got != "" {
|
|
t.Fatalf("expected rewind chunk to be ignored, got %q", got)
|
|
}
|
|
if prev != "abcde" {
|
|
t.Fatalf("expected previous snapshot to remain longest version, got %q", prev)
|
|
}
|
|
if got := normalizeChunk("abcdef", &prev); got != "f" {
|
|
t.Fatalf("expected only unseen suffix after rewind, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeChunkOverlapDelta(t *testing.T) {
|
|
prev := "hello world"
|
|
|
|
if got := normalizeChunk("world!!!", &prev); got != "!!!" {
|
|
t.Fatalf("expected overlap suffix delta, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestBuildKiroTransportUsesExplicitProxyURL(t *testing.T) {
|
|
transport := buildKiroTransport("http://proxy.local:8080")
|
|
req := &http.Request{URL: mustParseURL(t, "https://q.us-east-1.amazonaws.com")}
|
|
|
|
got, err := transport.Proxy(req)
|
|
if err != nil {
|
|
t.Fatalf("unexpected proxy error: %v", err)
|
|
}
|
|
assertProxyURL(t, got, "http://proxy.local:8080")
|
|
}
|
|
|
|
func TestBuildKiroTransportFallsBackToEnvironmentProxy(t *testing.T) {
|
|
t.Setenv("HTTPS_PROXY", "http://env-proxy.local:2323")
|
|
t.Setenv("NO_PROXY", "")
|
|
t.Setenv("no_proxy", "")
|
|
|
|
transport := buildKiroTransport("")
|
|
req := &http.Request{URL: mustParseURL(t, "https://q.us-east-1.amazonaws.com")}
|
|
|
|
got, err := transport.Proxy(req)
|
|
if err != nil {
|
|
t.Fatalf("unexpected proxy error: %v", err)
|
|
}
|
|
assertProxyURL(t, got, "http://env-proxy.local:2323")
|
|
}
|
|
|
|
func TestInitKiroHttpClientKeepsShortRestTimeout(t *testing.T) {
|
|
InitKiroHttpClient("")
|
|
t.Cleanup(func() { InitKiroHttpClient("") })
|
|
|
|
streamClient := kiroHttpStore.Load()
|
|
restClient := kiroRestHttpStore.Load()
|
|
|
|
if streamClient.Timeout != 5*time.Minute {
|
|
t.Fatalf("expected streaming timeout to be 5m, got %s", streamClient.Timeout)
|
|
}
|
|
if restClient.Timeout != 30*time.Second {
|
|
t.Fatalf("expected REST timeout to stay 30s, got %s", restClient.Timeout)
|
|
}
|
|
}
|
|
|
|
func mustParseURL(t *testing.T, raw string) *url.URL {
|
|
t.Helper()
|
|
parsed, err := url.Parse(raw)
|
|
if err != nil {
|
|
t.Fatalf("invalid test URL: %v", err)
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func assertProxyURL(t *testing.T, got *url.URL, want string) {
|
|
t.Helper()
|
|
if got == nil {
|
|
t.Fatalf("expected proxy URL %q, got nil", want)
|
|
}
|
|
if got.String() != want {
|
|
t.Fatalf("expected proxy URL %q, got %q", want, got.String())
|
|
}
|
|
}
|