33 lines
1.0 KiB
Go
33 lines
1.0 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestCreateLeadRejectsBrokenEncoding(t *testing.T) {
|
|
a := New(nil, "")
|
|
// имя в cp1251 внутри JSON: декодер превращает такие байты в U+FFFD
|
|
body := "{\"name\":\"\xc8\xe2\xe0\xed\",\"phone\":\"+7 928 962-00-94\",\"consent\":true}"
|
|
w := httptest.NewRecorder()
|
|
a.createLead(w, httptest.NewRequest(http.MethodPost, "/api/v1/leads", strings.NewReader(body)))
|
|
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "bad_encoding") {
|
|
t.Fatalf("got %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCleanUTM(t *testing.T) {
|
|
got := cleanUTM(map[string]string{
|
|
"utm_source": " yandex ", "referrer": "vk.com", "evil": "x", "utm_term": "",
|
|
"utm_campaign": strings.Repeat("я", 300),
|
|
})
|
|
if got["utm_source"] != "yandex" || got["referrer"] != "vk.com" || len(got) != 3 {
|
|
t.Fatalf("cleanUTM = %v", got)
|
|
}
|
|
if n := len([]rune(got["utm_campaign"])); n != 200 {
|
|
t.Fatalf("utm_campaign not truncated: %d", n)
|
|
}
|
|
}
|