1
1
// Copyright (c) Duende Software. All rights reserved.
2
2
// See LICENSE in the project root for license information.
3
3
4
- using CsQuery ;
5
- using FluentAssertions ;
6
4
using System ;
7
- using System . Collections . Generic ;
8
- using System . Diagnostics ;
9
5
using System . Linq ;
10
6
using System . Net ;
11
7
using System . Net . Http ;
@@ -67,209 +63,17 @@ private TestBrowserClient(CookieHandler handler)
67
63
_handler = handler ;
68
64
}
69
65
70
- public Cookie GetCookie ( string name )
71
- {
72
- return GetCookie ( _handler . CurrentUri . ToString ( ) , name ) ;
73
- }
74
- public Cookie GetCookie ( string uri , string name )
75
- {
76
- return _handler . CookieContainer . GetCookies ( new Uri ( uri ) ) . Cast < Cookie > ( ) . Where ( x => x . Name == name ) . FirstOrDefault ( ) ;
77
- }
78
-
79
66
public void RemoveCookie ( string name )
80
67
{
81
68
RemoveCookie ( CurrentUri . ToString ( ) , name ) ;
82
69
}
83
70
public void RemoveCookie ( string uri , string name )
84
71
{
85
- var cookie = CookieContainer . GetCookies ( new Uri ( uri ) ) . Cast < Cookie > ( ) . Where ( x => x . Name == name ) . FirstOrDefault ( ) ;
72
+ var cookie = CookieContainer . GetCookies ( new Uri ( uri ) ) . FirstOrDefault ( x => x . Name == name ) ;
86
73
if ( cookie != null )
87
74
{
88
75
cookie . Expired = true ;
89
76
}
90
77
}
91
-
92
- public async Task FollowRedirectAsync ( )
93
- {
94
- LastResponse . StatusCode . Should ( ) . Be ( HttpStatusCode . Redirect ) ;
95
- var location = LastResponse . Headers . Location . ToString ( ) ;
96
- await GetAsync ( location ) ;
97
- }
98
-
99
- public Task < HttpResponseMessage > PostFormAsync ( HtmlForm form )
100
- {
101
- return PostAsync ( form . Action , new FormUrlEncodedContent ( form . Inputs ) ) ;
102
- }
103
-
104
- public Task < HtmlForm > ReadFormAsync ( string selector = null )
105
- {
106
- return ReadFormAsync ( LastResponse , selector ) ;
107
- }
108
- public async Task < HtmlForm > ReadFormAsync ( HttpResponseMessage response , string selector = null )
109
- {
110
- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
111
-
112
- var htmlForm = new HtmlForm
113
- {
114
-
115
- } ;
116
-
117
- var html = await response . Content . ReadAsStringAsync ( ) ;
118
-
119
- var dom = new CQ ( html ) ;
120
- var form = dom . Find ( selector ?? "form" ) ;
121
- form . Length . Should ( ) . Be ( 1 ) ;
122
-
123
- var postUrl = form . Attr ( "action" ) ;
124
- if ( ! postUrl . StartsWith ( "http" , StringComparison . OrdinalIgnoreCase ) )
125
- {
126
- if ( postUrl . StartsWith ( "/" ) )
127
- {
128
- postUrl = CurrentUri . Scheme + "://" + CurrentUri . Authority + postUrl ;
129
- }
130
- else
131
- {
132
- postUrl = CurrentUri + postUrl ;
133
- }
134
- }
135
- htmlForm . Action = postUrl ;
136
-
137
-
138
- var data = new Dictionary < string , string > ( ) ;
139
-
140
- var inputs = form . Find ( "input" ) ;
141
- foreach ( var input in inputs )
142
- {
143
- var name = input . GetAttribute ( "name" ) ;
144
- var value = input . GetAttribute ( "value" ) ;
145
-
146
- if ( ! data . ContainsKey ( name ) )
147
- {
148
- data . Add ( name , value ) ;
149
- }
150
- else
151
- {
152
- data [ name ] = value ;
153
- }
154
- }
155
- htmlForm . Inputs = data ;
156
-
157
- return htmlForm ;
158
- }
159
-
160
-
161
- public Task < string > ReadElementTextAsync ( string selector )
162
- {
163
- return ReadElementTextAsync ( LastResponse , selector ) ;
164
- }
165
- public async Task < string > ReadElementTextAsync ( HttpResponseMessage response , string selector )
166
- {
167
- var html = await response . Content . ReadAsStringAsync ( ) ;
168
-
169
- var dom = new CQ ( html ) ;
170
- var element = dom . Find ( selector ) ;
171
- return element . Text ( ) ;
172
- }
173
-
174
- public Task < string > ReadElementAttributeAsync ( string selector , string attribute )
175
- {
176
- return ReadElementAttributeAsync ( LastResponse , selector , attribute ) ;
177
- }
178
- public async Task < string > ReadElementAttributeAsync ( HttpResponseMessage response , string selector , string attribute )
179
- {
180
- var html = await response . Content . ReadAsStringAsync ( ) ;
181
-
182
- var dom = new CQ ( html ) ;
183
- var element = dom . Find ( selector ) ;
184
- return element . Attr ( attribute ) ;
185
- }
186
-
187
- public Task AssertExistsAsync ( string selector )
188
- {
189
- return AssertExistsAsync ( LastResponse , selector ) ;
190
- }
191
-
192
- public async Task AssertExistsAsync ( HttpResponseMessage response , string selector )
193
- {
194
- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
195
-
196
- var html = await response . Content . ReadAsStringAsync ( ) ;
197
-
198
- var dom = new CQ ( html ) ;
199
- var element = dom . Find ( selector ) ;
200
- element . Length . Should ( ) . BeGreaterThan ( 0 ) ;
201
- }
202
-
203
- public Task AssertNotExistsAsync ( string selector )
204
- {
205
- return AssertNotExistsAsync ( selector ) ;
206
- }
207
- public async Task AssertNotExistsAsync ( HttpResponseMessage response , string selector )
208
- {
209
- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
210
-
211
- var html = await response . Content . ReadAsStringAsync ( ) ;
212
-
213
- var dom = new CQ ( html ) ;
214
- var element = dom . Find ( selector ) ;
215
- element . Length . Should ( ) . Be ( 0 ) ;
216
- }
217
-
218
- public Task AssertErrorPageAsync ( string error = null )
219
- {
220
- return AssertErrorPageAsync ( LastResponse , error ) ;
221
- }
222
- public async Task AssertErrorPageAsync ( HttpResponseMessage response , string error = null )
223
- {
224
- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
225
- await AssertExistsAsync ( response , ".error-page" ) ;
226
-
227
- if ( ! String . IsNullOrWhiteSpace ( error ) )
228
- {
229
- var errorText = await ReadElementTextAsync ( response , ".alert.alert-danger" ) ;
230
- errorText . Should ( ) . Contain ( error ) ;
231
- }
232
- }
233
-
234
- public Task AssertValidationErrorAsync ( string error = null )
235
- {
236
- return AssertValidationErrorAsync ( error ) ;
237
- }
238
- public async Task AssertValidationErrorAsync ( HttpResponseMessage response , string error = null )
239
- {
240
- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
241
- await AssertExistsAsync ( response , ".validation-summary-errors" ) ;
242
-
243
- if ( ! String . IsNullOrWhiteSpace ( error ) )
244
- {
245
- var errorText = await ReadElementTextAsync ( response , ".validation-summary-errors" ) ;
246
- errorText . ToLowerInvariant ( ) . Should ( ) . Contain ( error . ToLowerInvariant ( ) ) ;
247
- }
248
- }
249
- }
250
-
251
- [ DebuggerDisplay ( "{Action}, Inputs: {Inputs.Count}" ) ]
252
- public class HtmlForm
253
- {
254
- public HtmlForm ( string action = null )
255
- {
256
- Action = action ;
257
- }
258
-
259
- public string Action { get ; set ; }
260
- public Dictionary < string , string > Inputs { get ; set ; } = new Dictionary < string , string > ( ) ;
261
-
262
- public string this [ string key ]
263
- {
264
- get
265
- {
266
- if ( Inputs . ContainsKey ( key ) ) return Inputs [ key ] ;
267
- return null ;
268
- }
269
- set
270
- {
271
- Inputs [ key ] = value ;
272
- }
273
- }
274
78
}
275
79
}
0 commit comments