From 7ca143d35255319de7da135ff178a61c445ad97a Mon Sep 17 00:00:00 2001 From: 9seconds Date: Fri, 12 Mar 2021 12:16:35 +0300 Subject: [PATCH] Add tests for config type url --- config/type_url.go | 26 +++++++++++ config/type_url_test.go | 97 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 config/type_url_test.go diff --git a/config/type_url.go b/config/type_url.go index 6452b2c..ec19012 100644 --- a/config/type_url.go +++ b/config/type_url.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "net" "net/url" ) @@ -19,6 +20,31 @@ func (c *TypeURL) UnmarshalText(data []byte) error { return fmt.Errorf("incorrect URL: %w", err) } + switch value.Scheme { + case "http", "https", "socks5": + case "": + return fmt.Errorf("url %s has to have a schema", value) + default: + return fmt.Errorf("unsupported schema %s", value.Scheme) + } + + if value.Host == "" { + return fmt.Errorf("url %s has to have a host", value) + } + + if _, _, err := net.SplitHostPort(value.Host); err != nil { + switch value.Scheme { + case "http": + value.Host = net.JoinHostPort(value.Host, "80") + case "https": + value.Host = net.JoinHostPort(value.Host, "443") + case "socks5": + value.Host = net.JoinHostPort(value.Host, "1080") + default: + return fmt.Errorf("cannot set a default port for %s", value) + } + } + c.value = value return nil diff --git a/config/type_url_test.go b/config/type_url_test.go new file mode 100644 index 0000000..0ab8e5a --- /dev/null +++ b/config/type_url_test.go @@ -0,0 +1,97 @@ +package config_test + +import ( + "encoding/json" + "net/url" + "testing" + + "github.com/9seconds/mtg/v2/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type typeURLTestStruct struct { + Value config.TypeURL `json:"value"` +} + +type TypeURLTestSuite struct { + suite.Suite +} + +func (suite *TypeURLTestSuite) TestUnmarshalFail() { + testData := []string{ + "http:/aaa.com", + "ipv4", + "111", + "://111", + "http://aaa.com:xxx", + "gopher://aaa.com:888", + } + + for _, v := range testData { + data, err := json.Marshal(map[string]string{ + "value": v, + }) + suite.NoError(err) + + suite.T().Run(v, func(t *testing.T) { + assert.Error(t, json.Unmarshal(data, &typeURLTestStruct{})) + }) + } +} + +func (suite *TypeURLTestSuite) TestUnmarshalOk() { + testData := map[string]string{ + "https://10.0.0.10:80": "https://10.0.0.10:80", + "https://10.0.0.10:443": "https://10.0.0.10", + "http://10.0.0.10:8": "http://10.0.0.10:8", + "http://10.0.0.10:80": "http://10.0.0.10", + "socks5://10.0.0.10:1080": "socks5://10.0.0.10", + "socks5://10.0.0.10:888": "socks5://10.0.0.10:888", + } + + for k, v := range testData { + expected := k + actual := v + + data, err := json.Marshal(map[string]string{ + "value": actual, + }) + suite.NoError(err) + + suite.T().Run(actual, func(t *testing.T) { + testStruct := &typeURLTestStruct{} + + assert.NoError(t, json.Unmarshal(data, testStruct)) + assert.Equal(t, expected, testStruct.Value.Value(nil).String()) + + marshalled, err := testStruct.Value.MarshalText() + assert.NoError(t, err) + assert.Equal(t, expected, string(marshalled)) + }) + } +} + +func (suite *TypeURLTestSuite) TestValue() { + testStruct := &typeURLTestStruct{} + + u1, _ := url.Parse("https://10.0.0.10:80") + u2, _ := url.Parse("https://10.1.0.10:80") + + suite.Equal("https://10.0.0.10:80", testStruct.Value.Value(u1).String()) + suite.Equal("https://10.1.0.10:80", testStruct.Value.Value(u2).String()) + + data, err := json.Marshal(map[string]string{ + "value": "http://127.0.0.1:80", + }) + suite.NoError(err) + suite.NoError(json.Unmarshal(data, testStruct)) + + suite.Equal("http://127.0.0.1:80", testStruct.Value.Value(u1).String()) + suite.Equal("http://127.0.0.1:80", testStruct.Value.Value(u2).String()) +} + +func TestTypeURL(t *testing.T) { + t.Parallel() + suite.Run(t, &TypeURLTestSuite{}) +}