From 8b7b2fce7f62a0b65379581f929586ba8818a736 Mon Sep 17 00:00:00 2001 From: gaowenju Date: Wed, 8 Nov 2023 18:56:45 +0800 Subject: [PATCH] test: add ut --- pkg/protocol/uri_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pkg/protocol/uri_test.go b/pkg/protocol/uri_test.go index e245faf26..2e8109dc7 100644 --- a/pkg/protocol/uri_test.go +++ b/pkg/protocol/uri_test.go @@ -42,6 +42,7 @@ package protocol import ( + "bytes" "path/filepath" "reflect" "runtime" @@ -468,3 +469,39 @@ func TestParseURI(t *testing.T) { uri := string(ParseURI(expectURI).FullURI()) assert.DeepEqual(t, expectURI, uri) } + +func TestSplitHostURI(t *testing.T) { + cases := []struct { + host, uri []byte + wantScheme, wantHost, wantPath []byte + }{ + { + []byte("example.com"), []byte("/foobar"), + []byte("http"), []byte("example.com"), []byte("/foobar"), + }, + { + []byte("example2.com"), []byte("http://example2.com"), + []byte("http"), []byte("example2.com"), []byte("/"), + }, + { + []byte("example2.com"), []byte("http://example3.com"), + []byte("http"), []byte("example3.com"), []byte("/"), + }, + { + []byte("example3.com"), []byte("https://foobar.com?a=b"), + []byte("https"), []byte("foobar.com"), []byte("?a=b"), + }, + { + []byte("example.com"), []byte("//www.google.com/://../../ping"), + []byte("http"), []byte("example.com"), []byte("//www.google.com/://../../ping"), + }, + } + + for _, c := range cases { + gotScheme, gotHost, gotPath := splitHostURI(c.host, c.uri) + if !bytes.Equal(gotScheme, c.wantScheme) || !bytes.Equal(gotHost, c.wantHost) || !bytes.Equal(gotPath, c.wantPath) { + t.Errorf("splitHostURI(%q, %q) == (%q, %q, %q), want (%q, %q, %q)", + c.host, c.uri, gotScheme, gotHost, gotPath, c.wantScheme, c.wantHost, c.wantPath) + } + } +}