Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
523 lines
14 KiB
Go
523 lines
14 KiB
Go
package webhook
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
|
|
)
|
|
|
|
func init() {
|
|
util_http.InitGlobalHttpClient()
|
|
}
|
|
|
|
func TestHttpClientSendMessage(t *testing.T) {
|
|
var receivedPayload map[string]interface{}
|
|
var receivedHeaders http.Header
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
receivedHeaders = r.Header
|
|
body, _ := io.ReadAll(r.Body)
|
|
if err := json.Unmarshal(body, &receivedPayload); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer server.Close()
|
|
|
|
cfg := &config{
|
|
endpoint: server.URL,
|
|
authBearerToken: "test-token",
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{
|
|
OldEntry: nil,
|
|
NewEntry: &filer_pb.Entry{
|
|
Name: "test.txt",
|
|
IsDirectory: false,
|
|
},
|
|
}
|
|
|
|
err = client.sendMessage(newWebhookMessage("/test/path", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send message: %v", err)
|
|
}
|
|
|
|
if receivedPayload["key"] != "/test/path" {
|
|
t.Errorf("Expected key '/test/path', got %v", receivedPayload["key"])
|
|
}
|
|
|
|
if receivedPayload["event_type"] != "create" {
|
|
t.Errorf("Expected event_type 'create', got %v", receivedPayload["event_type"])
|
|
}
|
|
|
|
if receivedPayload["message"] == nil {
|
|
t.Error("Expected message to be present")
|
|
}
|
|
|
|
if receivedHeaders.Get("Content-Type") != "application/json" {
|
|
t.Errorf("Expected Content-Type 'application/json', got %s", receivedHeaders.Get("Content-Type"))
|
|
}
|
|
|
|
expectedAuth := "Bearer test-token"
|
|
if receivedHeaders.Get("Authorization") != expectedAuth {
|
|
t.Errorf("Expected Authorization '%s', got %s", expectedAuth, receivedHeaders.Get("Authorization"))
|
|
}
|
|
}
|
|
|
|
func TestHttpClientSendMessageWithoutToken(t *testing.T) {
|
|
var receivedHeaders http.Header
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
receivedHeaders = r.Header
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer server.Close()
|
|
|
|
cfg := &config{
|
|
endpoint: server.URL,
|
|
authBearerToken: "",
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{}
|
|
|
|
err = client.sendMessage(newWebhookMessage("/test/path", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send message: %v", err)
|
|
}
|
|
|
|
if receivedHeaders.Get("Authorization") != "" {
|
|
t.Errorf("Expected no Authorization header, got %s", receivedHeaders.Get("Authorization"))
|
|
}
|
|
}
|
|
|
|
func TestHttpClientSendMessageServerError(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
defer server.Close()
|
|
|
|
cfg := &config{
|
|
endpoint: server.URL,
|
|
authBearerToken: "test-token",
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{}
|
|
|
|
err = client.sendMessage(newWebhookMessage("/test/path", message))
|
|
if err == nil {
|
|
t.Error("Expected error for server error response")
|
|
}
|
|
}
|
|
|
|
func TestHttpClientSendMessageNetworkError(t *testing.T) {
|
|
cfg := &config{
|
|
endpoint: "http://localhost:99999",
|
|
authBearerToken: "",
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{}
|
|
|
|
err = client.sendMessage(newWebhookMessage("/test/path", message))
|
|
if err == nil {
|
|
t.Error("Expected error for network failure")
|
|
}
|
|
}
|
|
|
|
// TestHttpClientFollowsRedirectAsPost verifies that redirects are followed with POST method preserved
|
|
func TestHttpClientFollowsRedirectAsPost(t *testing.T) {
|
|
redirectCalled := false
|
|
finalCalled := false
|
|
var finalMethod string
|
|
var finalBody map[string]interface{}
|
|
|
|
// Create final destination server
|
|
finalServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
finalCalled = true
|
|
finalMethod = r.Method
|
|
body, _ := io.ReadAll(r.Body)
|
|
json.Unmarshal(body, &finalBody)
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer finalServer.Close()
|
|
|
|
// Create redirect server
|
|
redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
redirectCalled = true
|
|
// Return 301 redirect to final server
|
|
http.Redirect(w, r, finalServer.URL, http.StatusMovedPermanently)
|
|
}))
|
|
defer redirectServer.Close()
|
|
|
|
cfg := &config{
|
|
endpoint: redirectServer.URL,
|
|
authBearerToken: "test-token",
|
|
timeoutSeconds: 5,
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{
|
|
NewEntry: &filer_pb.Entry{
|
|
Name: "test.txt",
|
|
},
|
|
}
|
|
|
|
// Send message - should follow redirect and recreate POST request
|
|
err = client.sendMessage(newWebhookMessage("/test/path", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send message: %v", err)
|
|
}
|
|
|
|
if !redirectCalled {
|
|
t.Error("Expected redirect server to be called")
|
|
}
|
|
|
|
if !finalCalled {
|
|
t.Error("Expected final server to be called after redirect")
|
|
}
|
|
|
|
if finalMethod != "POST" {
|
|
t.Errorf("Expected POST method at final destination, got %s", finalMethod)
|
|
}
|
|
|
|
if finalBody["key"] != "/test/path" {
|
|
t.Errorf("Expected key '/test/path' at final destination, got %v", finalBody["key"])
|
|
}
|
|
|
|
// Verify the final URL is cached
|
|
client.endpointMu.RLock()
|
|
cachedURL := client.finalURL
|
|
client.endpointMu.RUnlock()
|
|
|
|
if cachedURL != finalServer.URL {
|
|
t.Errorf("Expected cached URL %s, got %s", finalServer.URL, cachedURL)
|
|
}
|
|
}
|
|
|
|
// TestHttpClientUsesCachedRedirect verifies that subsequent requests use the cached redirect destination
|
|
func TestHttpClientUsesCachedRedirect(t *testing.T) {
|
|
redirectCount := 0
|
|
finalCount := 0
|
|
|
|
// Create final destination server
|
|
finalServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
finalCount++
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer finalServer.Close()
|
|
|
|
// Create redirect server
|
|
redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
redirectCount++
|
|
http.Redirect(w, r, finalServer.URL, http.StatusMovedPermanently)
|
|
}))
|
|
defer redirectServer.Close()
|
|
|
|
cfg := &config{
|
|
endpoint: redirectServer.URL,
|
|
authBearerToken: "test-token",
|
|
timeoutSeconds: 5,
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{
|
|
NewEntry: &filer_pb.Entry{
|
|
Name: "test.txt",
|
|
},
|
|
}
|
|
|
|
// First request - should hit redirect server
|
|
err = client.sendMessage(newWebhookMessage("/test/path1", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send first message: %v", err)
|
|
}
|
|
|
|
if redirectCount != 1 {
|
|
t.Errorf("Expected 1 redirect call, got %d", redirectCount)
|
|
}
|
|
if finalCount != 1 {
|
|
t.Errorf("Expected 1 final call, got %d", finalCount)
|
|
}
|
|
|
|
// Second request - should use cached URL and skip redirect server
|
|
err = client.sendMessage(newWebhookMessage("/test/path2", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send second message: %v", err)
|
|
}
|
|
|
|
if redirectCount != 1 {
|
|
t.Errorf("Expected redirect server to be called only once (cached), got %d calls", redirectCount)
|
|
}
|
|
if finalCount != 2 {
|
|
t.Errorf("Expected 2 final calls, got %d", finalCount)
|
|
}
|
|
}
|
|
|
|
// TestHttpClientPreservesPostMethod verifies POST method is preserved and not converted to GET
|
|
func TestHttpClientPreservesPostMethod(t *testing.T) {
|
|
var receivedMethod string
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
receivedMethod = r.Method
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer server.Close()
|
|
|
|
cfg := &config{
|
|
endpoint: server.URL,
|
|
authBearerToken: "test-token",
|
|
timeoutSeconds: 5,
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{
|
|
NewEntry: &filer_pb.Entry{
|
|
Name: "test.txt",
|
|
},
|
|
}
|
|
|
|
err = client.sendMessage(newWebhookMessage("/test/path", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send message: %v", err)
|
|
}
|
|
|
|
if receivedMethod != "POST" {
|
|
t.Errorf("Expected POST method, got %s", receivedMethod)
|
|
}
|
|
}
|
|
|
|
// TestHttpClientInvalidatesCacheOnError verifies that cache is invalidated when cached URL fails
|
|
func TestHttpClientInvalidatesCacheOnError(t *testing.T) {
|
|
finalServerDown := false // Start with server UP
|
|
originalCallCount := 0
|
|
finalCallCount := 0
|
|
|
|
// Create final destination server that can be toggled
|
|
finalServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
finalCallCount++
|
|
if finalServerDown {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
} else {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
}))
|
|
defer finalServer.Close()
|
|
|
|
// Create redirect server
|
|
redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
originalCallCount++
|
|
http.Redirect(w, r, finalServer.URL, http.StatusMovedPermanently)
|
|
}))
|
|
defer redirectServer.Close()
|
|
|
|
cfg := &config{
|
|
endpoint: redirectServer.URL,
|
|
authBearerToken: "test-token",
|
|
timeoutSeconds: 5,
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{
|
|
NewEntry: &filer_pb.Entry{
|
|
Name: "test.txt",
|
|
},
|
|
}
|
|
|
|
// First request - should follow redirect and cache the final URL
|
|
err = client.sendMessage(newWebhookMessage("/test/path1", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send first message: %v", err)
|
|
}
|
|
|
|
if originalCallCount != 1 {
|
|
t.Errorf("Expected 1 original call, got %d", originalCallCount)
|
|
}
|
|
if finalCallCount != 1 {
|
|
t.Errorf("Expected 1 final call, got %d", finalCallCount)
|
|
}
|
|
|
|
// Verify cache was set
|
|
client.endpointMu.RLock()
|
|
cachedURL := client.finalURL
|
|
client.endpointMu.RUnlock()
|
|
if cachedURL != finalServer.URL {
|
|
t.Errorf("Expected cached URL %s, got %s", finalServer.URL, cachedURL)
|
|
}
|
|
|
|
// Second request with cached URL working - should use cache
|
|
err = client.sendMessage(newWebhookMessage("/test/path2", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send second message: %v", err)
|
|
}
|
|
|
|
if originalCallCount != 1 {
|
|
t.Errorf("Expected still 1 original call (using cache), got %d", originalCallCount)
|
|
}
|
|
if finalCallCount != 2 {
|
|
t.Errorf("Expected 2 final calls, got %d", finalCallCount)
|
|
}
|
|
|
|
// Third request - bring final server DOWN, should invalidate cache and retry with original
|
|
// Flow: cached URL (fail, depth=0) -> clear cache -> retry original (depth=1) -> redirect -> final (fail, depth=2)
|
|
finalServerDown = true
|
|
err = client.sendMessage(newWebhookMessage("/test/path3", message))
|
|
if err == nil {
|
|
t.Error("Expected error when cached URL fails and retry also fails")
|
|
}
|
|
|
|
// originalCallCount: 1 (initial) + 1 (retry after cache invalidation) = 2
|
|
if originalCallCount != 2 {
|
|
t.Errorf("Expected 2 original calls, got %d", originalCallCount)
|
|
}
|
|
// finalCallCount: 2 (previous) + 1 (cached fail) + 1 (retry after redirect) = 4
|
|
if finalCallCount != 4 {
|
|
t.Errorf("Expected 4 final calls, got %d", finalCallCount)
|
|
}
|
|
|
|
// Verify final URL is still set (to the failed destination from the redirect)
|
|
client.endpointMu.RLock()
|
|
finalURLAfterError := client.finalURL
|
|
client.endpointMu.RUnlock()
|
|
if finalURLAfterError != finalServer.URL {
|
|
t.Errorf("Expected finalURL to be %s after error, got %s", finalServer.URL, finalURLAfterError)
|
|
}
|
|
|
|
// Fourth request - bring final server back UP
|
|
// Since cache still has the final URL, it should use it directly
|
|
finalServerDown = false
|
|
err = client.sendMessage(newWebhookMessage("/test/path4", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send fourth message after recovery: %v", err)
|
|
}
|
|
|
|
// Should have used the cached URL directly (no new original call)
|
|
// originalCallCount: still 2
|
|
if originalCallCount != 2 {
|
|
t.Errorf("Expected 2 original calls (using cache), got %d", originalCallCount)
|
|
}
|
|
// finalCallCount: 4 + 1 = 5
|
|
if finalCallCount != 5 {
|
|
t.Errorf("Expected 5 final calls, got %d", finalCallCount)
|
|
}
|
|
|
|
// Verify cache was re-established
|
|
client.endpointMu.RLock()
|
|
reestablishedCache := client.finalURL
|
|
client.endpointMu.RUnlock()
|
|
if reestablishedCache != finalServer.URL {
|
|
t.Errorf("Expected cache to be re-established to %s, got %s", finalServer.URL, reestablishedCache)
|
|
}
|
|
}
|
|
|
|
// TestHttpClientInvalidatesCacheOnNetworkError verifies cache invalidation on network errors
|
|
func TestHttpClientInvalidatesCacheOnNetworkError(t *testing.T) {
|
|
originalCallCount := 0
|
|
var finalServer *httptest.Server
|
|
// Create redirect server
|
|
redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
originalCallCount++
|
|
if finalServer != nil {
|
|
http.Redirect(w, r, finalServer.URL, http.StatusMovedPermanently)
|
|
} else {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}))
|
|
defer redirectServer.Close()
|
|
|
|
// Create final destination server
|
|
finalServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
|
|
cfg := &config{
|
|
endpoint: redirectServer.URL,
|
|
authBearerToken: "test-token",
|
|
timeoutSeconds: 5,
|
|
}
|
|
|
|
client, err := newHTTPClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create HTTP client: %v", err)
|
|
}
|
|
|
|
message := &filer_pb.EventNotification{
|
|
NewEntry: &filer_pb.Entry{
|
|
Name: "test.txt",
|
|
},
|
|
}
|
|
|
|
// First request - establish cache
|
|
err = client.sendMessage(newWebhookMessage("/test/path1", message))
|
|
if err != nil {
|
|
t.Fatalf("Failed to send first message: %v", err)
|
|
}
|
|
|
|
if originalCallCount != 1 {
|
|
t.Errorf("Expected 1 original call, got %d", originalCallCount)
|
|
}
|
|
|
|
// Close final server to simulate network error
|
|
cachedURL := finalServer.URL
|
|
finalServer.Close()
|
|
finalServer = nil
|
|
|
|
// Second request - cached URL is down, should invalidate and retry with original
|
|
err = client.sendMessage(newWebhookMessage("/test/path2", message))
|
|
if err == nil {
|
|
t.Error("Expected error when network fails")
|
|
}
|
|
|
|
if originalCallCount != 2 {
|
|
t.Errorf("Expected 2 original calls (retry after cache invalidation), got %d", originalCallCount)
|
|
}
|
|
|
|
// Verify cache was cleared
|
|
client.endpointMu.RLock()
|
|
clearedCache := client.finalURL
|
|
client.endpointMu.RUnlock()
|
|
if clearedCache == cachedURL {
|
|
t.Errorf("Expected cache to be invalidated, but still has %s", clearedCache)
|
|
}
|
|
}
|