diff --git a/config/type_bytes_test.go b/config/type_bytes_test.go index c6a8c40..6baec1c 100644 --- a/config/type_bytes_test.go +++ b/config/type_bytes_test.go @@ -87,7 +87,7 @@ func (suite *TypeBytesTestSuite) TestMarshalOk() { marshalled, err := testStruct.Value.MarshalText() assert.NoError(t, err) - assert.Equal(t, string(marshalled), name) + assert.Equal(t, name, string(marshalled)) }) } } diff --git a/config/type_duration_test.go b/config/type_duration_test.go index 004ad52..be9a8fe 100644 --- a/config/type_duration_test.go +++ b/config/type_duration_test.go @@ -85,7 +85,7 @@ func (suite *TypeDurationTestSuite) TestMarshalOk() { marshalled, err := testStruct.Value.MarshalText() assert.NoError(t, err) - assert.Equal(t, string(marshalled), name) + assert.Equal(t, name, string(marshalled)) }) } } diff --git a/config/type_http_path.go b/config/type_http_path.go index fea406f..56a7a4e 100644 --- a/config/type_http_path.go +++ b/config/type_http_path.go @@ -19,6 +19,10 @@ func (c TypeHTTPPath) MarshalText() ([]byte, error) { } func (c TypeHTTPPath) String() string { + if c.value == "" { + return "/" + } + return c.value } diff --git a/config/type_http_path_test.go b/config/type_http_path_test.go new file mode 100644 index 0000000..4c456ef --- /dev/null +++ b/config/type_http_path_test.go @@ -0,0 +1,91 @@ +package config_test + +import ( + "encoding/json" + "testing" + + "github.com/9seconds/mtg/v2/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type typeHTTPPathTestStruct struct { + Value config.TypeHTTPPath `json:"value"` +} + +type TypeHTTPPathTestSuite struct { + suite.Suite +} + +func (suite *TypeHTTPPathTestSuite) TestUnmarshal() { + testData := []string{ + "/hello", + "hello", + "hello/", + "/hello/", + } + + for _, v := range testData { + data, err := json.Marshal(map[string]string{ + "value": v, + }) + suite.NoError(err) + + suite.T().Run(v, func(t *testing.T) { + testStruct := &typeHTTPPathTestStruct{} + + assert.NoError(t, json.Unmarshal(data, testStruct)) + assert.Equal(t, "/hello", testStruct.Value.Value("")) + }) + } +} + +func (suite *TypeHTTPPathTestSuite) TestMarshalOk() { + testData := map[string]string{ + "": "/", + "/hello": "/hello", + "/hello/": "/hello", + "hello/": "/hello", + "hello": "/hello", + } + + for k, v := range testData { + toPass := k + compareWith := v + + data, err := json.Marshal(map[string]string{ + "value": toPass, + }) + suite.NoError(err) + + suite.T().Run(toPass, func(t *testing.T) { + testStruct := &typeHTTPPathTestStruct{} + + assert.NoError(t, json.Unmarshal(data, testStruct)) + assert.Equal(t, compareWith, testStruct.Value.String()) + + marshalled, err := testStruct.Value.MarshalText() + assert.NoError(t, err) + assert.Equal(t, compareWith, string(marshalled)) + }) + } +} + +func (suite *TypeHTTPPathTestSuite) TestValue() { + testStruct := &typeHTTPPathTestStruct{} + + suite.Equal("/hello", testStruct.Value.Value("/hello")) + + data, err := json.Marshal(map[string]string{ + "value": "/map", + }) + suite.NoError(err) + suite.NoError(json.Unmarshal(data, testStruct)) + + suite.Equal("/map", testStruct.Value.Value("/hello")) +} + +func TestTypeHTTPPath(t *testing.T) { + t.Parallel() + suite.Run(t, &TypeHTTPPathTestSuite{}) +}