package mapatest
import (
"bytes"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// Where the reader's locale comes from (level-up build note 85, review
// round 8): their own choice in Settings, then their browser's
// Accept-Language, then the team's. A /de/ path prefix and rastrillo's
// locale cookie are not read.
// requestIn is a request from Ana's browser asking for language, with
// any cookies.
func (rig *rig) requestIn(method, target, language string, form url.Values,
cookies ...*http.Cookie) *httptest.ResponseRecorder {
var body *strings.Reader
if form != nil {
body = strings.NewReader(form.Encode())
} else {
body = strings.NewReader("")
}
request := httptest.NewRequest(method, target, body)
if form != nil {
request.Header.Set("Content-Type",
"application/x-www-form-urlencoded")
request.Header.Set("Origin", testOrigin)
}
request.Header.Set("Accept-Language", language)
for _, cookie := range cookies {
request.AddCookie(cookie)
}
recorder := httptest.NewRecorder()
rig.built.As(Ana).ServeHTTP(recorder, request)
return recorder
}
func TestNoPathPrefixOrCookieChoosesTheLocale(t *testing.T) {
rig := newRig(t)
if got := rig.requestIn(http.MethodGet, "/de/d/2026-11-17", "en-IE",
nil); got.Code != http.StatusNotFound {
t.Errorf("/de/d/2026-11-17 answered %d, not 404", got.Code)
}
cookie := &http.Cookie{Name: "rastrillo_locale", Value: "de"}
day := rig.requestIn(http.MethodGet, "/d/2026-11-17", "en-US", nil,
cookie).Body.String()
if !strings.Contains(day, "Tuesday, November 17<") ||
strings.Contains(day, "Dienstag") {
t.Error("rastrillo's locale cookie outranked Accept-Language")
}
}
func TestThePersonsOwnFormatOutranksTheBrowser(t *testing.T) {
rig := newRig(t)
settings := rig.requestIn(http.MethodGet, "/settings", "en-US", nil)
wantIn(t, "Settings", "en-US", settings.Body.String(),
``,
`