traefik/pkg/anonymize/anonymize_doOnJSON_test.go

66 lines
1.5 KiB
Go
Raw Normal View History

2017-10-02 08:32:02 +00:00
package anonymize
import (
2021-03-04 19:08:03 +00:00
"os"
2017-10-02 08:32:02 +00:00
"testing"
2017-11-08 10:40:04 +00:00
"github.com/stretchr/testify/assert"
2020-10-30 11:44:05 +00:00
"github.com/stretchr/testify/require"
2017-10-02 08:32:02 +00:00
)
func Test_doOnJSON(t *testing.T) {
2021-03-04 19:08:03 +00:00
baseConfiguration, err := os.ReadFile("./testdata/example.json")
2020-10-30 11:44:05 +00:00
require.NoError(t, err)
anomConfiguration := doOnJSON(string(baseConfiguration))
2021-03-04 19:08:03 +00:00
expectedConfiguration, err := os.ReadFile("./testdata/expected.json")
2020-10-30 11:44:05 +00:00
require.NoError(t, err)
2017-10-02 08:32:02 +00:00
2020-10-30 11:44:05 +00:00
assert.JSONEq(t, string(expectedConfiguration), anomConfiguration)
2017-10-02 08:32:02 +00:00
}
func Test_doOnJSON_simple(t *testing.T) {
testCases := []struct {
name string
input string
expectedOutput string
}{
{
name: "email",
input: `{
"email1": "goo@example.com",
"email2": "foo.bargoo@example.com",
"email3": "foo.bargoo@example.com.us"
}`,
expectedOutput: `{
"email1": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"email2": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"email3": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}`,
},
{
name: "url",
input: `{
"URL": "foo domain.com foo",
"URL": "foo sub.domain.com foo",
"URL": "foo sub.sub.domain.com foo",
"URL": "foo sub.sub.sub.domain.com.us foo"
}`,
expectedOutput: `{
"URL": "foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx foo",
"URL": "foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx foo",
"URL": "foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx foo",
"URL": "foo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx foo"
}`,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
output := doOnJSON(test.input)
assert.Equal(t, test.expectedOutput, output)
})
}
}