From bb59aadcd3a0a0de24d9757ce30a2c55cccbe60c Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Fri, 14 Oct 2022 22:58:26 +0300 Subject: [PATCH] Added tests for Make replacer method --- internal/urlreplacer/factory_test.go | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/internal/urlreplacer/factory_test.go b/internal/urlreplacer/factory_test.go index ff26bf80..29c9d55d 100644 --- a/internal/urlreplacer/factory_test.go +++ b/internal/urlreplacer/factory_test.go @@ -4,6 +4,8 @@ import ( "testing" "github.com/evg4b/uncors/internal/urlreplacer" + "github.com/evg4b/uncors/pkg/urlx" + "github.com/evg4b/uncors/testing/testutils" "github.com/stretchr/testify/assert" ) @@ -49,3 +51,48 @@ func TestNewUrlReplacerFactory(t *testing.T) { assert.NoError(t, err) }) } + +func TestFactoryMake(t *testing.T) { + factory, err := urlreplacer.NewURLReplacerFactory(map[string]string{ + "http://server1.com": "https://mappedserver1.com", + "https://server2.com": "https://mappedserver2.com", + }) + testutils.CheckNoError(t, err) + + tests := []struct { + name string + url string + err string + }{ + { + name: "mapped http server", + url: "http://server1.com", + }, + { + name: "mapped https server", + url: "https://server2.com", + }, + { + name: "unknown server", + url: "https://server3.com", + err: "mapping not found", + }, + } + for _, testCase := range tests { + t.Run(testCase.name, func(t *testing.T) { + parseURL, err := urlx.Parse(testCase.url) + testutils.CheckNoError(t, err) + + got, got1, err := factory.Make(parseURL) + if len(testCase.err) > 0 { + assert.Nil(t, got1) + assert.Nil(t, got) + assert.EqualError(t, err, testCase.err) + } else { + assert.NotNil(t, got1) + assert.NotNil(t, got) + assert.NoError(t, err) + } + }) + } +}