81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func serve(req *http.Request) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
newHandler().ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
|
|
// Scenario: Проверка здоровья
|
|
func TestHealthzOK(t *testing.T) {
|
|
rec := serve(httptest.NewRequest(http.MethodGet, "/healthz", nil))
|
|
if rec.Code != 200 || rec.Body.String() != "ok" {
|
|
t.Fatalf("got %d %q", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// Scenario: Неверный метод
|
|
func TestHealthzWrongMethod405(t *testing.T) {
|
|
rec := serve(httptest.NewRequest(http.MethodPost, "/healthz", nil))
|
|
if rec.Code != 405 {
|
|
t.Fatalf("got %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
// Scenario: Открытие страницы
|
|
func TestProductPageOpens(t *testing.T) {
|
|
rec := serve(httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if rec.Code != 200 || !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/html") {
|
|
t.Fatalf("got %d %q", rec.Code, rec.Header().Get("Content-Type"))
|
|
}
|
|
for _, h := range []string{"Что это", "Как пользоваться", "Как проверить"} {
|
|
if !strings.Contains(rec.Body.String(), h) {
|
|
t.Errorf("no %q on page", h)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Scenario: Неизвестная страница
|
|
func TestProductPageUnknown404(t *testing.T) {
|
|
if rec := serve(httptest.NewRequest(http.MethodGet, "/nope", nil)); rec.Code != 404 {
|
|
t.Fatalf("got %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
// Scenario Outline: Выбор языка
|
|
func TestProductPageLanguage(t *testing.T) {
|
|
cases := []struct{ lang, header, title string }{
|
|
{"kk", "", "Бұл не"},
|
|
{"en", "ru", "What it is"},
|
|
{"", "en-US,en;q=0.9", "What it is"},
|
|
{"", "", "Что это"},
|
|
{"de", "", "Что это"},
|
|
}
|
|
for _, c := range cases {
|
|
req := httptest.NewRequest(http.MethodGet, "/?lang="+c.lang, nil)
|
|
if c.header != "" {
|
|
req.Header.Set("Accept-Language", c.header)
|
|
}
|
|
if body := serve(req).Body.String(); !strings.Contains(body, c.title) {
|
|
t.Errorf("lang=%q header=%q: no %q", c.lang, c.header, c.title)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTranslationsHaveSameKeys(t *testing.T) {
|
|
for k := range translations[defaultLang] {
|
|
for _, l := range supportedLangs {
|
|
if translations[l][k] == "" {
|
|
t.Errorf("%s: missing %q", l, k)
|
|
}
|
|
}
|
|
}
|
|
}
|