-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More tests for web, diag, and grpc servers
- Loading branch information
Showing
8 changed files
with
328 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package diag | ||
|
||
import ( | ||
"github.com/configcat/configcat-proxy/config" | ||
"github.com/configcat/configcat-proxy/diag/metrics" | ||
"github.com/configcat/configcat-proxy/diag/status" | ||
"github.com/configcat/configcat-proxy/log" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestNewServer(t *testing.T) { | ||
errChan := make(chan error) | ||
conf := config.DiagConfig{ | ||
Port: 5050, | ||
Enabled: true, | ||
Status: config.StatusConfig{Enabled: true}, | ||
Metrics: config.MetricsConfig{Enabled: true}, | ||
} | ||
srv := NewServer(&conf, status.NewReporter(&config.Config{SDKs: map[string]*config.SDKConfig{"sdk": {Key: "key"}}}), metrics.NewReporter(), log.NewNullLogger(), errChan) | ||
go srv.Listen() | ||
|
||
req, _ := http.NewRequest(http.MethodGet, "http://localhost:5050/status", http.NoBody) | ||
resp, err := http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
req, _ = http.NewRequest(http.MethodGet, "http://localhost:5050/metrics", http.NoBody) | ||
resp, err = http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
srv.Shutdown() | ||
|
||
assert.Nil(t, readFromErrChan(errChan)) | ||
} | ||
|
||
func TestNewServer_NotEnabled(t *testing.T) { | ||
errChan := make(chan error) | ||
conf := config.DiagConfig{ | ||
Port: 5050, | ||
Enabled: true, | ||
Status: config.StatusConfig{Enabled: false}, | ||
Metrics: config.MetricsConfig{Enabled: false}, | ||
} | ||
|
||
srv := NewServer(&conf, status.NewReporter(&config.Config{SDKs: map[string]*config.SDKConfig{"sdk": {Key: "key"}}}), metrics.NewReporter(), log.NewNullLogger(), errChan) | ||
go srv.Listen() | ||
|
||
req, _ := http.NewRequest(http.MethodGet, "http://localhost:5050/status", http.NoBody) | ||
resp, err := http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
|
||
req, _ = http.NewRequest(http.MethodGet, "http://localhost:5050/metrics", http.NoBody) | ||
resp, err = http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
|
||
srv.Shutdown() | ||
|
||
assert.Nil(t, readFromErrChan(errChan)) | ||
} | ||
|
||
func TestNewServer_NilReporters(t *testing.T) { | ||
errChan := make(chan error) | ||
conf := config.DiagConfig{ | ||
Port: 5050, | ||
Enabled: true, | ||
Status: config.StatusConfig{Enabled: true}, | ||
Metrics: config.MetricsConfig{Enabled: true}, | ||
} | ||
srv := NewServer(&conf, nil, nil, log.NewNullLogger(), errChan) | ||
go srv.Listen() | ||
|
||
req, _ := http.NewRequest(http.MethodGet, "http://localhost:5050/status", http.NoBody) | ||
resp, err := http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
|
||
req, _ = http.NewRequest(http.MethodGet, "http://localhost:5050/metrics", http.NoBody) | ||
resp, err = http.DefaultClient.Do(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusNotFound, resp.StatusCode) | ||
|
||
srv.Shutdown() | ||
|
||
assert.Nil(t, readFromErrChan(errChan)) | ||
} | ||
|
||
func readFromErrChan(ch chan error) error { | ||
select { | ||
case val, ok := <-ch: | ||
if ok { | ||
return val | ||
} | ||
default: | ||
return nil | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package grpc | ||
|
||
import ( | ||
"github.com/configcat/configcat-proxy/config" | ||
"github.com/configcat/configcat-proxy/internal/testutils" | ||
"github.com/configcat/configcat-proxy/internal/utils" | ||
"github.com/configcat/configcat-proxy/log" | ||
"github.com/configcat/configcat-proxy/sdk" | ||
"github.com/configcat/go-sdk/v9/configcattest" | ||
"github.com/stretchr/testify/assert" | ||
"net/http/httptest" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewServer(t *testing.T) { | ||
errChan := make(chan error) | ||
|
||
key := configcattest.RandomSDKKey() | ||
var h configcattest.Handler | ||
_ = h.SetFlags(key, map[string]*configcattest.Flag{ | ||
"flag": { | ||
Default: "test1", | ||
}, | ||
}) | ||
sdkSrv := httptest.NewServer(&h) | ||
defer sdkSrv.Close() | ||
|
||
ctx := testutils.NewTestSdkContext(&config.SDKConfig{BaseUrl: sdkSrv.URL, Key: key, PollInterval: 1}, nil) | ||
sdkClient := sdk.NewClient(ctx, log.NewNullLogger()) | ||
defer sdkClient.Close() | ||
|
||
srv, _ := NewServer(map[string]sdk.Client{"test": sdkClient}, nil, &config.Config{Grpc: config.GrpcConfig{Port: 5060}}, log.NewNullLogger(), errChan) | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
srv.Listen() | ||
wg.Done() | ||
}() | ||
time.Sleep(1 * time.Second) | ||
srv.Shutdown() | ||
wg.Wait() | ||
|
||
assert.Nil(t, readFromErrChan(errChan)) | ||
} | ||
|
||
func TestNewServer_TLS(t *testing.T) { | ||
utils.UseTempFile(` | ||
-----BEGIN CERTIFICATE----- | ||
MIICrzCCAZcCFDnpdKF+Pg1smjtIXrNdIgxGYEJfMA0GCSqGSIb3DQEBCwUAMBQx | ||
EjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0yMzAzMDEyMTA2NThaFw0yNDAyMjkyMTA2 | ||
NThaMBQxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEP | ||
ADCCAQoCggEBAOiTDTjfAPvJLDZ2mwNvu0pohSHPRzzfZRc16iVI6+ESl0Dwjdjl | ||
yERFO/ts1GQnhE2ggykvoxH4zUy1OCnjTJ+Mm1ryjy4G5ZIILIF9MfFcyma5/5Xd | ||
oOTcDr3ZDTAwFaabKYKisoVMHAJCphencgoyOToW5/HRHMKOEpTJOQWSyNduXYfY | ||
nsWb3hx7WD9NajliW7/Jjbf7UnDtKY2VM2GZWT3ygIH/7SlBqyuXJNqyZXbqfbrP | ||
6mdZQ5wvYsnSUU4kNMtZg/ns+0H5R7PFmRhIRM0nZvJZTO9oHREdm+e2nnZwHyJF | ||
Z26LxE7Qr1bn8+PQSydyQIqeUdaSX2LuXqECAwEAATANBgkqhkiG9w0BAQsFAAOC | ||
AQEAjRoOTe4W4OQ6YOo5kx5sMAozh0Rg6eifS0s8GuxKwfuBop8FEnM3wAfF6x3J | ||
fsik9MmoM4L11HWjttb46UFq/rP3GsA3DLX8i1yBOES+iyCELd5Ss9q1jfr/Jqo3 | ||
cAanE4yl3NNEZoDmMdSj2U11BneKSzHDR+l2hDF9wBifWGI9DQ1ItfA5I6MwnL+0 | ||
J03vcwPSwme4bKC/avAT2oDD7jLGLA+kuhMqHvVq7nXRzs46xyFPBBv7fBxXjPPG | ||
c89d0ISafKtZ9kIKaRrzu2HX+b0fzKr0vtHYDLtC1U5oU7GPB12eupERkmWYlhrw | ||
hDL3X7kt3jEZFkzGV1XL1IJx/g== | ||
-----END CERTIFICATE-----`, func(certFile string) { | ||
utils.UseTempFile(`-----BEGIN PRIVATE KEY----- | ||
MIIEvwIBADANBgkq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1zj2HkqiXOPEkb3d3cC61rXiMK97NpshrpzFx+uMCH8MMu9/CVJEHNKGgAq | ||
6zZQ4LhjaNECgYEA3W4UeprmM2bO64d/iJ9Kk3traLw7c8EdCI+jYeVGOHXsfERQ | ||
ctddKfRCapOBv4wUiry+hFLZm0RJmvYbEHPOs6WDiYd5QeFuMGGBTZ7ahjrtwd3t | ||
2TGUQv6NHmQR/cNIHEG+u0DFi7whPp28vkybAx0HGMG0fyBekGZdY0iYmoECgYEA | ||
3mVOlVYHk9ba1AEsrsErDuSXe/AgQa/E8+YnVek4jqnI7LlfyrHUppFFEcDdUFdB | ||
XVFg+ZP4XXx5p+4EHrbP9NYuWsDm2lY1K2Livb0r+ybBqw0niPjpD6eTYQHdtOcu | ||
ihvZFAWZPL6TJCwhvSvNjOziox5FWnDIFFKuXsqWR9ECgYAfiG1izToF+GX3yUPq | ||
CU+ceTbM2uy3hVnQLvCnraN7hkF02Fa9ZwP6nmnsvhfdaIUP5WLm3A+qMWu/PL0i | ||
F/dUCUF6M/DyihQUnOl+MD9Sg89ZHiftqXSY8jGR14uH4woStyUFHiFbtajmnqV7 | ||
MK4Li/LGWcksyoF+hbPNXMFCIA== | ||
-----END PRIVATE KEY----- | ||
`, func(keyFile string) { | ||
errChan := make(chan error) | ||
tlsConf := config.TlsConfig{ | ||
Enabled: true, | ||
Certificates: []config.CertConfig{ | ||
{Cert: strings.ReplaceAll(certFile, "\\", "/"), Key: strings.ReplaceAll(keyFile, "\\", "/")}, | ||
}, | ||
} | ||
|
||
key := configcattest.RandomSDKKey() | ||
var h configcattest.Handler | ||
_ = h.SetFlags(key, map[string]*configcattest.Flag{ | ||
"flag": { | ||
Default: "test1", | ||
}, | ||
}) | ||
sdkSrv := httptest.NewServer(&h) | ||
defer sdkSrv.Close() | ||
|
||
ctx := testutils.NewTestSdkContext(&config.SDKConfig{BaseUrl: sdkSrv.URL, Key: key, PollInterval: 1}, nil) | ||
sdkClient := sdk.NewClient(ctx, log.NewNullLogger()) | ||
defer sdkClient.Close() | ||
|
||
srv, _ := NewServer(map[string]sdk.Client{"test": sdkClient}, nil, &config.Config{Grpc: config.GrpcConfig{Port: 5060}, Tls: tlsConf}, log.NewNullLogger(), errChan) | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
srv.Listen() | ||
wg.Done() | ||
}() | ||
time.Sleep(1 * time.Second) | ||
srv.Shutdown() | ||
wg.Wait() | ||
|
||
assert.Nil(t, readFromErrChan(errChan)) | ||
}) | ||
|
||
}) | ||
} | ||
|
||
func TestNewServer_TLS_Missing_Cert(t *testing.T) { | ||
errChan := make(chan error) | ||
tlsConf := config.TlsConfig{ | ||
Enabled: true, | ||
Certificates: []config.CertConfig{ | ||
{Cert: "./non-existing.cert", Key: "./non-existing.key"}, | ||
}, | ||
} | ||
|
||
key := configcattest.RandomSDKKey() | ||
var h configcattest.Handler | ||
_ = h.SetFlags(key, map[string]*configcattest.Flag{ | ||
"flag": { | ||
Default: "test1", | ||
}, | ||
}) | ||
sdkSrv := httptest.NewServer(&h) | ||
defer sdkSrv.Close() | ||
|
||
ctx := testutils.NewTestSdkContext(&config.SDKConfig{BaseUrl: sdkSrv.URL, Key: key, PollInterval: 1}, nil) | ||
sdkClient := sdk.NewClient(ctx, log.NewNullLogger()) | ||
defer sdkClient.Close() | ||
|
||
_, err := NewServer(map[string]sdk.Client{"test": sdkClient}, nil, &config.Config{Grpc: config.GrpcConfig{Port: 5060}, Tls: tlsConf}, log.NewDebugLogger(), errChan) | ||
assert.Error(t, err) | ||
} | ||
|
||
func readFromErrChan(ch chan error) error { | ||
select { | ||
case val, ok := <-ch: | ||
if ok { | ||
return val | ||
} | ||
default: | ||
return nil | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.