1
+ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2
+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . Reflection ;
7
+ using Microsoft . AspNet . Mvc . Rendering ;
8
+ using Microsoft . AspNet . Razor . Runtime . TagHelpers ;
9
+ using Moq ;
10
+ using Xunit ;
11
+
12
+ namespace Microsoft . AspNet . Mvc . TagHelpers
13
+ {
14
+ public class AnchorTagHelperTest
15
+ {
16
+ [ Fact ]
17
+ public void Process_CallsIntoRouteLinkWithExpectedParameters ( )
18
+ {
19
+ // Arrange
20
+ var anchorTagHelper = new AnchorTagHelper
21
+ {
22
+ Route = "Default" ,
23
+ Protocol = "http" ,
24
+ Host = "contoso.com" ,
25
+ Fragment = "hello=world"
26
+ } ;
27
+ var called = false ;
28
+ var context = new TagHelperContext (
29
+ allAttributes : new Dictionary < string , object > ( ) ) ;
30
+ var output = new TagHelperOutput (
31
+ "a" ,
32
+ attributes : new Dictionary < string , string > ( ) ,
33
+ content : string . Empty ) ;
34
+
35
+ var generator = new Mock < IHtmlGenerator > ( ) ;
36
+ generator . Setup ( mock =>
37
+ mock . GenerateRouteLink ( It . IsAny < string > ( ) ,
38
+ It . IsAny < string > ( ) ,
39
+ It . IsAny < string > ( ) ,
40
+ It . IsAny < string > ( ) ,
41
+ It . IsAny < string > ( ) ,
42
+ It . IsAny < object > ( ) ,
43
+ It . IsAny < object > ( ) ) )
44
+ . Callback < string , string , string , string , string , object , object > (
45
+ ( linkText ,
46
+ routeName ,
47
+ protocol ,
48
+ hostname ,
49
+ fragment ,
50
+ routeValues ,
51
+ htmlAttributes ) =>
52
+ {
53
+ called = true ;
54
+
55
+ Assert . Empty ( linkText ) ;
56
+ Assert . Equal ( "Default" , routeName ) ;
57
+ Assert . Equal ( "http" , protocol ) ;
58
+ Assert . Equal ( "contoso.com" , hostname ) ;
59
+ Assert . Equal ( "hello=world" , fragment ) ;
60
+ Assert . Null ( routeValues ) ;
61
+ Assert . Null ( htmlAttributes ) ;
62
+ } )
63
+ . Returns ( new TagBuilder ( "a" ) ) ;
64
+
65
+ SetGenerator ( anchorTagHelper , generator . Object ) ;
66
+
67
+ // Act & Assert
68
+ anchorTagHelper . Process ( context , output ) ;
69
+
70
+ Assert . True ( called ) ;
71
+ }
72
+
73
+ [ Fact ]
74
+ public void Process_CallsIntoActionLinkWithExpectedParameters ( )
75
+ {
76
+ // Arrange
77
+ var anchorTagHelper = new AnchorTagHelper
78
+ {
79
+ Action = "Index" ,
80
+ Controller = "Home" ,
81
+ Protocol = "http" ,
82
+ Host = "contoso.com" ,
83
+ Fragment = "hello=world"
84
+ } ;
85
+ var called = false ;
86
+ var context = new TagHelperContext (
87
+ allAttributes : new Dictionary < string , object > ( ) ) ;
88
+ var output = new TagHelperOutput (
89
+ "a" ,
90
+ attributes : new Dictionary < string , string > ( ) ,
91
+ content : string . Empty ) ;
92
+
93
+ var generator = new Mock < IHtmlGenerator > ( ) ;
94
+ generator . Setup ( mock =>
95
+ mock . GenerateActionLink ( It . IsAny < string > ( ) ,
96
+ It . IsAny < string > ( ) ,
97
+ It . IsAny < string > ( ) ,
98
+ It . IsAny < string > ( ) ,
99
+ It . IsAny < string > ( ) ,
100
+ It . IsAny < string > ( ) ,
101
+ It . IsAny < object > ( ) ,
102
+ It . IsAny < object > ( ) ) )
103
+ . Callback < string , string , string , string , string , string , object , object > (
104
+ ( linkText ,
105
+ actionName ,
106
+ controllerName ,
107
+ protocol ,
108
+ hostname ,
109
+ fragment ,
110
+ routeValues ,
111
+ htmlAttributes ) =>
112
+ {
113
+ called = true ;
114
+
115
+ Assert . Empty ( linkText ) ;
116
+ Assert . Equal ( "Index" , actionName ) ;
117
+ Assert . Equal ( "Home" , controllerName ) ;
118
+ Assert . Equal ( "http" , protocol ) ;
119
+ Assert . Equal ( "contoso.com" , hostname ) ;
120
+ Assert . Equal ( "hello=world" , fragment ) ;
121
+ Assert . Null ( routeValues ) ;
122
+ Assert . Null ( htmlAttributes ) ;
123
+ } )
124
+ . Returns ( new TagBuilder ( "a" ) ) ;
125
+
126
+ SetGenerator ( anchorTagHelper , generator . Object ) ;
127
+
128
+ // Act & Assert
129
+ anchorTagHelper . Process ( context , output ) ;
130
+
131
+ Assert . True ( called ) ;
132
+ }
133
+
134
+ [ Fact ]
135
+ public void Process_RestoresBoundHtmlAttributesIfHrefProvided ( )
136
+ {
137
+ // Arrange
138
+ var anchorTagHelper = new AnchorTagHelper ( )
139
+ {
140
+ Protocol = "http" ,
141
+ Host = "contoso.com" ,
142
+ Fragment = "hello=world"
143
+ } ;
144
+ var context = new TagHelperContext (
145
+ allAttributes : new Dictionary < string , object > ( )
146
+ {
147
+ { "pRoToCol" , "http" } ,
148
+ { "hOsT" , "contoso.com" } ,
149
+ { "FrAgMeNt" , "hello=world" } ,
150
+ } ) ;
151
+ var output = new TagHelperOutput (
152
+ "a" ,
153
+ attributes : new Dictionary < string , string > ( )
154
+ {
155
+ { "href" , "http://www.contoso.com" }
156
+ } ,
157
+ content : string . Empty ) ;
158
+
159
+ // Act
160
+ anchorTagHelper . Process ( context , output ) ;
161
+
162
+ // Assert
163
+ var attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "pRoToCol" ) ) ;
164
+ Assert . Equal ( "http" , attribute . Value ) ;
165
+ attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "hOsT" ) ) ;
166
+ Assert . Equal ( "contoso.com" , attribute . Value ) ;
167
+ attribute = Assert . Single ( output . Attributes , kvp => kvp . Key . Equals ( "FrAgMeNt" ) ) ;
168
+ Assert . Equal ( "hello=world" , attribute . Value ) ;
169
+ }
170
+
171
+ public void Process_ThrowsIfHrefIsAmbiguous ( string propertyName )
172
+ {
173
+ // Arrange
174
+ var anchorTagHelper = new AnchorTagHelper ( ) ;
175
+ typeof ( AnchorTagHelper ) . GetProperty ( propertyName ) . SetValue ( anchorTagHelper , "Home" ) ;
176
+ var output = new TagHelperOutput (
177
+ "a" ,
178
+ attributes : new Dictionary < string , string > ( )
179
+ {
180
+ { "href" , "http://www.contoso.com" }
181
+ } ,
182
+ content : string . Empty ) ;
183
+ var expectedErrorMessage = "Cannot determine an href for AnchorTagHelper. A AnchorTagHelper with a " +
184
+ "specified href must not have a Action, Controller or Route attribute." ;
185
+
186
+ // Act & Assert
187
+ var ex = Assert . Throws < InvalidOperationException > ( ( ) =>
188
+ {
189
+ anchorTagHelper . Process ( context : null , output : output ) ;
190
+ } ) ;
191
+
192
+ Assert . Equal ( expectedErrorMessage , ex . Message ) ;
193
+ }
194
+
195
+ [ Theory ]
196
+ [ InlineData ( "Action" ) ]
197
+ [ InlineData ( "Controller" ) ]
198
+ public void Process_ThrowsIfRouteAndActionOrControllerProvided ( string propertyName )
199
+ {
200
+ // Arrange
201
+ var anchorTagHelper = new AnchorTagHelper
202
+ {
203
+ Route = "Default"
204
+ } ;
205
+ typeof ( AnchorTagHelper ) . GetProperty ( propertyName ) . SetValue ( anchorTagHelper , "Home" ) ;
206
+ var output = new TagHelperOutput (
207
+ "a" ,
208
+ attributes : new Dictionary < string , string > ( ) ,
209
+ content : string . Empty ) ;
210
+ var expectedErrorMessage = "Cannot determine an href for AnchorTagHelper. A AnchorTagHelper with a " +
211
+ "specified Route must not have a Action or Controller attribute." ;
212
+
213
+ // Act & Assert
214
+ var ex = Assert . Throws < InvalidOperationException > ( ( ) =>
215
+ {
216
+ anchorTagHelper . Process ( context : null , output : output ) ;
217
+ } ) ;
218
+
219
+ Assert . Equal ( expectedErrorMessage , ex . Message ) ;
220
+ }
221
+
222
+ private void SetGenerator ( ITagHelper tagHelper , IHtmlGenerator generator )
223
+ {
224
+ var tagHelperType = tagHelper . GetType ( ) ;
225
+
226
+ tagHelperType . GetProperty ( "Generator" , BindingFlags . NonPublic | BindingFlags . Instance )
227
+ . SetValue ( tagHelper , generator ) ;
228
+ }
229
+ }
230
+ }
0 commit comments