utils_test.go 860 B

123456789101112131415161718192021222324252627282930
  1. package sqlx
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestEscape(t *testing.T) {
  8. s := "a\x00\n\r\\'\"\x1ab"
  9. out := escape(s)
  10. assert.Equal(t, `a\x00\n\r\\\'\"\x1ab`, out)
  11. }
  12. func TestDesensitize(t *testing.T) {
  13. datasource := "user:pass@tcp(111.222.333.44:3306)/any_table?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
  14. datasource = desensitize(datasource)
  15. assert.False(t, strings.Contains(datasource, "user"))
  16. assert.False(t, strings.Contains(datasource, "pass"))
  17. assert.True(t, strings.Contains(datasource, "tcp(111.222.333.44:3306)"))
  18. }
  19. func TestDesensitize_WithoutAccount(t *testing.T) {
  20. datasource := "tcp(111.222.333.44:3306)/any_table?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai"
  21. datasource = desensitize(datasource)
  22. assert.True(t, strings.Contains(datasource, "tcp(111.222.333.44:3306)"))
  23. }