1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
13 // getRecordedCookie returns the recorded cookie from a ResponseRecorder with
14 // the given name. It utilizes the cookie reader found in the standard library.
15 func getRecordedCookie(recorder *httptest.ResponseRecorder, name string) (*http.Cookie, error) {
16 r := &http.Response{Header: recorder.HeaderMap}
17 for _, cookie := range r.Cookies() {
18 if cookie.Name == name {
22 return nil, http.ErrNoCookie
25 // r.Original.URL.String()
26 func validationTester(req *Request, fn func(c *Controller)) *httptest.ResponseRecorder {
27 recorder := httptest.NewRecorder()
28 c := NewTestController(recorder, req.In.GetRaw().(*http.Request))
31 ValidationFilter(c, []Filter{I18nFilter, func(c *Controller, _ []Filter) {
37 // Test that errors are encoded into the _ERRORS cookie.
38 func TestValidationWithError(t *testing.T) {
39 recorder := validationTester(buildEmptyRequest().Request, func(c *Controller) {
40 c.Validation.Required("")
41 if !c.Validation.HasErrors() {
42 t.Fatal("errors should be present")
47 if cookie, err := getRecordedCookie(recorder, "REVEL_ERRORS"); err != nil {
49 } else if cookie.MaxAge < 0 {
50 t.Fatalf("cookie should not expire")
54 // Test that no cookie is sent if errors are found, but Keep() is not called.
55 func TestValidationNoKeep(t *testing.T) {
56 recorder := validationTester(buildEmptyRequest().Request, func(c *Controller) {
57 c.Validation.Required("")
58 if !c.Validation.HasErrors() {
59 t.Fatal("errors should not be present")
63 if _, err := getRecordedCookie(recorder, "REVEL_ERRORS"); err != http.ErrNoCookie {
68 // Test that a previously set _ERRORS cookie is deleted if no errors are found.
69 func TestValidationNoKeepCookiePreviouslySet(t *testing.T) {
70 req := buildRequestWithCookie("REVEL_ERRORS", "invalid").Request
71 recorder := validationTester(req, func(c *Controller) {
72 c.Validation.Required("success")
73 if c.Validation.HasErrors() {
74 t.Fatal("errors should not be present")
78 if cookie, err := getRecordedCookie(recorder, "REVEL_ERRORS"); err != nil {
80 } else if cookie.MaxAge >= 0 {
81 t.Fatalf("cookie should be deleted")
85 func TestValidateMessageKey(t *testing.T) {
86 Init("prod", "github.com/revel/revel/testdata", "")
87 loadMessages(testDataPath)
89 // Assert that we have the expected number of languages
90 if len(MessageLanguages()) != 2 {
91 t.Fatalf("Expected messages to contain no more or less than 2 languages, instead there are %d languages", len(MessageLanguages()))
93 req := buildRequestWithAcceptLanguages("nl").Request
95 validationTester(req, func(c *Controller) {
96 c.Validation.Required("").MessageKey("greeting")
97 if msg := c.Validation.Errors[0].Message; msg != "Hallo" {
98 t.Errorf("Failed expected message Hallo got %s", msg)
101 if !c.Validation.HasErrors() {
102 t.Fatal("errors should not be present")