File: filterconfig_test.go

package info (click to toggle)
golang-github-revel-revel 0.12.0+dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 828 kB
  • sloc: xml: 7; makefile: 4
file content (137 lines) | stat: -rw-r--r-- 3,400 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package revel

import "testing"

type FakeController struct{}

func (c FakeController) Foo()  {}
func (c *FakeController) Bar() {}

func TestFilterConfiguratorKey(t *testing.T) {
	conf := FilterController(FakeController{})
	if conf.key != "FakeController" {
		t.Errorf("Expected key 'FakeController', was %s", conf.key)
	}

	conf = FilterController(&FakeController{})
	if conf.key != "FakeController" {
		t.Errorf("Expected key 'FakeController', was %s", conf.key)
	}

	conf = FilterAction(FakeController.Foo)
	if conf.key != "FakeController.Foo" {
		t.Errorf("Expected key 'FakeController.Foo', was %s", conf.key)
	}

	conf = FilterAction((*FakeController).Bar)
	if conf.key != "FakeController.Bar" {
		t.Errorf("Expected key 'FakeController.Bar', was %s", conf.key)
	}
}

func TestFilterConfigurator(t *testing.T) {
	// Filters is global state.  Restore it after this test.
	oldFilters := make([]Filter, len(Filters))
	copy(oldFilters, Filters)
	defer func() {
		Filters = oldFilters
	}()

	Filters = []Filter{
		RouterFilter,
		FilterConfiguringFilter,
		SessionFilter,
		FlashFilter,
		ActionInvoker,
	}

	// Do one of each operation.
	conf := FilterAction(FakeController.Foo).
		Add(NilFilter).
		Remove(FlashFilter).
		Insert(ValidationFilter, BEFORE, NilFilter).
		Insert(I18nFilter, AFTER, NilFilter)
	expected := []Filter{
		SessionFilter,
		ValidationFilter,
		NilFilter,
		I18nFilter,
		ActionInvoker,
	}
	actual := getOverride("Foo")
	if len(actual) != len(expected) || !filterSliceEqual(actual, expected) {
		t.Errorf("Ops failed.\nActual: %#v\nExpect: %#v\nConf:%v", actual, expected, conf)
	}

	// Action2 should be unchanged
	if getOverride("Bar") != nil {
		t.Errorf("Filtering Action should not affect Action2.")
	}

	// Test that combining overrides on both the Controller and Action works.
	FilterController(FakeController{}).
		Add(PanicFilter)
	expected = []Filter{
		SessionFilter,
		ValidationFilter,
		NilFilter,
		I18nFilter,
		PanicFilter,
		ActionInvoker,
	}
	actual = getOverride("Foo")
	if len(actual) != len(expected) || !filterSliceEqual(actual, expected) {
		t.Errorf("Expected PanicFilter added to Foo.\nActual: %#v\nExpect: %#v", actual, expected)
	}

	expected = []Filter{
		SessionFilter,
		FlashFilter,
		PanicFilter,
		ActionInvoker,
	}
	actual = getOverride("Bar")
	if len(actual) != len(expected) || !filterSliceEqual(actual, expected) {
		t.Errorf("Expected PanicFilter added to Bar.\nActual: %#v\nExpect: %#v", actual, expected)
	}

	FilterAction((*FakeController).Bar).
		Add(NilFilter)
	expected = []Filter{
		SessionFilter,
		ValidationFilter,
		NilFilter,
		I18nFilter,
		PanicFilter,
		ActionInvoker,
	}
	actual = getOverride("Foo")
	if len(actual) != len(expected) || !filterSliceEqual(actual, expected) {
		t.Errorf("Expected no change to Foo.\nActual: %#v\nExpect: %#v", actual, expected)
	}

	expected = []Filter{
		SessionFilter,
		FlashFilter,
		PanicFilter,
		NilFilter,
		ActionInvoker,
	}
	actual = getOverride("Bar")
	if len(actual) != len(expected) || !filterSliceEqual(actual, expected) {
		t.Errorf("Expected NilFilter added to Bar.\nActual: %#v\nExpect: %#v", actual, expected)
	}
}

func filterSliceEqual(a, e []Filter) bool {
	for i, f := range a {
		if !FilterEq(f, e[i]) {
			return false
		}
	}
	return true
}

func getOverride(methodName string) []Filter {
	return getOverrideChain("FakeController", "FakeController."+methodName)
}