Files
hetty/pkg/db/bolt/reqlog_test.go

111 lines
2.7 KiB
Go
Raw Permalink Normal View History

2025-01-13 23:15:18 +01:00
package bolt_test
2022-01-21 11:45:54 +01:00
import (
"context"
"testing"
"github.com/oklog/ulid/v2"
2025-01-13 23:15:18 +01:00
"go.etcd.io/bbolt"
2022-01-21 11:45:54 +01:00
2025-01-13 23:15:18 +01:00
"github.com/dstotijn/hetty/pkg/db/bolt"
"github.com/dstotijn/hetty/pkg/http"
2025-01-13 23:15:18 +01:00
"github.com/dstotijn/hetty/pkg/proj"
2022-01-21 11:45:54 +01:00
"github.com/dstotijn/hetty/pkg/reqlog"
"github.com/dstotijn/hetty/pkg/testutil"
2022-01-21 11:45:54 +01:00
)
func TestFindRequestLogs(t *testing.T) {
t.Parallel()
t.Run("returns request logs and related response logs", func(t *testing.T) {
t.Parallel()
2025-01-13 23:15:18 +01:00
path := t.TempDir() + "bolt.db"
boltDB, err := bbolt.Open(path, 0o600, nil)
if err != nil {
t.Fatalf("failed to open bolt database: %v", err)
}
db, err := bolt.DatabaseFromBoltDB(boltDB)
2022-01-21 11:45:54 +01:00
if err != nil {
2025-01-13 23:15:18 +01:00
t.Fatalf("failed to create database: %v", err)
2022-01-21 11:45:54 +01:00
}
2025-01-13 23:15:18 +01:00
defer db.Close()
2022-01-21 11:45:54 +01:00
projectID := ulid.Make().String()
2022-01-21 11:45:54 +01:00
err = db.UpsertProject(context.Background(), &proj.Project{
Id: projectID,
2025-01-13 23:15:18 +01:00
})
if err != nil {
t.Fatalf("unexpected error upserting project: %v", err)
}
fixtures := []*reqlog.HttpRequestLog{
2022-01-21 11:45:54 +01:00
{
Id: ulid.Make().String(),
ProjectId: projectID,
Request: &http.Request{
Url: "https://example.com/foobar",
Method: http.Method_METHOD_POST,
Protocol: http.Protocol_PROTOCOL_HTTP11,
Headers: []*http.Header{
{Key: "X-Foo", Value: "baz"},
},
Body: []byte("foo"),
2022-01-21 11:45:54 +01:00
},
Response: &http.Response{
2022-01-21 11:45:54 +01:00
Status: "200 OK",
StatusCode: 200,
Headers: []*http.Header{
{Key: "X-Yolo", Value: "swag"},
2022-01-21 11:45:54 +01:00
},
Body: []byte("bar"),
},
},
{
Id: ulid.Make().String(),
ProjectId: projectID,
Request: &http.Request{
Url: "https://example.com/foo?bar=baz",
Method: http.Method_METHOD_GET,
Protocol: http.Protocol_PROTOCOL_HTTP11,
Headers: []*http.Header{
{Key: "X-Foo", Value: "baz"},
},
Body: []byte("foo"),
},
Response: &http.Response{
Status: "200 OK",
StatusCode: 200,
Headers: []*http.Header{
{Key: "X-Yolo", Value: "swag"},
},
Body: []byte("bar"),
2022-01-21 11:45:54 +01:00
},
},
}
// Store fixtures.
2022-02-28 15:40:13 +01:00
for _, reqLog := range fixtures {
2025-01-13 23:15:18 +01:00
err = db.StoreRequestLog(context.Background(), reqLog)
2022-01-21 11:45:54 +01:00
if err != nil {
t.Fatalf("unexpected error creating request log fixture: %v", err)
}
}
got, err := db.FindRequestLogs(context.Background(), projectID, nil)
2022-01-21 11:45:54 +01:00
if err != nil {
t.Fatalf("unexpected error finding request logs: %v", err)
}
2022-02-28 15:40:13 +01:00
// We expect the found request logs are *reversed*, e.g. newest first.
exp := make([]*reqlog.HttpRequestLog, len(fixtures))
2022-02-28 15:40:13 +01:00
for i, j := 0, len(fixtures)-1; i < j; i, j = i+1, j-1 {
exp[i], exp[j] = fixtures[j], fixtures[i]
}
testutil.ProtoSlicesDiff(t, "request logs not equal", exp, got)
2022-01-21 11:45:54 +01:00
})
}