mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-20 06:28:13 +00:00
improvements mid may 2025 (#24)
* General improvements and bug fixes. * Improve tests coverage. * fixup! Improve tests coverage. * Update README.md with latest changes. * Fix the uint32 * Resolve issue with race condition for logging. * fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * Fix the test of the rate limiter * Add default ratelimit.json file * Update dependencies. * Significant refactor. * fixup! Significant refactor. * fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025
This commit is contained in:
+60
-39
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -10,25 +11,24 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
libpack_cache "github.com/lukaszraczylo/graphql-monitoring-proxy/cache/memory"
|
||||
libpack_logging "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
|
||||
assertions "github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type Tests struct {
|
||||
suite.Suite
|
||||
app *fiber.App
|
||||
app *fiber.App
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
apiDone chan struct{}
|
||||
}
|
||||
|
||||
var (
|
||||
assert *assertions.Assertions
|
||||
)
|
||||
|
||||
func (suite *Tests) BeforeTest(suiteName, testName string) {
|
||||
}
|
||||
|
||||
func (suite *Tests) SetupTest() {
|
||||
assert = assertions.New(suite.T())
|
||||
// Setup test
|
||||
suite.app = fiber.New(
|
||||
fiber.Config{
|
||||
DisableStartupMessage: true,
|
||||
@@ -40,8 +40,20 @@ func (suite *Tests) SetupTest() {
|
||||
// Initialize a simple in-memory cache client for testing purposes
|
||||
libpack_cache.New(5 * time.Minute)
|
||||
parseConfig()
|
||||
enableApi()
|
||||
StartMonitoringServer()
|
||||
|
||||
// Create context with cancel for cleanup
|
||||
suite.ctx, suite.cancel = context.WithCancel(context.Background())
|
||||
suite.apiDone = make(chan struct{})
|
||||
|
||||
// Start API server in goroutine
|
||||
// Temporarily disable API server in tests to isolate issues
|
||||
// go func() {
|
||||
// enableApi(suite.ctx)
|
||||
// close(suite.apiDone)
|
||||
// }()
|
||||
close(suite.apiDone) // Close immediately since we're not starting the server
|
||||
|
||||
_ = StartMonitoringServer()
|
||||
|
||||
// Update logger with proper synchronization
|
||||
logger := libpack_logging.New().SetMinLogLevel(libpack_logging.GetLogLevel(getDetailsFromEnv("LOG_LEVEL", "info")))
|
||||
@@ -50,29 +62,38 @@ func (suite *Tests) SetupTest() {
|
||||
cfgMutex.Unlock()
|
||||
|
||||
// Setup environment variables here if needed
|
||||
os.Setenv("GMP_TEST_STRING", "testValue")
|
||||
os.Setenv("GMP_TEST_INT", "123")
|
||||
os.Setenv("GMP_TEST_BOOL", "true")
|
||||
os.Setenv("NON_GMP_TEST_INT", "31337")
|
||||
_ = os.Setenv("GMP_TEST_STRING", "testValue")
|
||||
_ = os.Setenv("GMP_TEST_INT", "123")
|
||||
_ = os.Setenv("GMP_TEST_BOOL", "true")
|
||||
_ = os.Setenv("NON_GMP_TEST_INT", "31337")
|
||||
}
|
||||
|
||||
// TearDownTest is run after each test to clean up
|
||||
func (suite *Tests) TearDownTest() {
|
||||
// Cancel context to shutdown API server
|
||||
if suite.cancel != nil {
|
||||
suite.cancel()
|
||||
// Wait for API server to shutdown
|
||||
select {
|
||||
case <-suite.apiDone:
|
||||
case <-time.After(2 * time.Second):
|
||||
// Timeout waiting for shutdown
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown connection pool
|
||||
ShutdownConnectionPool()
|
||||
|
||||
// Clean up environment variables here if needed
|
||||
os.Unsetenv("GMP_TEST_STRING")
|
||||
os.Unsetenv("GMP_TEST_INT")
|
||||
os.Unsetenv("GMP_TEST_BOOL")
|
||||
os.Unsetenv("NON_GMP_TEST_INT")
|
||||
_ = os.Unsetenv("GMP_TEST_STRING")
|
||||
_ = os.Unsetenv("GMP_TEST_INT")
|
||||
_ = os.Unsetenv("GMP_TEST_BOOL")
|
||||
_ = os.Unsetenv("NON_GMP_TEST_INT")
|
||||
}
|
||||
|
||||
// func (suite *Tests) AfterTest(suiteName, testName string) {)
|
||||
|
||||
func TestSuite(t *testing.T) {
|
||||
cfgMutex.Lock()
|
||||
cfg = &config{}
|
||||
cfgMutex.Unlock()
|
||||
parseConfig()
|
||||
StartMonitoringServer()
|
||||
suite.Run(t, new(Tests))
|
||||
}
|
||||
|
||||
@@ -118,33 +139,33 @@ func (suite *Tests) Test_envVariableSetting() {
|
||||
for _, tt := range tests {
|
||||
suite.Run(tt.name, func() {
|
||||
result := getDetailsFromEnv(tt.envKey, tt.defaultValue)
|
||||
assert.Equal(tt.expected, result)
|
||||
assert.Equal(suite.T(), tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *Tests) Test_getDetailsFromEnv() {
|
||||
tests := []struct {
|
||||
defaultValue interface{}
|
||||
expected interface{}
|
||||
name string
|
||||
key string
|
||||
defaultValue interface{}
|
||||
envValue string
|
||||
expected interface{}
|
||||
}{
|
||||
{"string value", "TEST_STRING", "default", "envValue", "envValue"},
|
||||
{"int value", "TEST_INT", 0, "123", 123},
|
||||
{"bool value", "TEST_BOOL", false, "true", true},
|
||||
{"default value", "NON_EXISTENT", "default", "", "default"},
|
||||
{"default", "envValue", "string value", "TEST_STRING", "envValue"},
|
||||
{0, 123, "int value", "TEST_INT", "123"},
|
||||
{false, true, "bool value", "TEST_BOOL", "true"},
|
||||
{"default", "default", "default value", "NON_EXISTENT", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
suite.Run(tt.name, func() {
|
||||
if tt.envValue != "" {
|
||||
os.Setenv("GMP_"+tt.key, tt.envValue)
|
||||
defer os.Unsetenv("GMP_" + tt.key)
|
||||
_ = os.Setenv("GMP_"+tt.key, tt.envValue)
|
||||
defer func() { _ = os.Unsetenv("GMP_" + tt.key) }()
|
||||
}
|
||||
result := getDetailsFromEnv(tt.key, tt.defaultValue)
|
||||
assert.Equal(tt.expected, result)
|
||||
assert.Equal(suite.T(), tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -161,22 +182,22 @@ func (suite *Tests) TestIntrospectionEnvironmentConfig() {
|
||||
for _, env := range varsToSave {
|
||||
if val, exists := os.LookupEnv(env); exists {
|
||||
oldEnv[env] = val
|
||||
os.Unsetenv(env)
|
||||
_ = os.Unsetenv(env)
|
||||
}
|
||||
}
|
||||
defer func() {
|
||||
// Restore original env vars
|
||||
for k, v := range oldEnv {
|
||||
os.Setenv(k, v)
|
||||
_ = os.Setenv(k, v)
|
||||
}
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
envVars map[string]string
|
||||
name string
|
||||
query string
|
||||
wantBlocked bool
|
||||
wantEndpoint string
|
||||
wantBlocked bool
|
||||
}{
|
||||
{
|
||||
name: "basic typename allowed",
|
||||
@@ -245,7 +266,7 @@ func (suite *Tests) TestIntrospectionEnvironmentConfig() {
|
||||
suite.Run(tt.name, func() {
|
||||
// Set test env vars
|
||||
for k, v := range tt.envVars {
|
||||
os.Setenv(k, v)
|
||||
_ = os.Setenv(k, v)
|
||||
}
|
||||
|
||||
// Reset global config with proper synchronization
|
||||
@@ -262,9 +283,9 @@ func (suite *Tests) TestIntrospectionEnvironmentConfig() {
|
||||
ctx.Request().SetBody([]byte(fmt.Sprintf(`{"query": %q}`, tt.query)))
|
||||
|
||||
result := parseGraphQLQuery(ctx)
|
||||
assert.Equal(tt.wantBlocked, result.shouldBlock)
|
||||
assert.Equal(suite.T(), tt.wantBlocked, result.shouldBlock)
|
||||
for k := range tt.envVars {
|
||||
os.Unsetenv(k)
|
||||
_ = os.Unsetenv(k)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user