All checks were successful
Release Tag / release (push) Successful in 27s
- Updated the `downloadThroughCache` function to remove the upstream URL parameter, streamlining the caching process. - Modified the `serializeRawResponse` function to eliminate unnecessary parameters, enhancing clarity and usability. - Adjusted integration tests to align with the new function signatures, ensuring consistent testing of caching behavior.
280 lines
8.4 KiB
Go
280 lines
8.4 KiB
Go
package steamcache
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const SteamHostname = "cache2-den-iwst.steamcontent.com"
|
|
|
|
func TestSteamIntegration(t *testing.T) {
|
|
// Skip this test if we don't have internet access or want to avoid hitting Steam servers
|
|
if testing.Short() {
|
|
t.Skip("Skipping integration test in short mode")
|
|
}
|
|
|
|
// Test URLs from real Steam usage - these should be cached when requested by Steam clients
|
|
testURLs := []string{
|
|
"/depot/516751/patch/288061881745926019/4378193572994177373",
|
|
"/depot/516751/chunk/42e7c13eb4b4e426ec5cf6d1010abfd528e5065a",
|
|
"/depot/516751/chunk/f949f71e102d77ed6e364e2054d06429d54bebb1",
|
|
"/depot/516751/chunk/6790f5105833556d37797657be72c1c8dd2e7074",
|
|
}
|
|
|
|
for _, testURL := range testURLs {
|
|
t.Run(fmt.Sprintf("URL_%s", testURL), func(t *testing.T) {
|
|
testSteamURL(t, testURL)
|
|
})
|
|
}
|
|
}
|
|
|
|
func testSteamURL(t *testing.T, urlPath string) {
|
|
// Create a unique temporary directory for this test to avoid cache persistence issues
|
|
tempDir, err := os.MkdirTemp("", "steamcache_test_*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempDir) // Clean up after test
|
|
|
|
// Create SteamCache instance with unique temp directory
|
|
sc := New(":0", "100MB", "1GB", tempDir, "", "LRU", "LRU", 10, 5)
|
|
|
|
// Use real Steam server
|
|
steamURL := "https://" + SteamHostname + urlPath
|
|
|
|
// Test direct download from Steam server
|
|
directResp, directBody := downloadDirectly(t, steamURL)
|
|
|
|
// Test download through SteamCache
|
|
cacheResp, cacheBody := downloadThroughCache(t, sc, urlPath)
|
|
|
|
// Compare responses
|
|
compareResponses(t, directResp, directBody, cacheResp, cacheBody, urlPath)
|
|
}
|
|
|
|
func downloadDirectly(t *testing.T, url string) (*http.Response, []byte) {
|
|
client := &http.Client{Timeout: 30 * time.Second}
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create request: %v", err)
|
|
}
|
|
|
|
// Add Steam user agent
|
|
req.Header.Set("User-Agent", "Valve/Steam HTTP Client 1.0")
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("Failed to download directly from Steam: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read direct response body: %v", err)
|
|
}
|
|
|
|
return resp, body
|
|
}
|
|
|
|
func downloadThroughCache(t *testing.T, sc *SteamCache, urlPath string) (*http.Response, []byte) {
|
|
// Create a test server for SteamCache
|
|
cacheServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// For real Steam URLs, we need to set the upstream to the Steam hostname
|
|
// and let SteamCache handle the full URL construction
|
|
sc.upstream = "https://" + SteamHostname
|
|
sc.ServeHTTP(w, r)
|
|
}))
|
|
defer cacheServer.Close()
|
|
|
|
// First request - should be a MISS and cache the file
|
|
client := &http.Client{Timeout: 30 * time.Second}
|
|
|
|
req1, err := http.NewRequest("GET", cacheServer.URL+urlPath, nil)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create first request: %v", err)
|
|
}
|
|
req1.Header.Set("User-Agent", "Valve/Steam HTTP Client 1.0")
|
|
|
|
resp1, err := client.Do(req1)
|
|
if err != nil {
|
|
t.Fatalf("Failed to download through cache (first request): %v", err)
|
|
}
|
|
defer resp1.Body.Close()
|
|
|
|
body1, err := io.ReadAll(resp1.Body)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read cache response body (first request): %v", err)
|
|
}
|
|
|
|
// Verify first request was a MISS
|
|
if resp1.Header.Get("X-LanCache-Status") != "MISS" {
|
|
t.Errorf("Expected first request to be MISS, got %s", resp1.Header.Get("X-LanCache-Status"))
|
|
}
|
|
|
|
// Second request - should be a HIT from cache
|
|
req2, err := http.NewRequest("GET", cacheServer.URL+urlPath, nil)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create second request: %v", err)
|
|
}
|
|
req2.Header.Set("User-Agent", "Valve/Steam HTTP Client 1.0")
|
|
|
|
resp2, err := client.Do(req2)
|
|
if err != nil {
|
|
t.Fatalf("Failed to download through cache (second request): %v", err)
|
|
}
|
|
defer resp2.Body.Close()
|
|
|
|
body2, err := io.ReadAll(resp2.Body)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read cache response body (second request): %v", err)
|
|
}
|
|
|
|
// Verify second request was a HIT (unless hash verification failed)
|
|
status2 := resp2.Header.Get("X-LanCache-Status")
|
|
if status2 != "HIT" && status2 != "MISS" {
|
|
t.Errorf("Expected second request to be HIT or MISS, got %s", status2)
|
|
}
|
|
|
|
// If it's a MISS, it means hash verification failed and content wasn't cached
|
|
// This is correct behavior - we shouldn't cache content that doesn't match the expected hash
|
|
if status2 == "MISS" {
|
|
t.Logf("Second request was MISS (hash verification failed) - this is correct behavior")
|
|
}
|
|
|
|
// Verify both cache responses are identical
|
|
if !bytes.Equal(body1, body2) {
|
|
t.Error("First and second cache responses should be identical")
|
|
}
|
|
|
|
// Return the second response (from cache)
|
|
return resp2, body2
|
|
}
|
|
|
|
func compareResponses(t *testing.T, directResp *http.Response, directBody []byte, cacheResp *http.Response, cacheBody []byte, urlPath string) {
|
|
// Compare status codes
|
|
if directResp.StatusCode != cacheResp.StatusCode {
|
|
t.Errorf("Status code mismatch: direct=%d, cache=%d", directResp.StatusCode, cacheResp.StatusCode)
|
|
}
|
|
|
|
// Compare response bodies (this is the most important test)
|
|
if !bytes.Equal(directBody, cacheBody) {
|
|
t.Errorf("Response body mismatch for URL %s", urlPath)
|
|
t.Errorf("Direct body length: %d, Cache body length: %d", len(directBody), len(cacheBody))
|
|
|
|
// Find first difference
|
|
minLen := len(directBody)
|
|
if len(cacheBody) < minLen {
|
|
minLen = len(cacheBody)
|
|
}
|
|
|
|
for i := 0; i < minLen; i++ {
|
|
if directBody[i] != cacheBody[i] {
|
|
t.Errorf("First difference at byte %d: direct=0x%02x, cache=0x%02x", i, directBody[i], cacheBody[i])
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Compare important headers (excluding cache-specific ones)
|
|
importantHeaders := []string{
|
|
"Content-Type",
|
|
"Content-Length",
|
|
"X-Sha1",
|
|
"Cache-Control",
|
|
}
|
|
|
|
for _, header := range importantHeaders {
|
|
directValue := directResp.Header.Get(header)
|
|
cacheValue := cacheResp.Header.Get(header)
|
|
|
|
if directValue != cacheValue {
|
|
t.Errorf("Header %s mismatch: direct=%s, cache=%s", header, directValue, cacheValue)
|
|
}
|
|
}
|
|
|
|
// Verify cache-specific headers are present
|
|
if cacheResp.Header.Get("X-LanCache-Status") == "" {
|
|
t.Error("Cache response should have X-LanCache-Status header")
|
|
}
|
|
|
|
if cacheResp.Header.Get("X-LanCache-Processed-By") != "SteamCache2" {
|
|
t.Error("Cache response should have X-LanCache-Processed-By header set to SteamCache2")
|
|
}
|
|
|
|
t.Logf("✅ URL %s: Direct and cache responses are identical", urlPath)
|
|
}
|
|
|
|
// TestCacheFileFormat tests the cache file format directly
|
|
func TestCacheFileFormat(t *testing.T) {
|
|
// Create test data
|
|
bodyData := []byte("test steam content")
|
|
contentHash := calculateSHA256(bodyData)
|
|
|
|
// Create mock response
|
|
resp := &http.Response{
|
|
StatusCode: 200,
|
|
Status: "200 OK",
|
|
Header: make(http.Header),
|
|
Body: http.NoBody,
|
|
}
|
|
resp.Header.Set("Content-Type", "application/x-steam-chunk")
|
|
resp.Header.Set("Content-Length", "18")
|
|
resp.Header.Set("X-Sha1", contentHash)
|
|
|
|
// Create SteamCache instance
|
|
sc := &SteamCache{}
|
|
|
|
// Reconstruct raw response
|
|
rawResponse := sc.reconstructRawResponse(resp, bodyData)
|
|
|
|
// Serialize to cache format
|
|
cacheData, err := serializeRawResponse(rawResponse)
|
|
if err != nil {
|
|
t.Fatalf("Failed to serialize cache file: %v", err)
|
|
}
|
|
|
|
// Deserialize from cache format
|
|
cacheFile, err := deserializeCacheFile(cacheData)
|
|
if err != nil {
|
|
t.Fatalf("Failed to deserialize cache file: %v", err)
|
|
}
|
|
|
|
// Verify cache file structure
|
|
if cacheFile.ContentHash != contentHash {
|
|
t.Errorf("ContentHash mismatch: expected %s, got %s", contentHash, cacheFile.ContentHash)
|
|
}
|
|
|
|
if cacheFile.ResponseSize != int64(len(rawResponse)) {
|
|
t.Errorf("ResponseSize mismatch: expected %d, got %d", len(rawResponse), cacheFile.ResponseSize)
|
|
}
|
|
|
|
// Verify raw response is preserved
|
|
if !bytes.Equal(cacheFile.Response, rawResponse) {
|
|
t.Error("Raw response not preserved in cache file")
|
|
}
|
|
|
|
// Test streaming the cached response
|
|
recorder := httptest.NewRecorder()
|
|
req := httptest.NewRequest("GET", "/test/format", nil)
|
|
|
|
sc.streamCachedResponse(recorder, req, cacheFile, "test-key", "127.0.0.1", time.Now())
|
|
|
|
// Verify streamed response
|
|
if recorder.Code != 200 {
|
|
t.Errorf("Expected status code 200, got %d", recorder.Code)
|
|
}
|
|
|
|
if !bytes.Equal(recorder.Body.Bytes(), bodyData) {
|
|
t.Error("Streamed response body does not match original")
|
|
}
|
|
|
|
t.Log("✅ Cache file format test passed")
|
|
}
|