mirror of
https://github.com/1f349/violet.git
synced 2024-11-24 12:21:33 +00:00
Add tests and remove full route and redirect performance code
This commit is contained in:
parent
3834787f8f
commit
754fd2d396
@ -146,11 +146,9 @@ func (m *Manager) GetAllRoutes(hosts []string) ([]target.RouteWithActive, error)
|
|||||||
return []target.RouteWithActive{}, nil
|
return []target.RouteWithActive{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
searchString, hostArgs := generateRouteAndRedirectSearch(hosts)
|
|
||||||
|
|
||||||
s := make([]target.RouteWithActive, 0)
|
s := make([]target.RouteWithActive, 0)
|
||||||
|
|
||||||
query, err := m.db.Query(`SELECT source, destination, flags, active FROM routes `+searchString, hostArgs)
|
query, err := m.db.Query(`SELECT source, destination, flags, active FROM routes `)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -188,11 +186,9 @@ func (m *Manager) GetAllRedirects(hosts []string) ([]target.RedirectWithActive,
|
|||||||
return []target.RedirectWithActive{}, nil
|
return []target.RedirectWithActive{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
searchString, hostArgs := generateRouteAndRedirectSearch(hosts)
|
|
||||||
|
|
||||||
s := make([]target.RedirectWithActive, 0)
|
s := make([]target.RedirectWithActive, 0)
|
||||||
|
|
||||||
query, err := m.db.Query(`SELECT source, destination, flags, code, active FROM redirects `+searchString, hostArgs)
|
query, err := m.db.Query(`SELECT source, destination, flags, code, active FROM redirects`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -225,7 +221,9 @@ func (m *Manager) DeleteRedirect(source string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateRouteAndRedirectSearch(hosts []string) (string, []string) {
|
// GenerateHostSearch this should help improve performance
|
||||||
|
// TODO(Melon) discover how to implement this correctly
|
||||||
|
func GenerateHostSearch(hosts []string) (string, []string) {
|
||||||
var searchString strings.Builder
|
var searchString strings.Builder
|
||||||
searchString.WriteString("WHERE ")
|
searchString.WriteString("WHERE ")
|
||||||
|
|
||||||
|
@ -50,3 +50,75 @@ func TestNewManager(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusOK, res.StatusCode)
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
||||||
assert.NotNil(t, ft.req)
|
assert.NotNil(t, ft.req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestManager_GetAllRoutes(t *testing.T) {
|
||||||
|
db, err := sql.Open("sqlite3", "file:GetAllRoutes?mode=memory&cache=shared")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m := NewManager(db, nil)
|
||||||
|
a := []error{
|
||||||
|
m.InsertRoute(target.Route{Src: "example.com"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "test.example.com"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "example.com/hello"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "test.example.com/hello"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "example.org"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "test.example.org"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "example.org/hello"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "test.example.org/hello"}),
|
||||||
|
}
|
||||||
|
for _, i := range a {
|
||||||
|
if i != nil {
|
||||||
|
t.Fatal(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
routes, err := m.GetAllRoutes([]string{"example.com"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, []target.RouteWithActive{
|
||||||
|
{Route: target.Route{Src: "example.com"}, Active: true},
|
||||||
|
{Route: target.Route{Src: "test.example.com"}, Active: true},
|
||||||
|
{Route: target.Route{Src: "example.com/hello"}, Active: true},
|
||||||
|
{Route: target.Route{Src: "test.example.com/hello"}, Active: true},
|
||||||
|
}, routes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManager_GetAllRedirects(t *testing.T) {
|
||||||
|
db, err := sql.Open("sqlite3", "file:GetAllRedirects?mode=memory&cache=shared")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m := NewManager(db, nil)
|
||||||
|
a := []error{
|
||||||
|
m.InsertRoute(target.Route{Src: "example.com"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "test.example.com"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "example.com/hello"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "test.example.com/hello"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "example.org"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "test.example.org"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "example.org/hello"}),
|
||||||
|
m.InsertRoute(target.Route{Src: "test.example.org/hello"}),
|
||||||
|
}
|
||||||
|
for _, i := range a {
|
||||||
|
if i != nil {
|
||||||
|
t.Fatal(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
redirects, err := m.GetAllRoutes([]string{"example.com"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, []target.RouteWithActive{
|
||||||
|
{Route: target.Route{Src: "example.com"}, Active: true},
|
||||||
|
{Route: target.Route{Src: "test.example.com"}, Active: true},
|
||||||
|
{Route: target.Route{Src: "example.com/hello"}, Active: true},
|
||||||
|
{Route: target.Route{Src: "test.example.com/hello"}, Active: true},
|
||||||
|
}, redirects)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateHostSearch(t *testing.T) {
|
||||||
|
query, args := GenerateHostSearch([]string{"example.com", "example.org"})
|
||||||
|
assert.Equal(t, "WHERE source LIKE '%' + ? + '/%' OR source LIKE '%' + ? OR source LIKE '%' + ? + '/%' OR source LIKE '%' + ?", query)
|
||||||
|
assert.Equal(t, []string{"example.com", "example.com", "example.org", "example.org"}, args)
|
||||||
|
}
|
||||||
|
@ -7,6 +7,22 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestRedirect_OnDomain(t *testing.T) {
|
||||||
|
assert.True(t, Route{Src: "example.com"}.OnDomain("example.com"))
|
||||||
|
assert.True(t, Route{Src: "test.example.com"}.OnDomain("example.com"))
|
||||||
|
assert.True(t, Route{Src: "example.com/hello"}.OnDomain("example.com"))
|
||||||
|
assert.True(t, Route{Src: "test.example.com/hello"}.OnDomain("example.com"))
|
||||||
|
assert.False(t, Route{Src: "example.com"}.OnDomain("example.org"))
|
||||||
|
assert.False(t, Route{Src: "test.example.com"}.OnDomain("example.org"))
|
||||||
|
assert.False(t, Route{Src: "example.com/hello"}.OnDomain("example.org"))
|
||||||
|
assert.False(t, Route{Src: "test.example.com/hello"}.OnDomain("example.org"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedirect_HasFlag(t *testing.T) {
|
||||||
|
assert.True(t, Route{Flags: FlagPre | FlagAbs}.HasFlag(FlagPre))
|
||||||
|
assert.False(t, Route{Flags: FlagPre | FlagAbs}.HasFlag(FlagCors))
|
||||||
|
}
|
||||||
|
|
||||||
func TestRedirect_ServeHTTP(t *testing.T) {
|
func TestRedirect_ServeHTTP(t *testing.T) {
|
||||||
a := []struct {
|
a := []struct {
|
||||||
Redirect
|
Redirect
|
||||||
|
@ -27,6 +27,17 @@ func (p *proxyTester) RoundTrip(req *http.Request) (*http.Response, error) {
|
|||||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRoute_OnDomain(t *testing.T) {
|
||||||
|
assert.True(t, Route{Src: "example.com"}.OnDomain("example.com"))
|
||||||
|
assert.True(t, Route{Src: "test.example.com"}.OnDomain("example.com"))
|
||||||
|
assert.True(t, Route{Src: "example.com/hello"}.OnDomain("example.com"))
|
||||||
|
assert.True(t, Route{Src: "test.example.com/hello"}.OnDomain("example.com"))
|
||||||
|
assert.False(t, Route{Src: "example.com"}.OnDomain("example.org"))
|
||||||
|
assert.False(t, Route{Src: "test.example.com"}.OnDomain("example.org"))
|
||||||
|
assert.False(t, Route{Src: "example.com/hello"}.OnDomain("example.org"))
|
||||||
|
assert.False(t, Route{Src: "test.example.com/hello"}.OnDomain("example.org"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestRoute_HasFlag(t *testing.T) {
|
func TestRoute_HasFlag(t *testing.T) {
|
||||||
assert.True(t, Route{Flags: FlagPre | FlagAbs}.HasFlag(FlagPre))
|
assert.True(t, Route{Flags: FlagPre | FlagAbs}.HasFlag(FlagPre))
|
||||||
assert.False(t, Route{Flags: FlagPre | FlagAbs}.HasFlag(FlagCors))
|
assert.False(t, Route{Flags: FlagPre | FlagAbs}.HasFlag(FlagCors))
|
||||||
|
Loading…
Reference in New Issue
Block a user