Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / validation_test.go
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.
4
5 package revel
6
7 import (
8         "net/http"
9         "net/http/httptest"
10         "testing"
11 )
12
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 {
19                         return cookie, nil
20                 }
21         }
22         return nil, http.ErrNoCookie
23 }
24
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))
29         c.Request = req
30
31         ValidationFilter(c, []Filter{I18nFilter, func(c *Controller, _ []Filter) {
32                 fn(c)
33         }})
34         return recorder
35 }
36
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")
43                 }
44                 c.Validation.Keep()
45         })
46
47         if cookie, err := getRecordedCookie(recorder, "REVEL_ERRORS"); err != nil {
48                 t.Fatal(err)
49         } else if cookie.MaxAge < 0 {
50                 t.Fatalf("cookie should not expire")
51         }
52 }
53
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")
60                 }
61         })
62
63         if _, err := getRecordedCookie(recorder, "REVEL_ERRORS"); err != http.ErrNoCookie {
64                 t.Fatal(err)
65         }
66 }
67
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")
75                 }
76         })
77
78         if cookie, err := getRecordedCookie(recorder, "REVEL_ERRORS"); err != nil {
79                 t.Fatal(err)
80         } else if cookie.MaxAge >= 0 {
81                 t.Fatalf("cookie should be deleted")
82         }
83 }
84
85 func TestValidateMessageKey(t *testing.T) {
86         Init("prod", "github.com/revel/revel/testdata", "")
87         loadMessages(testDataPath)
88
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()))
92         }
93         req := buildRequestWithAcceptLanguages("nl").Request
94
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)
99                 }
100
101                 if !c.Validation.HasErrors() {
102                         t.Fatal("errors should not be present")
103                 }
104         })
105
106 }