This commit is contained in:
2025-09-30 00:36:46 +01:00
parent 240929a808
commit 013dfa99fd
11 changed files with 387 additions and 56 deletions
+88 -13
View File
@@ -2,6 +2,7 @@ package gql
import (
"net/http"
"os"
"testing"
"time"
@@ -13,7 +14,8 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
client := NewConnection()
client.SetEndpoint("http://example.com/graphql")
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
assert.Equal(30*time.Second, httpClient.Timeout)
assert.NotNil(httpClient.Transport)
@@ -33,10 +35,22 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
})
suite.T().Run("should create HTTP/2 client for https endpoints", func(t *testing.T) {
// Set environment variable for test to skip TLS verification
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
t.Cleanup(func() {
if oldValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
} else {
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
}
})
client := NewConnection()
client.SetEndpoint("https://example.com/graphql")
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
assert.Equal(30*time.Second, httpClient.Timeout)
assert.NotNil(httpClient.Transport)
@@ -56,7 +70,9 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
client := NewConnection()
client.SetEndpoint("ftp://example.com/graphql")
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.Error(err)
assert.Contains(err.Error(), "invalid endpoint")
assert.Nil(httpClient)
})
@@ -64,21 +80,24 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
client := NewConnection()
client.SetEndpoint("http://example.com/graphql")
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
assert.NotNil(httpClient.CheckRedirect)
// Test redirect policy
req, _ := http.NewRequest("GET", "http://example.com", nil)
err := httpClient.CheckRedirect(req, []*http.Request{})
assert.Equal(http.ErrUseLastResponse, err)
err2 := httpClient.CheckRedirect(req, []*http.Request{})
assert.Equal(http.ErrUseLastResponse, err2)
})
suite.T().Run("should handle empty endpoint", func(t *testing.T) {
client := NewConnection()
client.SetEndpoint("")
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.Error(err)
assert.Contains(err.Error(), "invalid endpoint")
assert.Nil(httpClient)
})
@@ -86,9 +105,35 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
client := NewConnection()
client.SetEndpoint("example.com/graphql")
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.Error(err)
assert.Contains(err.Error(), "invalid endpoint")
assert.Nil(httpClient)
})
suite.T().Run("should enable TLS verification by default for https endpoints", func(t *testing.T) {
// Ensure the environment variable is not set
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
t.Cleanup(func() {
if oldValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
}
})
client := NewConnection()
client.SetEndpoint("https://example.com/graphql")
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
// Check that TLS verification is enabled by default
transport, ok := httpClient.Transport.(*http2.Transport)
assert.True(ok)
assert.NotNil(transport.TLSClientConfig)
assert.False(transport.TLSClientConfig.InsecureSkipVerify) // Should be false by default
})
}
func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
@@ -96,7 +141,9 @@ func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
client := NewConnection()
client.SetEndpoint("http://example.com/graphql")
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
transport := httpClient.Transport.(*http.Transport)
// Verify all transport settings
@@ -112,10 +159,23 @@ func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
})
suite.T().Run("should configure HTTP/2 transport correctly", func(t *testing.T) {
// Set environment variable for test to skip TLS verification
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
t.Cleanup(func() {
if oldValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
} else {
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
}
})
client := NewConnection()
client.SetEndpoint("https://example.com/graphql")
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
transport := httpClient.Transport.(*http2.Transport)
// Verify all HTTP/2 transport settings
@@ -135,17 +195,30 @@ func (suite *Tests) TestBaseClient_createHttpClient_logging() {
// This test verifies that the function runs without error
// The actual logging is tested indirectly through the logger tests
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
})
suite.T().Run("should log HTTP/2 usage", func(t *testing.T) {
// Set environment variable for test to skip TLS verification
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
t.Cleanup(func() {
if oldValue != "" {
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
} else {
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
}
})
client := NewConnection()
client.SetEndpoint("https://example.com/graphql")
// This test verifies that the function runs without error
// The actual logging is tested indirectly through the logger tests
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.NoError(err)
assert.NotNil(httpClient)
})
@@ -155,7 +228,9 @@ func (suite *Tests) TestBaseClient_createHttpClient_logging() {
// This test verifies that the function runs without error
// and returns nil for invalid endpoints (Critical() won't exit in tests)
httpClient := client.createHttpClient()
httpClient, err := client.createHttpClient()
assert.Error(err)
assert.Contains(err.Error(), "invalid endpoint")
assert.Nil(httpClient)
})
}