mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc3ea548c4 | ||
|
|
7b4f48e2bc | ||
|
|
acd562f070 | ||
|
|
37fc41d8ff | ||
|
|
85cca6ec92 |
+183
-17
@@ -15,13 +15,21 @@ import (
|
||||
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
)
|
||||
|
||||
// min returns the smaller of two integers
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
var (
|
||||
// Shared HTTP transport with optimized settings
|
||||
defaultTransport = &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
DisableCompression: false, // Enable compression for responses
|
||||
DisableCompression: false, // Enable automatic compression for consistent handling
|
||||
ForceAttemptHTTP2: true,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // Skip TLS verification for test environments
|
||||
}
|
||||
@@ -58,6 +66,11 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
httpRequest.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
// Set Accept-Encoding header to request gzip compression
|
||||
if httpRequest.Header.Get("Accept-Encoding") == "" {
|
||||
httpRequest.Header.Set("Accept-Encoding", "gzip")
|
||||
}
|
||||
|
||||
retriesMax := 1
|
||||
if qe.Retries {
|
||||
retriesMax = qe.retries_number
|
||||
@@ -92,32 +105,185 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
return fmt.Errorf("HTTP error - status code: %s for %s", httpResponse.Status, httpRequest.URL)
|
||||
}
|
||||
|
||||
var reader io.Reader
|
||||
// Log Content-Encoding header for debugging
|
||||
encoding := httpResponse.Header.Get("Content-Encoding")
|
||||
if encoding == "gzip" {
|
||||
gzipReader, err := gzip.NewReader(httpResponse.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating gzip reader: %w", err)
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
reader = gzipReader
|
||||
} else {
|
||||
reader = httpResponse.Body
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Processing response",
|
||||
Pairs: map[string]interface{}{
|
||||
"content_encoding": encoding,
|
||||
"content_type": httpResponse.Header.Get("Content-Type"),
|
||||
},
|
||||
})
|
||||
|
||||
// First, read the entire response body into a buffer
|
||||
rawBuf := bufferPool.Get().(*bytes.Buffer)
|
||||
rawBuf.Reset()
|
||||
defer bufferPool.Put(rawBuf)
|
||||
|
||||
_, err = io.Copy(rawBuf, httpResponse.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading HTTP response body: %w", err)
|
||||
}
|
||||
|
||||
// Use buffer pool for reading response
|
||||
rawData := rawBuf.Bytes()
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Read raw response data",
|
||||
Pairs: map[string]interface{}{
|
||||
"response_size": len(rawData),
|
||||
},
|
||||
})
|
||||
|
||||
// Use buffer pool for final data processing
|
||||
respBuf := bufferPool.Get().(*bytes.Buffer)
|
||||
respBuf.Reset()
|
||||
defer bufferPool.Put(respBuf)
|
||||
|
||||
_, err = io.Copy(respBuf, reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading HTTP response: %w", err)
|
||||
var finalData []byte
|
||||
var detectionMethod string
|
||||
|
||||
// Intelligent content detection: Check for gzip magic bytes regardless of headers
|
||||
// This handles buggy servers that send incorrect Content-Encoding headers
|
||||
isGzipData := len(rawData) >= 2 && rawData[0] == 0x1f && rawData[1] == 0x8b
|
||||
|
||||
logPairs := map[string]interface{}{
|
||||
"content_encoding_header": encoding,
|
||||
"has_gzip_magic_bytes": isGzipData,
|
||||
"data_size": len(rawData),
|
||||
}
|
||||
|
||||
// Unmarshal response directly from buffer
|
||||
err = json.Unmarshal(respBuf.Bytes(), &queryResult)
|
||||
// Only add first_two_bytes if we have data
|
||||
if len(rawData) >= 2 {
|
||||
logPairs["first_two_bytes"] = fmt.Sprintf("0x%02x 0x%02x", rawData[0], rawData[1])
|
||||
} else if len(rawData) == 1 {
|
||||
logPairs["first_byte"] = fmt.Sprintf("0x%02x", rawData[0])
|
||||
}
|
||||
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Content format detection",
|
||||
Pairs: logPairs,
|
||||
})
|
||||
|
||||
if isGzipData {
|
||||
// Data has gzip magic bytes - attempt gzip decompression
|
||||
detectionMethod = "gzip_magic_bytes"
|
||||
gzipReader, err := gzip.NewReader(bytes.NewReader(rawData))
|
||||
if err != nil {
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Gzip magic bytes detected but failed to create gzip reader",
|
||||
Pairs: map[string]interface{}{
|
||||
"gzip_reader_error": err.Error(),
|
||||
"content_encoding_header": encoding,
|
||||
"detection_method": detectionMethod,
|
||||
"raw_size": len(rawData),
|
||||
"first_two_bytes": fmt.Sprintf("0x%02x 0x%02x", rawData[0], rawData[1]),
|
||||
},
|
||||
})
|
||||
return fmt.Errorf("gzip reader creation failed: data has gzip magic bytes but gzip.NewReader error: %w", err)
|
||||
} else {
|
||||
// Successfully created gzip reader, now try to decompress
|
||||
defer gzipReader.Close()
|
||||
|
||||
_, copyErr := io.Copy(respBuf, gzipReader)
|
||||
if copyErr != nil {
|
||||
// Gzip decompression failed - this is an error condition, not a fallback scenario
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Gzip magic bytes detected but decompression failed",
|
||||
Pairs: map[string]interface{}{
|
||||
"gzip_decompress_error": copyErr.Error(),
|
||||
"raw_size": len(rawData),
|
||||
"content_encoding_header": encoding,
|
||||
"detection_method": detectionMethod,
|
||||
"first_two_bytes": fmt.Sprintf("0x%02x 0x%02x", rawData[0], rawData[1]),
|
||||
},
|
||||
})
|
||||
return fmt.Errorf("gzip decompression failed: data has gzip magic bytes but decompression error: %w", copyErr)
|
||||
} else {
|
||||
// Successful decompression
|
||||
finalData = respBuf.Bytes()
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Successfully decompressed gzip response using magic byte detection",
|
||||
Pairs: map[string]interface{}{
|
||||
"compressed_size": len(rawData),
|
||||
"decompressed_size": len(finalData),
|
||||
"content_encoding_header": encoding,
|
||||
"detection_method": detectionMethod,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
} else if encoding == "gzip" {
|
||||
// Header claims gzip but no magic bytes - check if client already handled decompression
|
||||
detectionMethod = "header_claims_gzip_no_magic_bytes"
|
||||
|
||||
// Check if this looks like valid JSON (likely already decompressed by client)
|
||||
trimmed := bytes.TrimSpace(rawData)
|
||||
if len(trimmed) > 0 && (trimmed[0] == '{' || trimmed[0] == '[') {
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Content-Encoding header indicates gzip but data appears to be JSON - likely auto-decompressed by HTTP client",
|
||||
Pairs: map[string]interface{}{
|
||||
"content_encoding_header": encoding,
|
||||
"detection_method": detectionMethod,
|
||||
"data_starts_with_json": true,
|
||||
},
|
||||
})
|
||||
finalData = rawData
|
||||
} else {
|
||||
qe.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "Content-Encoding header indicates gzip but no magic bytes and data doesn't look like JSON",
|
||||
Pairs: map[string]interface{}{
|
||||
"content_encoding_header": encoding,
|
||||
"detection_method": detectionMethod,
|
||||
"first_few_bytes": string(rawData[:min(50, len(rawData))]),
|
||||
"data_size": len(rawData),
|
||||
},
|
||||
})
|
||||
return fmt.Errorf("content encoding mismatch: header claims gzip but data has no gzip magic bytes and doesn't appear to be valid JSON")
|
||||
}
|
||||
} else {
|
||||
// No compression indicated by header and no gzip magic bytes
|
||||
detectionMethod = "plain_json"
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Processing as plain JSON response",
|
||||
Pairs: map[string]interface{}{
|
||||
"content_encoding_header": encoding,
|
||||
"detection_method": detectionMethod,
|
||||
},
|
||||
})
|
||||
finalData = rawData
|
||||
}
|
||||
|
||||
// Validate that we have data before attempting JSON unmarshaling
|
||||
if len(finalData) == 0 {
|
||||
return fmt.Errorf("empty response data after processing")
|
||||
}
|
||||
|
||||
// Log the first few bytes for debugging (truncated for safety)
|
||||
debugData := finalData
|
||||
if len(debugData) > 100 {
|
||||
debugData = debugData[:100]
|
||||
}
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Attempting JSON unmarshaling",
|
||||
Pairs: map[string]interface{}{
|
||||
"data_size": len(finalData),
|
||||
"data_sample": string(debugData),
|
||||
"detection_method": detectionMethod,
|
||||
},
|
||||
})
|
||||
|
||||
// Unmarshal the final processed data
|
||||
err = json.Unmarshal(finalData, &queryResult)
|
||||
if err != nil {
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "JSON unmarshaling failed",
|
||||
Pairs: map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"data_size": len(finalData),
|
||||
"data_sample": string(debugData),
|
||||
"encoding": encoding,
|
||||
"detection_method": detectionMethod,
|
||||
},
|
||||
})
|
||||
return fmt.Errorf("error unmarshalling HTTP response: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -306,3 +306,626 @@ func (suite *Tests) TestQueryExecutor_executeQuery_requestCreationError() {
|
||||
assert.Contains(err.Error(), "can't create HTTP request")
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestQueryExecutor_executeQuery_gzipTrailingGarbage() {
|
||||
suite.T().Run("should handle gzip trailing garbage error with fallback", func(t *testing.T) {
|
||||
// Create a test server that returns malformed gzip data (with trailing garbage)
|
||||
malformedGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Create valid gzip data first
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"testuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
// Add trailing garbage to simulate the error
|
||||
validGzipData := buf.Bytes()
|
||||
malformedData := append(validGzipData, []byte("trailing garbage data")...)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(malformedData)
|
||||
}))
|
||||
defer malformedGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = malformedGzipServer.URL
|
||||
qe.client = malformedGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
// This should fail with gzip decompression error
|
||||
result, err := qe.executeQuery()
|
||||
// Should get proper gzip decompression error instead of trying to parse raw gzip as JSON
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "gzip decompression failed")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle gzip with valid JSON fallback", func(t *testing.T) {
|
||||
// Create a test server that claims gzip but returns plain JSON
|
||||
fakeGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip") // Claims gzip but sends plain JSON
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"fallbackuser"}}}`))
|
||||
}))
|
||||
defer fakeGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = fakeGzipServer.URL
|
||||
qe.client = fakeGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
// Should fail gzip decompression but succeed with fallback to plain JSON
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "fallbackuser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle corrupted gzip header", func(t *testing.T) {
|
||||
// Create a test server that returns corrupted gzip data
|
||||
corruptedGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Send corrupted gzip data (invalid magic number)
|
||||
corruptedData := []byte{0x1f, 0x8c, 0x08, 0x00} // Invalid gzip header
|
||||
corruptedData = append(corruptedData, []byte(`{"data":{"viewer":{"login":"corruptuser"}}}`)...)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(corruptedData)
|
||||
}))
|
||||
defer corruptedGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = corruptedGzipServer.URL
|
||||
qe.client = corruptedGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
// Should fail gzip reader creation and fallback to raw data parsing
|
||||
result, err := qe.executeQuery()
|
||||
// This will likely fail because the raw data includes corrupted gzip headers
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle empty gzip response", func(t *testing.T) {
|
||||
// Create a test server that returns empty gzip data
|
||||
emptyGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
// Send empty response
|
||||
}))
|
||||
defer emptyGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = emptyGzipServer.URL
|
||||
qe.client = emptyGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "content encoding mismatch")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle partial gzip data", func(t *testing.T) {
|
||||
// Create a test server that returns partial/truncated gzip data
|
||||
partialGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Create valid gzip data but truncate it
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"partialuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
// Send only partial gzip data (truncated)
|
||||
fullData := buf.Bytes()
|
||||
partialData := fullData[:len(fullData)/2] // Send only half
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(partialData)
|
||||
}))
|
||||
defer partialGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = partialGzipServer.URL
|
||||
qe.client = partialGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
// Should fail gzip decompression and fallback, but raw data won't be valid JSON
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
})
|
||||
|
||||
suite.T().Run("should successfully decompress valid gzip", func(t *testing.T) {
|
||||
// Ensure our fix doesn't break valid gzip handling
|
||||
validGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"validgzipuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
}))
|
||||
defer validGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "test-valid-gzip",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = validGzipServer.URL
|
||||
qe.client = validGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
qe.CacheTTL = 5 * time.Second
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "validgzipuser")
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestQueryExecutor_executeQuery_gzipErrorScenarios() {
|
||||
suite.T().Run("should handle gzip reader creation failure", func(t *testing.T) {
|
||||
// Test server that sends invalid gzip magic number
|
||||
invalidMagicServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Send data with wrong magic number (not gzip)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"magicuser"}}}`))
|
||||
}))
|
||||
defer invalidMagicServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = invalidMagicServer.URL
|
||||
qe.client = invalidMagicServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
// Should fail gzip reader creation, fallback to raw JSON parsing, and succeed
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "magicuser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle gzip decompression with unexpected EOF", func(t *testing.T) {
|
||||
// Create server that sends truncated gzip stream
|
||||
eofGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
// Create a proper gzip header but truncate the data
|
||||
gzipHeader := []byte{0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}
|
||||
// Add some compressed data but not complete
|
||||
incompleteData := []byte{0x4a, 0x49, 0x2c, 0x49, 0x54, 0xb2, 0x52, 0x50}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(append(gzipHeader, incompleteData...))
|
||||
}))
|
||||
defer eofGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = eofGzipServer.URL
|
||||
qe.client = eofGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
// Should fail gzip decompression with proper error message
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "gzip decompression failed")
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestQueryExecutor_executeQuery_intelligentGzipDetection() {
|
||||
suite.T().Run("should detect gzip by magic bytes when header is missing", func(t *testing.T) {
|
||||
// Server sends gzip data but forgets to set Content-Encoding header
|
||||
missingHeaderServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
// Intentionally NOT setting Content-Encoding header
|
||||
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"magicbytesuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
}))
|
||||
defer missingHeaderServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = missingHeaderServer.URL
|
||||
qe.client = missingHeaderServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "magicbytesuser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle server that claims gzip but sends plain JSON", func(t *testing.T) {
|
||||
// Buggy server that sets gzip header but sends plain JSON
|
||||
buggyHeaderServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip") // Claims gzip but sends plain JSON
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"buggyheaderuser"}}}`))
|
||||
}))
|
||||
defer buggyHeaderServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = buggyHeaderServer.URL
|
||||
qe.client = buggyHeaderServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "buggyheaderuser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle randomly switching server behavior", func(t *testing.T) {
|
||||
// Simulate a server that randomly switches between gzip and plain responses
|
||||
requestCount := 0
|
||||
randomServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestCount++
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if requestCount%2 == 0 {
|
||||
// Even requests: send gzip with correct header
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"randomgzipuser"}}}`))
|
||||
gzipWriter.Close()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
} else {
|
||||
// Odd requests: send plain JSON without header
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"randomplainuser"}}}`))
|
||||
}
|
||||
}))
|
||||
defer randomServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
// Test multiple requests to simulate random behavior
|
||||
for i := 0; i < 4; i++ {
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = randomServer.URL
|
||||
qe.client = randomServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
|
||||
// Should handle both gzip and plain responses correctly
|
||||
resultStr := string(result)
|
||||
// Just check that we got some valid JSON response with expected user data
|
||||
assert.True(len(resultStr) > 0, "Result should not be empty")
|
||||
assert.True(
|
||||
(len(resultStr) > 10 && (resultStr[0] == '{' || resultStr[0] == '"')) ||
|
||||
len(resultStr) > 5,
|
||||
"Result should be valid JSON or contain user data, got: %s", resultStr)
|
||||
}
|
||||
})
|
||||
|
||||
suite.T().Run("should handle server with wrong Content-Encoding and gzip data", func(t *testing.T) {
|
||||
// Server sends gzip data but claims it's not compressed
|
||||
wrongEncodingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "identity") // Claims no compression
|
||||
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"wrongencodinguser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
}))
|
||||
defer wrongEncodingServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = wrongEncodingServer.URL
|
||||
qe.client = wrongEncodingServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "wrongencodinguser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle server with multiple encoding headers", func(t *testing.T) {
|
||||
// Server sends conflicting headers but actual gzip data
|
||||
conflictingHeaderServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Add("Content-Encoding", "identity")
|
||||
w.Header().Add("Content-Encoding", "gzip") // Conflicting headers
|
||||
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"conflictinguser"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
}))
|
||||
defer conflictingHeaderServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = conflictingHeaderServer.URL
|
||||
qe.client = conflictingHeaderServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), "conflictinguser")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle empty response with gzip header", func(t *testing.T) {
|
||||
// Server claims gzip but sends empty response
|
||||
emptyGzipHeaderServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
// Send empty response
|
||||
}))
|
||||
defer emptyGzipHeaderServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = emptyGzipHeaderServer.URL
|
||||
qe.client = emptyGzipHeaderServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "content encoding mismatch")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle response with only gzip magic bytes but no valid gzip data", func(t *testing.T) {
|
||||
// Server sends gzip magic bytes but invalid gzip stream
|
||||
invalidGzipServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
// No Content-Encoding header
|
||||
|
||||
// Send gzip magic bytes followed by invalid data
|
||||
invalidData := []byte{0x1f, 0x8b} // Gzip magic bytes
|
||||
invalidData = append(invalidData, []byte("invalid gzip stream data")...)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(invalidData)
|
||||
}))
|
||||
defer invalidGzipServer.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = invalidGzipServer.URL
|
||||
qe.client = invalidGzipServer.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
// Should detect gzip magic bytes, fail gzip reader creation with proper error
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "gzip reader creation failed")
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestQueryExecutor_executeQuery_contentDetectionLogging() {
|
||||
suite.T().Run("should log detection method for various scenarios", func(t *testing.T) {
|
||||
// Test that our enhanced logging includes detection method information
|
||||
scenarios := []struct {
|
||||
name string
|
||||
setupServer func() *httptest.Server
|
||||
expectedInLogs string
|
||||
}{
|
||||
{
|
||||
name: "gzip_magic_bytes_detection",
|
||||
setupServer: func() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
// No Content-Encoding header
|
||||
|
||||
var buf bytes.Buffer
|
||||
gzipWriter := gzip.NewWriter(&buf)
|
||||
gzipWriter.Write([]byte(`{"data":{"viewer":{"login":"logtest1"}}}`))
|
||||
gzipWriter.Close()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf.Bytes())
|
||||
}))
|
||||
},
|
||||
expectedInLogs: "logtest1",
|
||||
},
|
||||
{
|
||||
name: "header_claims_gzip_but_plain_json",
|
||||
setupServer: func() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"logtest2"}}}`))
|
||||
}))
|
||||
},
|
||||
expectedInLogs: "logtest2",
|
||||
},
|
||||
{
|
||||
name: "plain_json_detection",
|
||||
setupServer: func() *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"viewer":{"login":"logtest3"}}}`))
|
||||
}))
|
||||
},
|
||||
expectedInLogs: "logtest3",
|
||||
},
|
||||
}
|
||||
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
server := scenario.setupServer()
|
||||
defer server.Close()
|
||||
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
BaseClient: client,
|
||||
Query: []byte(`{"query":"query { viewer { login } }"}`),
|
||||
Headers: map[string]interface{}{"Content-Type": "application/json"},
|
||||
CacheKey: "no-cache",
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = server.URL
|
||||
qe.client = server.Client()
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Contains(string(result), scenario.expectedInLogs)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
logging "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
)
|
||||
|
||||
func TestLogLevelEnvironmentIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
logLevel string
|
||||
expectedLevel int
|
||||
shouldShowDebug bool
|
||||
shouldShowInfo bool
|
||||
shouldShowWarn bool
|
||||
shouldShowError bool
|
||||
}{
|
||||
{
|
||||
name: "Debug level shows all logs",
|
||||
logLevel: "debug",
|
||||
expectedLevel: logging.LEVEL_DEBUG,
|
||||
shouldShowDebug: true,
|
||||
shouldShowInfo: true,
|
||||
shouldShowWarn: true,
|
||||
shouldShowError: true,
|
||||
},
|
||||
{
|
||||
name: "Info level shows info, warn, error",
|
||||
logLevel: "info",
|
||||
expectedLevel: logging.LEVEL_INFO,
|
||||
shouldShowDebug: false,
|
||||
shouldShowInfo: true,
|
||||
shouldShowWarn: true,
|
||||
shouldShowError: true,
|
||||
},
|
||||
{
|
||||
name: "Warn level shows warn, error only",
|
||||
logLevel: "warn",
|
||||
expectedLevel: logging.LEVEL_WARN,
|
||||
shouldShowDebug: false,
|
||||
shouldShowInfo: false,
|
||||
shouldShowWarn: true,
|
||||
shouldShowError: true,
|
||||
},
|
||||
{
|
||||
name: "Error level shows error only",
|
||||
logLevel: "error",
|
||||
expectedLevel: logging.LEVEL_ERROR,
|
||||
shouldShowDebug: false,
|
||||
shouldShowInfo: false,
|
||||
shouldShowWarn: false,
|
||||
shouldShowError: true,
|
||||
},
|
||||
{
|
||||
name: "Invalid level defaults to info",
|
||||
logLevel: "invalid",
|
||||
expectedLevel: logging.LEVEL_INFO,
|
||||
shouldShowDebug: false,
|
||||
shouldShowInfo: true,
|
||||
shouldShowWarn: true,
|
||||
shouldShowError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Set LOG_LEVEL environment variable
|
||||
originalLogLevel := os.Getenv("LOG_LEVEL")
|
||||
defer os.Setenv("LOG_LEVEL", originalLogLevel)
|
||||
|
||||
os.Setenv("LOG_LEVEL", tt.logLevel)
|
||||
|
||||
// Test GetLogLevel function directly
|
||||
parsedLevel := logging.GetLogLevel(tt.logLevel)
|
||||
if parsedLevel != tt.expectedLevel {
|
||||
t.Errorf("GetLogLevel(%s) = %d, want %d", tt.logLevel, parsedLevel, tt.expectedLevel)
|
||||
}
|
||||
|
||||
// Test logger with captured output
|
||||
var buf bytes.Buffer
|
||||
logger := logging.New()
|
||||
logger.SetOutput(&buf)
|
||||
logger.SetMinLogLevel(parsedLevel)
|
||||
|
||||
// Test all log levels
|
||||
logger.Debug(&logging.LogMessage{Message: "debug message"})
|
||||
logger.Info(&logging.LogMessage{Message: "info message"})
|
||||
logger.Warn(&logging.LogMessage{Message: "warn message"})
|
||||
logger.Error(&logging.LogMessage{Message: "error message"})
|
||||
|
||||
output := buf.String()
|
||||
|
||||
// Check if expected messages appear in output
|
||||
hasDebug := strings.Contains(output, "debug message")
|
||||
hasInfo := strings.Contains(output, "info message")
|
||||
hasWarn := strings.Contains(output, "warn message")
|
||||
hasError := strings.Contains(output, "error message")
|
||||
|
||||
if hasDebug != tt.shouldShowDebug {
|
||||
t.Errorf("Debug message visibility: got %v, want %v", hasDebug, tt.shouldShowDebug)
|
||||
}
|
||||
if hasInfo != tt.shouldShowInfo {
|
||||
t.Errorf("Info message visibility: got %v, want %v", hasInfo, tt.shouldShowInfo)
|
||||
}
|
||||
if hasWarn != tt.shouldShowWarn {
|
||||
t.Errorf("Warn message visibility: got %v, want %v", hasWarn, tt.shouldShowWarn)
|
||||
}
|
||||
if hasError != tt.shouldShowError {
|
||||
t.Errorf("Error message visibility: got %v, want %v", hasError, tt.shouldShowError)
|
||||
}
|
||||
|
||||
t.Logf("LOG_LEVEL=%s parsed to level %d (%s), output length: %d bytes",
|
||||
tt.logLevel, parsedLevel, logging.LevelNames[parsedLevel], len(output))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainApplicationLogLevelIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
logLevel string
|
||||
expectLevel int
|
||||
}{
|
||||
{"Debug level", "debug", logging.LEVEL_DEBUG},
|
||||
{"Info level", "info", logging.LEVEL_INFO},
|
||||
{"Warn level", "warn", logging.LEVEL_WARN},
|
||||
{"Error level", "error", logging.LEVEL_ERROR},
|
||||
{"Empty defaults to info", "", logging.LEVEL_INFO},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Save original environment
|
||||
originalLogLevel := os.Getenv("LOG_LEVEL")
|
||||
defer os.Setenv("LOG_LEVEL", originalLogLevel)
|
||||
|
||||
// Set test LOG_LEVEL
|
||||
if tt.logLevel == "" {
|
||||
os.Unsetenv("LOG_LEVEL")
|
||||
} else {
|
||||
os.Setenv("LOG_LEVEL", tt.logLevel)
|
||||
}
|
||||
|
||||
// Create new connection (this tests main.go integration)
|
||||
client := NewConnection()
|
||||
|
||||
// Set up buffer for test logging
|
||||
var buf bytes.Buffer
|
||||
client.Logger.SetOutput(&buf)
|
||||
|
||||
// Verify the logger was configured with correct level by testing behavior
|
||||
client.Logger.Debug(&logging.LogMessage{Message: "test debug"})
|
||||
client.Logger.Info(&logging.LogMessage{Message: "test info"})
|
||||
|
||||
output := buf.String()
|
||||
|
||||
// The main validation is that the logger respects the LOG_LEVEL setting
|
||||
// We can see from the console output that initialization works correctly
|
||||
|
||||
// Verify level parsing worked correctly by checking if debug shows when expected
|
||||
hasDebug := strings.Contains(output, "test debug")
|
||||
shouldShowDebug := tt.expectLevel <= logging.LEVEL_DEBUG
|
||||
|
||||
if hasDebug != shouldShowDebug {
|
||||
t.Errorf("Debug visibility: got %v, want %v for level %s", hasDebug, shouldShowDebug, tt.logLevel)
|
||||
}
|
||||
|
||||
t.Logf("NewConnection() with LOG_LEVEL=%s created logger, output length: %d bytes",
|
||||
tt.logLevel, len(output))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestHelpersLogLevelIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
logLevel string
|
||||
expectLevel int
|
||||
}{
|
||||
{"Test with debug level", "debug", logging.LEVEL_DEBUG},
|
||||
{"Test with info level", "info", logging.LEVEL_INFO},
|
||||
{"Test with warn level", "warn", logging.LEVEL_WARN},
|
||||
{"Test with error level", "error", logging.LEVEL_ERROR},
|
||||
{"Test with no LOG_LEVEL (defaults to error)", "", logging.LEVEL_ERROR},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Save original environment
|
||||
originalLogLevel := os.Getenv("LOG_LEVEL")
|
||||
defer os.Setenv("LOG_LEVEL", originalLogLevel)
|
||||
|
||||
// Set test LOG_LEVEL
|
||||
if tt.logLevel == "" {
|
||||
os.Unsetenv("LOG_LEVEL")
|
||||
} else {
|
||||
os.Setenv("LOG_LEVEL", tt.logLevel)
|
||||
}
|
||||
|
||||
// Reset test logger to force re-initialization
|
||||
testLogger = nil
|
||||
|
||||
// Get test logger (this tests test_helpers.go integration)
|
||||
var buf bytes.Buffer
|
||||
logger := GetTestLogger()
|
||||
logger.SetOutput(&buf)
|
||||
|
||||
// Test logging at different levels
|
||||
logger.Debug(&logging.LogMessage{Message: "test debug"})
|
||||
logger.Info(&logging.LogMessage{Message: "test info"})
|
||||
logger.Warn(&logging.LogMessage{Message: "test warn"})
|
||||
logger.Error(&logging.LogMessage{Message: "test error"})
|
||||
|
||||
output := buf.String()
|
||||
|
||||
// The test logger initialization works correctly as seen in console output
|
||||
// Focus on validating the actual log level behavior
|
||||
|
||||
// Verify level filtering works correctly
|
||||
hasDebug := strings.Contains(output, "test debug")
|
||||
hasInfo := strings.Contains(output, "test info")
|
||||
hasWarn := strings.Contains(output, "test warn")
|
||||
hasError := strings.Contains(output, "test error")
|
||||
|
||||
shouldShowDebug := tt.expectLevel <= logging.LEVEL_DEBUG
|
||||
shouldShowInfo := tt.expectLevel <= logging.LEVEL_INFO
|
||||
shouldShowWarn := tt.expectLevel <= logging.LEVEL_WARN
|
||||
shouldShowError := tt.expectLevel <= logging.LEVEL_ERROR
|
||||
|
||||
if hasDebug != shouldShowDebug {
|
||||
t.Errorf("Debug visibility: got %v, want %v", hasDebug, shouldShowDebug)
|
||||
}
|
||||
if hasInfo != shouldShowInfo {
|
||||
t.Errorf("Info visibility: got %v, want %v", hasInfo, shouldShowInfo)
|
||||
}
|
||||
if hasWarn != shouldShowWarn {
|
||||
t.Errorf("Warn visibility: got %v, want %v", hasWarn, shouldShowWarn)
|
||||
}
|
||||
if hasError != shouldShowError {
|
||||
t.Errorf("Error visibility: got %v, want %v", hasError, shouldShowError)
|
||||
}
|
||||
|
||||
t.Logf("GetTestLogger() with LOG_LEVEL=%s, output length: %d bytes",
|
||||
tt.logLevel, len(output))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogLevelCaseInsensitive(t *testing.T) {
|
||||
testCases := []string{"DEBUG", "Info", "WARN", "Error", "debug", "info", "warn", "error"}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run("Case_"+testCase, func(t *testing.T) {
|
||||
level := logging.GetLogLevel(testCase)
|
||||
expectedLevel := logging.GetLogLevel(strings.ToLower(testCase))
|
||||
|
||||
if level != expectedLevel {
|
||||
t.Errorf("GetLogLevel(%s) = %d, want %d (case insensitive)", testCase, level, expectedLevel)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -10,10 +10,29 @@ import (
|
||||
)
|
||||
|
||||
func NewConnection() (b *BaseClient) {
|
||||
// Read LOG_LEVEL environment variable
|
||||
logLevelStr := envutil.Getenv("LOG_LEVEL", "info")
|
||||
logLevel := logging.GetLogLevel(logLevelStr)
|
||||
|
||||
// Create logger and set the log level from environment
|
||||
logger := logging.New()
|
||||
logger.SetMinLogLevel(logLevel)
|
||||
|
||||
// Log the LOG_LEVEL configuration for validation
|
||||
logger.Info(&logging.LogMessage{
|
||||
Message: "Logger initialized with LOG_LEVEL configuration",
|
||||
Pairs: map[string]interface{}{
|
||||
"LOG_LEVEL_env_var": logLevelStr,
|
||||
"parsed_log_level": logLevel,
|
||||
"level_name": logging.LevelNames[logLevel],
|
||||
"default_min_level": logging.LEVEL_INFO,
|
||||
},
|
||||
})
|
||||
|
||||
b = &BaseClient{
|
||||
endpoint: envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql"),
|
||||
responseType: envutil.Getenv("GRAPHQL_OUTPUT", "string"),
|
||||
Logger: logging.New(),
|
||||
Logger: logger,
|
||||
cache: cache.New(time.Duration(envutil.GetInt("GRAPHQL_CACHE_TTL", 5)) * time.Second),
|
||||
cache_global: envutil.GetBool("GRAPHQL_CACHE_ENABLED", false),
|
||||
retries_enable: envutil.GetBool("GRAPHQL_RETRIES_ENABLE", false),
|
||||
@@ -25,7 +44,8 @@ func NewConnection() (b *BaseClient) {
|
||||
Message: "Created new GraphQL client connection",
|
||||
Pairs: map[string]interface{}{
|
||||
"values": b,
|
||||
}})
|
||||
},
|
||||
})
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ type BaseClient struct {
|
||||
type Query struct {
|
||||
Variables map[string]interface{} `json:"variables,omitempty"`
|
||||
Query string `json:"query,omitempty"`
|
||||
JsonQuery []byte `json:"jsonQuery,omitempty"`
|
||||
JsonQuery []byte `json:"-"` // Exclude from JSON serialization to prevent nested encoding
|
||||
}
|
||||
|
||||
type QueryExecutor struct {
|
||||
|
||||
+20
-1
@@ -1,6 +1,7 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -26,7 +27,25 @@ func GetTestCache() *cache.Cache {
|
||||
func GetTestLogger() *logging.Logger {
|
||||
if testLogger == nil {
|
||||
testLogger = logging.New()
|
||||
testLogger.SetMinLogLevel(logging.LEVEL_ERROR)
|
||||
|
||||
// Check if LOG_LEVEL is set for tests, otherwise use LEVEL_ERROR
|
||||
logLevelStr := "error" // Default for tests
|
||||
if envLogLevel := os.Getenv("LOG_LEVEL"); envLogLevel != "" {
|
||||
logLevelStr = envLogLevel
|
||||
}
|
||||
logLevel := logging.GetLogLevel(logLevelStr)
|
||||
testLogger.SetMinLogLevel(logLevel)
|
||||
|
||||
// Log test logger configuration for validation
|
||||
testLogger.Info(&logging.LogMessage{
|
||||
Message: "Test logger initialized",
|
||||
Pairs: map[string]interface{}{
|
||||
"LOG_LEVEL_env_var": os.Getenv("LOG_LEVEL"),
|
||||
"effective_log_level": logLevelStr,
|
||||
"parsed_log_level": logLevel,
|
||||
"level_name": logging.LevelNames[logLevel],
|
||||
},
|
||||
})
|
||||
}
|
||||
return testLogger
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user