-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add correlation * update go mod * unit test for correllation id and setting error id in write error with int_correlation_id * fixed printing error id * go mod tidy * add correlation * update go mod * unit test for correllation id and setting error id in write error with int_correlation_id * fixed printing error id * Merged with main, linting issues * misc * integrated changes from utils * bump utils version * minor changes * fix Co-authored-by: SpideyPool192 <[email protected]> Co-authored-by: Stelios <[email protected]>
- Loading branch information
1 parent
1eeb3b3
commit 4cc0014
Showing
6 changed files
with
109 additions
and
29 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,85 @@ | ||
package router_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/allinbits/demeris-api-server/api/config" | ||
"github.com/allinbits/demeris-api-server/api/database" | ||
"github.com/allinbits/demeris-api-server/api/router" | ||
"github.com/allinbits/emeris-utils/logging" | ||
"github.com/allinbits/emeris-utils/store" | ||
"github.com/cockroachdb/cockroach-go/v2/testserver" | ||
"github.com/gofrs/uuid" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest/observer" | ||
) | ||
|
||
func TestCorrelationIDMiddleWare(t *testing.T) { | ||
t.Parallel() | ||
r, cfg, observedLogs, tDown := setup(t) | ||
defer tDown() | ||
require.NotNil(t, r) | ||
|
||
go r.Serve(cfg.ListenAddr) | ||
time.Sleep(2 * time.Second) | ||
|
||
client := http.Client{ | ||
Timeout: 2 * time.Second, | ||
} | ||
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s%s", cfg.ListenAddr, "/chains"), nil) | ||
require.NoError(t, err) | ||
|
||
id, err := uuid.NewV4() | ||
require.NoError(t, err) | ||
|
||
req.Header.Set("X-Correlation-id", fmt.Sprintf("%x", id)) | ||
|
||
_, err = client.Do(req) | ||
require.NoError(t, err) | ||
|
||
require.Eventually(t, func() bool { | ||
count := 0 | ||
for _, info := range observedLogs.All() { | ||
if info.ContextMap()[string(logging.IntCorrelationIDName)] != nil { | ||
count++ | ||
} | ||
if info.ContextMap()[string(logging.CorrelationIDName)] == fmt.Sprintf("%x", id) { | ||
count++ | ||
} | ||
} | ||
return count == 2 | ||
}, 5*time.Second, 1*time.Second) | ||
} | ||
|
||
func setup(t *testing.T) (router.Router, config.Config, *observer.ObservedLogs, func()) { | ||
tServer, err := testserver.NewTestServer() | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, tServer.WaitForInit()) | ||
|
||
connStr := tServer.PGURL().String() | ||
require.NotNil(t, connStr) | ||
|
||
cfg := &config.Config{ | ||
DatabaseConnectionURL: connStr, | ||
ListenAddr: "127.0.0.1:9090", | ||
RedisAddr: "127.0.0.1:6379", | ||
KubernetesNamespace: "emeris", | ||
Debug: true, | ||
} | ||
|
||
db, err := database.Init(cfg) | ||
require.NoError(t, err) | ||
|
||
s, err := store.NewClient(cfg.RedisAddr) | ||
require.NoError(t, err) | ||
|
||
observedZapCore, observedLogs := observer.New(zap.InfoLevel) | ||
observedLogger := zap.New(observedZapCore) | ||
|
||
return *router.New(db, observedLogger.Sugar(), s, nil, "", nil, cfg.Debug), *cfg, observedLogs, func() { tServer.Stop() } | ||
} |
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
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