mirror of
https://github.com/dstotijn/hetty.git
synced 2025-07-01 18:47:29 -04:00
Replace SQLite with BadgerDB
This commit is contained in:
@ -1,124 +1,43 @@
|
||||
package reqlog_test
|
||||
|
||||
//go:generate moq -out proj_mock_test.go -pkg reqlog_test ../proj Service:ProjServiceMock
|
||||
//go:generate moq -out repo_mock_test.go -pkg reqlog_test . Repository:RepoMock
|
||||
//go:generate go run github.com/matryer/moq -out repo_mock_test.go -pkg reqlog_test . Repository:RepoMock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/dstotijn/hetty/pkg/proj"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/oklog/ulid"
|
||||
|
||||
"github.com/dstotijn/hetty/pkg/proxy"
|
||||
"github.com/dstotijn/hetty/pkg/reqlog"
|
||||
"github.com/dstotijn/hetty/pkg/scope"
|
||||
)
|
||||
|
||||
//nolint:gosec
|
||||
var ulidEntropy = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
//nolint:paralleltest
|
||||
func TestNewService(t *testing.T) {
|
||||
projSvcMock := &ProjServiceMock{
|
||||
OnProjectOpenFunc: func(fn proj.OnProjectOpenFn) {},
|
||||
OnProjectCloseFunc: func(fn proj.OnProjectCloseFn) {},
|
||||
}
|
||||
func TestRequestModifier(t *testing.T) {
|
||||
repoMock := &RepoMock{
|
||||
FindSettingsByModuleFunc: func(_ context.Context, _ string, _ interface{}) error {
|
||||
StoreRequestLogFunc: func(_ context.Context, _ reqlog.RequestLog) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
svc := reqlog.NewService(reqlog.Config{
|
||||
ProjectService: projSvcMock,
|
||||
Repository: repoMock,
|
||||
})
|
||||
|
||||
t.Run("registered handlers for project open and close", func(t *testing.T) {
|
||||
got := len(projSvcMock.OnProjectOpenCalls())
|
||||
if exp := 1; exp != got {
|
||||
t.Fatalf("incorrect `proj.Service.OnProjectOpen` calls (expected: %v, got: %v)", exp, got)
|
||||
}
|
||||
|
||||
got = len(projSvcMock.OnProjectCloseCalls())
|
||||
if exp := 1; exp != got {
|
||||
t.Fatalf("incorrect `proj.Service.OnProjectClose` calls (expected: %v, got: %v)", exp, got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("calls handler when project is opened", func(t *testing.T) {
|
||||
// Mock opening a project.
|
||||
err := projSvcMock.OnProjectOpenCalls()[0].Fn("foobar")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error (expected: nil, got: %v)", err)
|
||||
}
|
||||
|
||||
// Assert that settings were fetched from repository, with `svc` as the
|
||||
// destination.
|
||||
got := len(repoMock.FindSettingsByModuleCalls())
|
||||
if exp := 1; exp != got {
|
||||
t.Fatalf("incorrect `proj.Service.OnProjectOpen` calls (expected: %v, got: %v)", exp, got)
|
||||
}
|
||||
|
||||
findSettingsByModuleCall := repoMock.FindSettingsByModuleCalls()[0]
|
||||
expModule := "reqlog"
|
||||
expSettings := svc
|
||||
|
||||
if expModule != findSettingsByModuleCall.Module {
|
||||
t.Fatalf("incorrect `module` argument for `proj.Service.OnProjectOpen` (expected: %v, got: %v)",
|
||||
expModule, findSettingsByModuleCall.Module)
|
||||
}
|
||||
|
||||
if expSettings != findSettingsByModuleCall.Settings {
|
||||
t.Fatalf("incorrect `settings` argument for `proj.Service.OnProjectOpen` (expected: %v, got: %v)",
|
||||
expModule, findSettingsByModuleCall.Settings)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("calls handler when project is closed", func(t *testing.T) {
|
||||
// Mock updating service settings.
|
||||
svc.BypassOutOfScopeRequests = true
|
||||
svc.FindReqsFilter = reqlog.FindRequestsFilter{OnlyInScope: true}
|
||||
|
||||
// Mock closing a project.
|
||||
err := projSvcMock.OnProjectCloseCalls()[0].Fn("foobar")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error (expected: nil, got: %v)", err)
|
||||
}
|
||||
|
||||
// Assert that settings were set to defaults on project close.
|
||||
expBypassOutOfScopeReqs := false
|
||||
expFindReqsFilter := reqlog.FindRequestsFilter{}
|
||||
|
||||
if expBypassOutOfScopeReqs != svc.BypassOutOfScopeRequests {
|
||||
t.Fatalf("incorrect `Service.BypassOutOfScopeRequests` value (expected: %v, got: %v)",
|
||||
expBypassOutOfScopeReqs, svc.BypassOutOfScopeRequests)
|
||||
}
|
||||
|
||||
if expFindReqsFilter != svc.FindReqsFilter {
|
||||
t.Fatalf("incorrect `Service.FindReqsFilter` value (expected: %v, got: %v)",
|
||||
expFindReqsFilter, svc.FindReqsFilter)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//nolint:paralleltest
|
||||
func TestRequestModifier(t *testing.T) {
|
||||
projSvcMock := &ProjServiceMock{
|
||||
OnProjectOpenFunc: func(fn proj.OnProjectOpenFn) {},
|
||||
OnProjectCloseFunc: func(fn proj.OnProjectCloseFn) {},
|
||||
}
|
||||
repoMock := &RepoMock{
|
||||
AddRequestLogFunc: func(_ context.Context, _ http.Request, _ []byte, _ time.Time) (*reqlog.Request, error) {
|
||||
return &reqlog.Request{}, nil
|
||||
},
|
||||
}
|
||||
svc := reqlog.NewService(reqlog.Config{
|
||||
ProjectService: projSvcMock,
|
||||
Repository: repoMock,
|
||||
Repository: repoMock,
|
||||
Scope: &scope.Scope{},
|
||||
})
|
||||
svc.ActiveProjectID = ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy)
|
||||
|
||||
next := func(req *http.Request) {
|
||||
req.Body = ioutil.NopCloser(strings.NewReader("modified body"))
|
||||
req.Body = io.NopCloser(strings.NewReader("modified body"))
|
||||
}
|
||||
reqModFn := svc.RequestModifier(next)
|
||||
req := httptest.NewRequest("GET", "https://example.com/", strings.NewReader("bar"))
|
||||
@ -126,49 +45,54 @@ func TestRequestModifier(t *testing.T) {
|
||||
reqModFn(req)
|
||||
|
||||
t.Run("request log was stored in repository", func(t *testing.T) {
|
||||
got := len(repoMock.AddRequestLogCalls())
|
||||
if exp := 1; exp != got {
|
||||
t.Fatalf("incorrect `proj.Service.AddRequestLog` calls (expected: %v, got: %v)", exp, got)
|
||||
gotCount := len(repoMock.StoreRequestLogCalls())
|
||||
if expCount := 1; expCount != gotCount {
|
||||
t.Fatalf("incorrect `proj.Service.AddRequestLog` calls (expected: %v, got: %v)", expCount, gotCount)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ran next modifier first, before calling repository", func(t *testing.T) {
|
||||
got := repoMock.AddRequestLogCalls()[0].Body
|
||||
if exp := "modified body"; exp != string(got) {
|
||||
t.Fatalf("incorrect `body` argument for `Repository.AddRequestLogCalls` (expected: %v, got: %v)", exp, string(got))
|
||||
exp := reqlog.RequestLog{
|
||||
ID: ulid.ULID{}, // Empty value
|
||||
ProjectID: svc.ActiveProjectID,
|
||||
Method: req.Method,
|
||||
URL: req.URL,
|
||||
Proto: req.Proto,
|
||||
Header: req.Header,
|
||||
Body: []byte("modified body"),
|
||||
}
|
||||
got := repoMock.StoreRequestLogCalls()[0].ReqLog
|
||||
got.ID = ulid.ULID{} // Override to empty value so we can compare against expected value.
|
||||
|
||||
if diff := cmp.Diff(exp, got); diff != "" {
|
||||
t.Fatalf("request log not equal (-exp, +got):\n%v", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//nolint:paralleltest
|
||||
func TestResponseModifier(t *testing.T) {
|
||||
projSvcMock := &ProjServiceMock{
|
||||
OnProjectOpenFunc: func(fn proj.OnProjectOpenFn) {},
|
||||
OnProjectCloseFunc: func(fn proj.OnProjectCloseFn) {},
|
||||
}
|
||||
repoMock := &RepoMock{
|
||||
AddResponseLogFunc: func(_ context.Context, _ int64, _ http.Response,
|
||||
_ []byte, _ time.Time) (*reqlog.Response, error) {
|
||||
return &reqlog.Response{}, nil
|
||||
StoreResponseLogFunc: func(_ context.Context, _ ulid.ULID, _ reqlog.ResponseLog) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
svc := reqlog.NewService(reqlog.Config{
|
||||
ProjectService: projSvcMock,
|
||||
Repository: repoMock,
|
||||
Repository: repoMock,
|
||||
})
|
||||
svc.ActiveProjectID = ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy)
|
||||
|
||||
next := func(res *http.Response) error {
|
||||
res.Body = ioutil.NopCloser(strings.NewReader("modified body"))
|
||||
res.Body = io.NopCloser(strings.NewReader("modified body"))
|
||||
return nil
|
||||
}
|
||||
resModFn := svc.ResponseModifier(next)
|
||||
|
||||
req := httptest.NewRequest("GET", "https://example.com/", strings.NewReader("bar"))
|
||||
req = req.WithContext(context.WithValue(req.Context(), proxy.ReqIDKey, int64(42)))
|
||||
reqLogID := ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy)
|
||||
req = req.WithContext(context.WithValue(req.Context(), proxy.ReqLogIDKey, reqLogID))
|
||||
|
||||
res := &http.Response{
|
||||
Request: req,
|
||||
Body: ioutil.NopCloser(strings.NewReader("bar")),
|
||||
Body: io.NopCloser(strings.NewReader("bar")),
|
||||
}
|
||||
|
||||
if err := resModFn(res); err != nil {
|
||||
@ -178,16 +102,23 @@ func TestResponseModifier(t *testing.T) {
|
||||
t.Run("request log was stored in repository", func(t *testing.T) {
|
||||
// Dirty (but simple) wait for other goroutine to finish calling repository.
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
got := len(repoMock.AddResponseLogCalls())
|
||||
got := len(repoMock.StoreResponseLogCalls())
|
||||
if exp := 1; exp != got {
|
||||
t.Fatalf("incorrect `proj.Service.AddResponseLog` calls (expected: %v, got: %v)", exp, got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ran next modifier first, before calling repository", func(t *testing.T) {
|
||||
got := repoMock.AddResponseLogCalls()[0].Body
|
||||
if exp := "modified body"; exp != string(got) {
|
||||
t.Fatalf("incorrect `body` argument for `Repository.AddResponseLogCalls` (expected: %v, got: %v)", exp, string(got))
|
||||
}
|
||||
t.Run("ran next modifier first, before calling repository", func(t *testing.T) {
|
||||
got := repoMock.StoreResponseLogCalls()[0].ResLog.Body
|
||||
if exp := "modified body"; exp != string(got) {
|
||||
t.Fatalf("incorrect `ResponseLog.Body` value (expected: %v, got: %v)", exp, string(got))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("called repository with request log id", func(t *testing.T) {
|
||||
got := repoMock.StoreResponseLogCalls()[0].ReqLogID
|
||||
if exp := reqLogID; exp.Compare(got) != 0 {
|
||||
t.Fatalf("incorrect `reqLogID` argument for `Repository.AddResponseLogCalls` (expected: %v, got: %v)", exp.String(), got.String())
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user