count as rune

This commit is contained in:
Umputun
2018-05-12 12:02:49 -05:00
parent 75a2cac999
commit 70c910b21a
3 changed files with 5 additions and 5 deletions
+2 -2
View File
@@ -62,7 +62,7 @@ func TestServer_CreateTooBig(t *testing.T) {
require.NotNil(t, srv)
defer cleanup(srv)
longComment := fmt.Sprintf(`{"text": "%4001s", "locator":{"url": "https://radio-t.com/blah1", "site": "radio-t"}}`, "X")
longComment := fmt.Sprintf(`{"text": "%4001s", "locator":{"url": "https://radio-t.com/blah1", "site": "radio-t"}}`, "Щ")
r := strings.NewReader(longComment)
resp, err := http.Post(fmt.Sprintf("http://dev:password@127.0.0.1:%d/api/v1/comment", port), "application/json", r)
assert.Nil(t, err)
@@ -73,7 +73,7 @@ func TestServer_CreateTooBig(t *testing.T) {
err = json.Unmarshal(b, &c)
assert.Nil(t, err)
assert.Equal(t, "comment text exceeded max allowed size", c["error"])
assert.Equal(t, "comment text exceeded max allowed size 4000 (4001)", c["error"])
assert.Equal(t, "invalid comment", c["details"])
}
+2 -2
View File
@@ -133,8 +133,8 @@ func (s *Service) ValidateComment(c *Comment) error {
if c.Text == "" {
return errors.New("empty comment text")
}
if len(c.Text) > maxSize {
return errors.New("comment text exceeded max allowed size")
if len([]rune(c.Text)) > maxSize {
return errors.Errorf("comment text exceeded max allowed size %d (%d)", maxSize, len([]rune(c.Text)))
}
if c.User.ID == "" || c.User.Name == "" {
return errors.Errorf("empty user info")
+1 -1
View File
@@ -195,7 +195,7 @@ func TestService_ValidateComment(t *testing.T) {
{inp: Comment{}, err: errors.New("empty comment text")},
{inp: Comment{Text: "something blah", User: User{ID: "myid", Name: "name"}}, err: nil},
{inp: Comment{Text: "something blah", User: User{ID: "myid"}}, err: errors.New("empty user info")},
{inp: Comment{Text: longText, User: User{ID: "myid", Name: "name"}}, err: errors.New("comment text exceeded max allowed size")},
{inp: Comment{Text: longText, User: User{ID: "myid", Name: "name"}}, err: errors.New("comment text exceeded max allowed size 2000 (4000)")},
}
for n, tt := range tbl {