From 6756e7f7ccaa442f0ea04897874fe7fb9bc6b7de Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sat, 24 Feb 2024 12:27:19 +0100 Subject: [PATCH] Add test for groupBase. --- webserver/webserver_test.go | 59 ++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/webserver/webserver_test.go b/webserver/webserver_test.go index 4b223b9..750c5dd 100644 --- a/webserver/webserver_test.go +++ b/webserver/webserver_test.go @@ -1,8 +1,14 @@ package webserver import ( + "crypto/tls" + "encoding/json" + "net/http" + "os" + "path/filepath" "testing" + "github.com/jech/galene/group" "github.com/pion/webrtc/v3" ) @@ -30,6 +36,57 @@ func TestParseGroupName(t *testing.T) { } } +func TestBase(t *testing.T) { + a := []struct { + p string + t bool + h, res string + }{ + {"", true, "a.org", "https://a.org/group/"}, + {"", false, "a.org", "http://a.org/group/"}, + {"https://b.org", true, "a.org", "https://b.org/group/"}, + {"https://b.org", false, "a.org", "https://b.org/group/"}, + {"http://b.org", true, "a.org", "http://b.org/group/"}, + {"http://b.org", false, "a.org", "http://b.org/group/"}, + } + + dir := t.TempDir() + group.DataDirectory = dir + + for _, v := range a { + conf := group.Configuration{ + ProxyURL: v.p, + } + c, err := json.Marshal(conf) + if err != nil { + t.Errorf("Marshal: %v", err) + continue + } + err = os.WriteFile( + filepath.Join(dir, "config.json"), + c, + 0600, + ) + if err != nil { + t.Errorf("Write: %v", err) + continue + } + var tcs *tls.ConnectionState + if v.t { + tcs = &tls.ConnectionState{} + } + base, err := groupBase(&http.Request{ + TLS: tcs, + Host: v.h, + }) + if err != nil || base != v.res { + t.Errorf("Expected %v, got %v (%v)", + v.res, base, err, + ) + } + } +} + func TestParseSplit(t *testing.T) { a := []struct{ p, a, b, c string }{ {"", "", "", ""}, @@ -43,7 +100,7 @@ func TestParseSplit(t *testing.T) { {"/a/.b/c/d./", "/a", ".b", "/c/d./"}, } - for _, pabc := range(a) { + for _, pabc := range a { a, b, c := splitPath(pabc.p) if pabc.a != a || pabc.b != b || pabc.c != c { t.Errorf("Path %v, got %v, %v, %v, expected %v, %v, %v",