package towel_test import ( "fmt" "net/http" "net/http/httptest" "git.golem15.com/golem15/summercms/modules/towel" ) func ExampleWithLocale() { // A middleware stores the organization once for the whole request. withAcme := func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := towel.WithOrganization(r.Context(), "acme") next.ServeHTTP(w, r.WithContext(ctx)) }) } // The handler reads the values back from its request context. listPosts := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { org, _ := towel.Organization(r.Context()) locale, ok := towel.Locale(r.Context()) if !ok { locale = "en" } fmt.Fprintf(w, "posts for %s in %s", org, locale) }) // surf sets the locale from Accept-Language; here the test sets it. req := httptest.NewRequest(http.MethodGet, "/api/blog/posts", nil) req = req.WithContext(towel.WithLocale(req.Context(), "pl")) rec := httptest.NewRecorder() withAcme(listPosts).ServeHTTP(rec, req) fmt.Println(rec.Body.String()) // Output: posts for acme in pl }