func TestNotValidFlashOption(t *testing.T) { handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { c := new(controller) c.Request = r c.Response = rw c.Title = "Welcome" assert.Panics(t, func() { flash.Set(c.Request, "T", "This option is not allowed.") }, "Should panic because wrong option.") c.serve() }) ts := createHttpServerTest(createMiddlewareHandler(session.New(), handler)) defer ts.Close() }
// Validate, if the user is authorized to use the handler. // Will use like middleware. func AllowVisit(handler http.Handler) http.Handler { n := negroni.New(negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { signed := IsSignedInUser(r) if !signed { flash.Set(r, "E", i18n.Translate(httphead.GetLang(r), i18nSec, "text12")) http.Redirect(rw, r, "/user/signin", 303) return } // If authorization is passed next(rw, r) })) n.UseHandler(handler) return n }
func TestFlashMessage(t *testing.T) { handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { c := new(controller) c.Request = r c.Response = rw c.Title = "Welcome" flash.Set(c.Request, "I", "This is the flash message") c.serve() }) ts := createHttpServerTest(createMiddlewareHandler(session.New(), handler)) defer ts.Close() client := &http.Client{} response, err := client.Get(ts.URL) assert.NoError(t, err, "Should not contain any error.") buffer := new(bytes.Buffer) io.Copy(buffer, response.Body) assert.Contains(t, buffer.String(), "This is the flash message", "Flash message.") }
// Set flash message func (rcv *Controller) SetFlash(option, text string) { flash.Set(rcv.Request, option, rcv.Translate(text)) }