mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 00:38:33 +02:00
refactor: move task, error and testing utils to separte repo; apply gofumpt
This commit is contained in:
@@ -6,9 +6,9 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
gphttp "github.com/yusing/godoxy/internal/net/gphttp"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
"github.com/yusing/goutils/http/reverseproxy"
|
||||
strutils "github.com/yusing/goutils/strings"
|
||||
)
|
||||
|
||||
@@ -3,7 +3,7 @@ package rules
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/yusing/godoxy/internal/utils/testing"
|
||||
expect "github.com/yusing/goutils/testing"
|
||||
)
|
||||
|
||||
func TestParseCommands(t *testing.T) {
|
||||
@@ -131,9 +131,9 @@ func TestParseCommands(t *testing.T) {
|
||||
cmd := Command{}
|
||||
err := cmd.Parse(tt.input)
|
||||
if tt.wantErr != nil {
|
||||
ExpectError(t, tt.wantErr, err)
|
||||
expect.ErrorIs(t, tt.wantErr, err)
|
||||
} else {
|
||||
ExpectNoError(t, err)
|
||||
expect.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -2,14 +2,13 @@ package rules
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"slices"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
"github.com/yusing/godoxy/internal/route/routes"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
strutils "github.com/yusing/goutils/strings"
|
||||
)
|
||||
|
||||
@@ -248,8 +247,10 @@ var checkers = map[string]struct {
|
||||
},
|
||||
}
|
||||
|
||||
var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
|
||||
var andSeps = [256]uint8{'&': 1, '\n': 1}
|
||||
var (
|
||||
asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
|
||||
andSeps = [256]uint8{'&': 1, '\n': 1}
|
||||
)
|
||||
|
||||
func indexAnd(s string) int {
|
||||
for i, c := range s {
|
||||
|
||||
@@ -3,8 +3,8 @@ package rules
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
expect "github.com/yusing/godoxy/internal/utils/testing"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
expect "github.com/yusing/goutils/testing"
|
||||
)
|
||||
|
||||
func TestSplitAnd(t *testing.T) {
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/yusing/godoxy/internal/route"
|
||||
"github.com/yusing/godoxy/internal/route/routes"
|
||||
. "github.com/yusing/godoxy/internal/route/rules"
|
||||
expect "github.com/yusing/godoxy/internal/utils/testing"
|
||||
expect "github.com/yusing/goutils/testing"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"unicode"
|
||||
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
var escapedChars = map[rune]rune{
|
||||
@@ -57,7 +57,7 @@ func parse(v string) (subject string, args []string, err gperr.Error) {
|
||||
buf.WriteRune(ch)
|
||||
} else {
|
||||
err = ErrUnsupportedEscapeChar.Subjectf("\\%c", r)
|
||||
return
|
||||
return subject, args, err
|
||||
}
|
||||
escaped = false
|
||||
continue
|
||||
@@ -93,5 +93,5 @@ func parse(v string) (subject string, args []string, err gperr.Error) {
|
||||
} else {
|
||||
flush(false)
|
||||
}
|
||||
return
|
||||
return subject, args, err
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
. "github.com/yusing/godoxy/internal/utils/testing"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
expect "github.com/yusing/goutils/testing"
|
||||
)
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
@@ -68,15 +68,15 @@ func TestParser(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
subject, args, err := parse(tt.input)
|
||||
if tt.wantErr != nil {
|
||||
ExpectError(t, tt.wantErr, err)
|
||||
expect.ErrorIs(t, tt.wantErr, err)
|
||||
return
|
||||
}
|
||||
// t.Log(subject, args, err)
|
||||
ExpectNoError(t, err)
|
||||
ExpectEqual(t, subject, tt.subject)
|
||||
ExpectEqual(t, len(args), len(tt.args))
|
||||
expect.NoError(t, err)
|
||||
expect.Equal(t, subject, tt.subject)
|
||||
expect.Equal(t, len(args), len(tt.args))
|
||||
for i, arg := range args {
|
||||
ExpectEqual(t, arg, tt.args[i])
|
||||
expect.Equal(t, arg, tt.args[i])
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func TestParser(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
_, _, err := parse(test)
|
||||
ExpectError(t, ErrUnterminatedQuotes, err)
|
||||
expect.ErrorIs(t, ErrUnterminatedQuotes, err)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -130,5 +130,5 @@ func (rule *Rule) Check(cached Cache, r *http.Request) bool {
|
||||
|
||||
func (rule *Rule) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool) {
|
||||
proceed = rule.Do.exec.Handle(cached, w, r)
|
||||
return
|
||||
return proceed
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/yusing/godoxy/internal/serialization"
|
||||
. "github.com/yusing/godoxy/internal/utils/testing"
|
||||
expect "github.com/yusing/goutils/testing"
|
||||
)
|
||||
|
||||
func TestParseRule(t *testing.T) {
|
||||
@@ -29,18 +29,18 @@ func TestParseRule(t *testing.T) {
|
||||
Rules Rules
|
||||
}
|
||||
err := serialization.MapUnmarshalValidate(serialization.SerializedObject{"rules": test}, &rules)
|
||||
ExpectNoError(t, err)
|
||||
ExpectEqual(t, len(rules.Rules), len(test))
|
||||
ExpectEqual(t, rules.Rules[0].Name, "test")
|
||||
ExpectEqual(t, rules.Rules[0].On.String(), "method POST")
|
||||
ExpectEqual(t, rules.Rules[0].Do.String(), "error 403 Forbidden")
|
||||
expect.NoError(t, err)
|
||||
expect.Equal(t, len(rules.Rules), len(test))
|
||||
expect.Equal(t, rules.Rules[0].Name, "test")
|
||||
expect.Equal(t, rules.Rules[0].On.String(), "method POST")
|
||||
expect.Equal(t, rules.Rules[0].Do.String(), "error 403 Forbidden")
|
||||
|
||||
ExpectEqual(t, rules.Rules[1].Name, "auth")
|
||||
ExpectEqual(t, rules.Rules[1].On.String(), `basic_auth "username" "password" | basic_auth username2 "password2" | basic_auth "username3" "password3"`)
|
||||
ExpectEqual(t, rules.Rules[1].Do.String(), "bypass")
|
||||
expect.Equal(t, rules.Rules[1].Name, "auth")
|
||||
expect.Equal(t, rules.Rules[1].On.String(), `basic_auth "username" "password" | basic_auth username2 "password2" | basic_auth "username3" "password3"`)
|
||||
expect.Equal(t, rules.Rules[1].Do.String(), "bypass")
|
||||
|
||||
ExpectEqual(t, rules.Rules[2].Name, "default")
|
||||
ExpectEqual(t, rules.Rules[2].Do.String(), "require_basic_auth any_realm")
|
||||
expect.Equal(t, rules.Rules[2].Name, "default")
|
||||
expect.Equal(t, rules.Rules[2].Do.String(), "require_basic_auth any_realm")
|
||||
}
|
||||
|
||||
// TODO: real tests.
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
gphttp "github.com/yusing/godoxy/internal/net/gphttp"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Reference in New Issue
Block a user