From 059b9b9fcea247b927b45a5057e5ca2cf05d20bb Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Mon, 25 Mar 2024 13:03:34 -0400 Subject: [PATCH] test: translateUrl test cases --- src/routes/home_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/routes/home_test.go diff --git a/src/routes/home_test.go b/src/routes/home_test.go new file mode 100644 index 0000000..eda6d64 --- /dev/null +++ b/src/routes/home_test.go @@ -0,0 +1,32 @@ +package routes + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTranslateUrl(t *testing.T) { + assert := assert.New(t) + + // Test with a Valid StackOverflow URL + assert.Equal("/questions/example-question", translateUrl("https://stackoverflow.com/questions/example-question"), "StackOverflow URL should not be modified") + + // Test with Complex Subdomain + assert.Equal("/exchange/meta.math.stackexchange.com/q/example-question", translateUrl("https://meta.math.stackexchange.com/q/example-question"), "Complex StackExchange subdomain should be used as full exchange") + + // Test with Non-StackExchange Domain + assert.Equal("/exchange/example.com/questions/example-question", translateUrl("https://example.com/questions/example-question"), "Non-StackExchange domain should be detected as exchange") + + // Test with Invalid URL + assert.Equal("", translateUrl("This is not a URL"), "Invalid URL should return an empty string") + + // Test with Empty String + assert.Equal("", translateUrl(""), "Empty string should return an empty string") + + // Test with Missing Path + assert.Equal("", translateUrl("https://stackoverflow.com"), "URL without path should return an empty string") + + // Test with Valid URL but Root Domain for StackExchange + assert.Equal("", translateUrl("https://stackexchange.com"), "Root StackExchange domain without subdomain should return an empty string") +}