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