-
Notifications
You must be signed in to change notification settings - Fork 7
/
RequestEngineTest.cfc
160 lines (132 loc) · 6 KB
/
RequestEngineTest.cfc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @doc_abstract true
*/
component extends='testbox.system.BaseSpec' {
function isAbstractSpec() {
var md = getMetadata(this);
return structKeyExists( md, 'doc_abstract' ) && md.doc_abstract == true;
}
function getCUT() {
throw('Method is abstract and must be implemented in a concrete test component. Return the component from this method.');
}
function beforeAll() {
if (isAbstractSpec()) {
return;
}
this.CUT = getCUT();
getMockBox().prepareMock( this.CUT );
this.CUT.$property( propertyName = "appMapping", mock = "/SampleApp" );
this.CUT.beforeAll();
this.CUT.$("getRequestContext", "event-object");
}
function run() {
if (isAbstractSpec()) {
debug( "This spec is not run directly. To run these tests, extend this class from a concrete component." );
return;
}
describe('Request Engine — #getMetadata(this).fullname#', function() {
it('visits a url', function() {
this.CUT.$( "execute" )
.$args( route = "/exists", renderResults = true )
.$results( "event-object" );
expect(function() {
var event = this.CUT.makeRequest(
method = "GET",
route = "/exists"
);
}).notToThrow();
var callLog = this.CUT.$callLog().execute;
expect( callLog ).toHaveLength( 1 );
expect( callLog[1]["route"] ).toBe( "/exists" );
expect( callLog[1]["renderResults"] ).toBe( true );
});
it('fails when the url cannot be found', function() {
this.CUT.$( "execute" )
.$args( route = "/does-not-exist", renderResults = true )
.$throws(
type = "TestBox.AssertionFailed",
message = "Could not find any route called [/does-not-exist]."
);
expect(function() {
this.CUT.makeRequest(
method = "GET",
route = "/does-not-exist"
);
}).toThrow(
type = 'TestBox.AssertionFailed',
regex = 'Could not find any route called \[\/does-not-exist\]\.'
);
var callLog = this.CUT.$callLog().execute;
expect( callLog ).toHaveLength( 1 );
expect( callLog[1]["route"] ).toBe( "/does-not-exist" );
expect( callLog[1]["renderResults"] ).toBe( true );
});
it('visits a framework event', function() {
this.CUT.$( "execute" )
.$args( event = "Main.index", renderResults = true )
.$results( "event-object" );
expect(function() {
this.CUT.makeRequest(
method = "GET",
event = "Main.index"
);
}).notToThrow();
var callLog = this.CUT.$callLog().execute;
expect( callLog ).toHaveLength( 1 );
expect( callLog[1]["event"] ).toBe( "Main.index" );
expect( callLog[1]["renderResults"] ).toBe( true );
});
it('fails when the event cannot be found', function() {
this.CUT.$( "execute" )
.$args( event = "Main.doesntExist", renderResults = true )
.$throws(
type = "TestBox.AssertionFailed",
message = "Could not find any event called [Main.doesntExist]."
);
expect(function() {
this.CUT.makeRequest(
method = "GET",
event = "Main.doesntExist"
);
}).toThrow(
type = 'TestBox.AssertionFailed',
regex = 'Could not find any event called \[Main\.doesntExist\]\.'
);
var callLog = this.CUT.$callLog().execute;
expect( callLog ).toHaveLength( 1 );
expect( callLog[1]["event"] ).toBe( "Main.doesntExist" );
expect( callLog[1]["renderResults"] ).toBe( true );
});
it( "passes along any parameters to the request", function() {
var mockEvent = getMockBox().createMock("coldbox.system.web.context.RequestContext");
getMockBox().prepareMock(mockEvent);
mockEvent.$("setValue");
this.CUT.$( "execute" )
.$args( route = "/exists", renderResults = true )
.$results( "event-object" );
this.CUT.$( "getRequestContext", mockEvent );
this.CUT.makeRequest(
method = "GET",
route = "/exists",
parameters = {
email = "[email protected]",
password = "my_awesome_password"
}
);
var callLog = mockEvent.$callLog().setValue;
expect( callLog ).toHaveLength( 2, "Two parameters were passed so setValue should have been called twice." );
if ( callLog[1][1] == "PASSWORD" ) {
expect( callLog[1][1] ).toBe( "PASSWORD" );
expect( callLog[1][2] ).toBe( "my_awesome_password" );
expect( callLog[2][1] ).toBe( "EMAIL" );
expect( callLog[2][2] ).toBe( "[email protected]" );
} else {
expect( callLog[1][1] ).toBe( "EMAIL" );
expect( callLog[1][2] ).toBe( "[email protected]" );
expect( callLog[2][1] ).toBe( "PASSWORD" );
expect( callLog[2][2] ).toBe( "my_awesome_password" );
}
});
});
}
}