Add tests for target.Route

This commit is contained in:
Melon 2023-06-06 00:39:12 +01:00
parent 25c9a87068
commit d0149c87dc
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 223 additions and 90 deletions

View File

@ -1,6 +1,7 @@
package router package router
import ( import (
"github.com/MrMelon54/violet/proxy"
"github.com/MrMelon54/violet/target" "github.com/MrMelon54/violet/target"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -8,6 +9,12 @@ import (
"testing" "testing"
) )
type routeTestBase struct {
path string
dst target.Route
tests map[string]string
}
type redirectTestBase struct { type redirectTestBase struct {
path string path string
dst target.Redirect dst target.Redirect
@ -16,97 +23,201 @@ type redirectTestBase struct {
type mss map[string]string type mss map[string]string
var redirectTests = []redirectTestBase{ var (
{"/", target.Redirect{}, mss{ routeTests = []routeTestBase{
"/": "/", {"/", target.Route{}, mss{
"/hello": "", "/": "/",
}}, "/hello": "",
{"/", target.Redirect{Path: "world"}, mss{ }},
"/": "/world", {"/", target.Route{Path: "/world"}, mss{
"/hello": "", "/": "/world",
}}, "/hello": "",
{"/", target.Redirect{Abs: true}, mss{ }},
"/": "/", {"/", target.Route{Abs: true}, mss{
"/hello": "", "/": "/",
}}, "/hello": "",
{"/", target.Redirect{Abs: true, Path: "world"}, mss{ }},
"/": "/world", {"/", target.Route{Abs: true, Path: "world"}, mss{
"/hello": "", "/": "/world",
}}, "/hello": "",
{"/", target.Redirect{Pre: true}, mss{ }},
"/": "/", {"/", target.Route{Pre: true}, mss{
"/hello": "/hello", "/": "/",
}}, "/hello": "/hello",
{"/", target.Redirect{Pre: true, Path: "world"}, mss{ }},
"/": "/world", {"/", target.Route{Pre: true, Path: "world"}, mss{
"/hello": "/world/hello", "/": "/world",
}}, "/hello": "/world/hello",
{"/", target.Redirect{Pre: true, Abs: true}, mss{ }},
"/": "/", {"/", target.Route{Pre: true, Abs: true}, mss{
"/hello": "/", "/": "/",
}}, "/hello": "/",
{"/", target.Redirect{Pre: true, Abs: true, Path: "world"}, mss{ }},
"/": "/world", {"/", target.Route{Pre: true, Abs: true, Path: "world"}, mss{
"/hello": "/world", "/": "/world",
}}, "/hello": "/world",
{"/hello", target.Redirect{}, mss{ }},
"/": "", {"/hello", target.Route{}, mss{
"/hello": "/", "/": "",
"/hello/hi": "", "/hello": "/",
}}, "/hello/hi": "",
{"/hello", target.Redirect{Path: "world"}, mss{ }},
"/": "", {"/hello", target.Route{Path: "world"}, mss{
"/hello": "/world", "/": "",
"/hello/hi": "", "/hello": "/world",
}}, "/hello/hi": "",
{"/hello", target.Redirect{Abs: true}, mss{ }},
"/": "", {"/hello", target.Route{Abs: true}, mss{
"/hello": "/", "/": "",
"/hello/hi": "", "/hello": "/",
}}, "/hello/hi": "",
{"/hello", target.Redirect{Abs: true, Path: "world"}, mss{ }},
"/": "", {"/hello", target.Route{Abs: true, Path: "world"}, mss{
"/hello": "/world", "/": "",
"/hello/hi": "", "/hello": "/world",
}}, "/hello/hi": "",
{"/hello", target.Redirect{Pre: true}, mss{ }},
"/": "", {"/hello", target.Route{Pre: true}, mss{
"/hello": "/", "/": "",
"/hello/hi": "/hi", "/hello": "/",
}}, "/hello/hi": "/hi",
{"/hello", target.Redirect{Pre: true, Path: "world"}, mss{ }},
"/": "", {"/hello", target.Route{Pre: true, Path: "world"}, mss{
"/hello": "/world", "/": "",
"/hello/hi": "/world/hi", "/hello": "/world",
}}, "/hello/hi": "/world/hi",
{"/hello", target.Redirect{Pre: true, Abs: true}, mss{ }},
"/": "", {"/hello", target.Route{Pre: true, Abs: true}, mss{
"/hello": "/", "/": "",
"/hello/hi": "/", "/hello": "/",
}}, "/hello/hi": "/",
{"/hello", target.Redirect{Pre: true, Abs: true, Path: "world"}, mss{ }},
"/": "", {"/hello", target.Route{Pre: true, Abs: true, Path: "world"}, mss{
"/hello": "/world", "/": "",
"/hello/hi": "/world", "/hello": "/world",
}}, "/hello/hi": "/world",
}},
}
redirectTests = []redirectTestBase{
{"/", target.Redirect{}, mss{
"/": "/",
"/hello": "",
}},
{"/", target.Redirect{Path: "world"}, mss{
"/": "/world",
"/hello": "",
}},
{"/", target.Redirect{Abs: true}, mss{
"/": "/",
"/hello": "",
}},
{"/", target.Redirect{Abs: true, Path: "world"}, mss{
"/": "/world",
"/hello": "",
}},
{"/", target.Redirect{Pre: true}, mss{
"/": "/",
"/hello": "/hello",
}},
{"/", target.Redirect{Pre: true, Path: "world"}, mss{
"/": "/world",
"/hello": "/world/hello",
}},
{"/", target.Redirect{Pre: true, Abs: true}, mss{
"/": "/",
"/hello": "/",
}},
{"/", target.Redirect{Pre: true, Abs: true, Path: "world"}, mss{
"/": "/world",
"/hello": "/world",
}},
{"/hello", target.Redirect{}, mss{
"/": "",
"/hello": "/",
"/hello/hi": "",
}},
{"/hello", target.Redirect{Path: "world"}, mss{
"/": "",
"/hello": "/world",
"/hello/hi": "",
}},
{"/hello", target.Redirect{Abs: true}, mss{
"/": "",
"/hello": "/",
"/hello/hi": "",
}},
{"/hello", target.Redirect{Abs: true, Path: "world"}, mss{
"/": "",
"/hello": "/world",
"/hello/hi": "",
}},
{"/hello", target.Redirect{Pre: true}, mss{
"/": "",
"/hello": "/",
"/hello/hi": "/hi",
}},
{"/hello", target.Redirect{Pre: true, Path: "world"}, mss{
"/": "",
"/hello": "/world",
"/hello/hi": "/world/hi",
}},
{"/hello", target.Redirect{Pre: true, Abs: true}, mss{
"/": "",
"/hello": "/",
"/hello/hi": "/",
}},
{"/hello", target.Redirect{Pre: true, Abs: true, Path: "world"}, mss{
"/": "",
"/hello": "/world",
"/hello/hi": "/world",
}},
}
)
type fakeTransport struct{ req *http.Request }
func (f *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
f.req = req
rec := httptest.NewRecorder()
rec.WriteHeader(http.StatusOK)
return rec.Result(), nil
} }
func assertHttpRedirect(t *testing.T, r *Router, code int, target, method, start string) { func TestRouter_AddRoute(t *testing.T) {
res := httptest.NewRecorder() transSecure := &fakeTransport{}
req := httptest.NewRequest(method, start, nil) transInsecure := &fakeTransport{}
r.ServeHTTP(res, req)
l := res.Header().Get("Location") for _, i := range routeTests {
if target == "" { r := New(proxy.NewHybridTransportWithCalls(transSecure, transInsecure))
if code == res.Code || "" != l { dst := i.dst
t.Logf("Test URL: %#v\n", req.URL) dst.Host = "127.0.0.1"
t.Log(r.redirect["www.example.com"].String()) dst.Port = 8080
t.Fatalf("%s => %s\n", start, target) t.Logf("Running tests for %#v\n", dst)
} r.AddRoute("example.com", i.path, dst)
} else { for k, v := range i.tests {
if code != res.Code || target != l { u1 := &url.URL{Scheme: "https", Host: "example.com", Path: k}
t.Logf("Test URL: %#v\n", req.URL) req, _ := http.NewRequest(http.MethodGet, u1.String(), nil)
t.Log(r.redirect["www.example.com"].String()) rec := httptest.NewRecorder()
t.Fatalf("\nexpected %s => %s\n got %s => %s\n", start, target, start, l) r.ServeHTTP(rec, req)
if v == "" {
if transSecure.req != nil {
t.Logf("Test URL: %#v\n", req.URL)
t.Log(r.redirect["example.com"].String())
t.Fatalf("%s => %s\n", k, v)
}
} else {
if transSecure.req == nil {
t.Logf("Test URL: %#v\n", req.URL)
t.Log(r.route["example.com"].String())
t.Fatalf("\nexpected %s => %s\n got %s => %s\n", k, v, k, "")
}
if v != transSecure.req.URL.Path {
t.Logf("Test URL: %#v\n", req.URL)
t.Log(r.route["example.com"].String())
t.Fatalf("\nexpected %s => %s\n got %s => %s\n", k, v, k, transSecure.req.URL.Path)
}
transSecure.req = nil
}
} }
} }
} }
@ -130,6 +241,26 @@ func TestRouter_AddRedirect(t *testing.T) {
} }
} }
func assertHttpRedirect(t *testing.T, r *Router, code int, target, method, start string) {
res := httptest.NewRecorder()
req := httptest.NewRequest(method, start, nil)
r.ServeHTTP(res, req)
l := res.Header().Get("Location")
if target == "" {
if code == res.Code || "" != l {
t.Logf("Test URL: %#v\n", req.URL)
t.Log(r.redirect["www.example.com"].String())
t.Fatalf("%s => %s\n", start, target)
}
} else {
if code != res.Code || target != l {
t.Logf("Test URL: %#v\n", req.URL)
t.Log(r.redirect["www.example.com"].String())
t.Fatalf("\nexpected %s => %s\n got %s => %s\n", start, target, start, l)
}
}
}
func outputUrl(u *url.URL) string { func outputUrl(u *url.URL) string {
if u == nil { if u == nil {
return "" return ""

View File

@ -118,7 +118,9 @@ func (r Route) internalServeHTTP(rw http.ResponseWriter, req *http.Request) {
} }
// close the incoming body after use // close the incoming body after use
defer req.Body.Close() if req.Body != nil {
defer req.Body.Close()
}
// create the internal request // create the internal request
req2, err := http.NewRequest(req.Method, u.String(), req.Body) req2, err := http.NewRequest(req.Method, u.String(), req.Body)