bouncer/internal/store/testsuite/proxy_repository.go

99 lines
2.1 KiB
Go

package testsuite
import (
"context"
"net/url"
"testing"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
)
type proxyRepositoryTestCase struct {
Name string
Do func(repo store.ProxyRepository) error
}
var proxyRepositoryTestCases = []proxyRepositoryTestCase{
{
Name: "Create proxy",
Do: func(repo store.ProxyRepository) error {
ctx := context.Background()
url, err := url.Parse("http://example.com")
if err != nil {
return errors.WithStack(err)
}
proxy, err := repo.CreateProxy(ctx, url, "*:*")
if err != nil {
return errors.WithStack(err)
}
if proxy == nil {
return errors.Errorf("proxy should not be nil")
}
if proxy.ID == "" {
return errors.Errorf("proxy.ID should not be empty")
}
if proxy.To == nil {
return errors.Errorf("proxy.To should not be nil")
}
if e, g := url.String(), proxy.To.String(); e != g {
return errors.Errorf("proxy.URL.String(): expected '%v', got '%v'", url.String(), proxy.To.String())
}
if proxy.CreatedAt.IsZero() {
return errors.Errorf("proxy.CreatedAt should not be zero value")
}
if proxy.UpdatedAt.IsZero() {
return errors.Errorf("proxy.UpdatedAt should not be zero value")
}
return nil
},
},
{
Name: "Create then get proxy",
Do: func(repo store.ProxyRepository) error {
ctx := context.Background()
url, err := url.Parse("http://example.com")
if err != nil {
return errors.WithStack(err)
}
createdProxy, err := repo.CreateProxy(ctx, url, "*")
if err != nil {
return errors.WithStack(err)
}
foundProxy, err := repo.GetProxy(ctx, createdProxy.ID)
if err != nil {
return errors.WithStack(err)
}
spew.Dump(foundProxy)
return nil
},
},
}
func TestProxyRepository(t *testing.T, repo store.ProxyRepository) {
for _, tc := range proxyRepositoryTestCases {
func(tc proxyRepositoryTestCase) {
t.Run(tc.Name, func(t *testing.T) {
if err := tc.Do(repo); err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
})
}(tc)
}
}