1
1
#region License
2
2
// Distributed under the BSD License
3
3
// =================================
4
- //
4
+ //
5
5
// Copyright (c) 2010, Hadi Hariri
6
6
// All rights reserved.
7
- //
7
+ //
8
8
// Redistribution and use in source and binary forms, with or without
9
9
// modification, are permitted provided that the following conditions are met:
10
10
// * Redistributions of source code must retain the above copyright
15
15
// * Neither the name of Hadi Hariri nor the
16
16
// names of its contributors may be used to endorse or promote products
17
17
// derived from this software without specific prior written permission.
18
- //
18
+ //
19
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
20
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
21
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
28
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
29
// =============================================================
30
- //
31
- //
30
+ //
31
+ //
32
32
// Parts of this Software use JsonFX Serialization Library which is distributed under the MIT License:
33
- //
33
+ //
34
34
// Distributed under the terms of an MIT-style license:
35
- //
35
+ //
36
36
// The MIT License
37
- //
37
+ //
38
38
// Copyright (c) 2006-2009 Stephen M. McKamey
39
- //
39
+ //
40
40
// Permission is hereby granted, free of charge, to any person obtaining a copy
41
41
// of this software and associated documentation files (the "Software"), to deal
42
42
// in the Software without restriction, including without limitation the rights
43
43
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
44
44
// copies of the Software, and to permit persons to whom the Software is
45
45
// furnished to do so, subject to the following conditions:
46
- //
46
+ //
47
47
// The above copyright notice and this permission notice shall be included in
48
48
// all copies or substantial portions of the Software.
49
- //
49
+ //
50
50
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51
51
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52
52
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57
57
#endregion
58
58
59
59
using System ;
60
+ using System . Collections . Generic ;
60
61
using System . Net ;
61
62
using EasyHttp . Http ;
62
63
using EasyHttp . Specs . Helpers ;
63
64
using Machine . Specifications ;
65
+ using NSubstitute ;
64
66
using Result = EasyHttp . Specs . Helpers . ResultResponse ;
65
67
66
68
namespace EasyHttp . Specs . Specs
@@ -86,7 +88,7 @@ public class when_making_any_type_of_request_to_invalid_host
86
88
}
87
89
88
90
[ Subject ( "HttpClient" ) ]
89
- public class when_making_a_DELETE_request_with_a_valid_uri
91
+ public class when_making_a_DELETE_request_with_a_valid_uri
90
92
{
91
93
Establish context = ( ) =>
92
94
{
@@ -141,11 +143,95 @@ public class when_making_a_GET_request_with_valid_uri
141
143
It should_return_body_with_rawtext =
142
144
( ) => httpResponse . RawText . ShouldNotBeEmpty ( ) ;
143
145
144
-
146
+
145
147
static HttpClient httpClient ;
146
148
static HttpResponse httpResponse ;
147
149
}
148
150
151
+ [ Subject ( "HttpClient" ) ]
152
+ public class when_mocking_a_GET_request_with_valid_uri_to_return_a_NotFound
153
+ {
154
+ Establish context = ( ) =>
155
+ {
156
+ var injectedResponse = Substitute . For < HttpResponse > ( ) ;
157
+ injectedResponse . StatusCode . Returns ( HttpStatusCode . NotFound ) ;
158
+
159
+ httpClient = Substitute . For < HttpClient > ( ) ;
160
+ httpClient . Get ( Arg . Any < string > ( ) ) . Returns ( injectedResponse ) ;
161
+ } ;
162
+
163
+ Because of = ( ) =>
164
+ {
165
+ httpResponse = httpClient . Get ( "http://localhost:16000" ) ;
166
+ } ;
167
+
168
+ It should_return_a_NotFound =
169
+ ( ) => httpResponse . StatusCode . ShouldEqual ( HttpStatusCode . NotFound ) ;
170
+
171
+
172
+ static HttpClient httpClient ;
173
+ static HttpResponse httpResponse ;
174
+ }
175
+
176
+ [ Subject ( "HttpClient" ) ]
177
+ public class when_mocking_a_GET_request_with_valid_uri_to_inject_a_specific_response
178
+ {
179
+ Establish context = ( ) =>
180
+ {
181
+ var injectedResponseBody =
182
+ @"{
183
+ 'name': 'Serenity',
184
+ 'characterNames': [
185
+ 'Mal',
186
+ 'Wash',
187
+ 'Zoe'
188
+ ]
189
+ }" ;
190
+ httpClient = new HttpClient ( ) ;
191
+
192
+ httpClient . OnRequest ( r =>
193
+ r . Uri . Contains ( "localhost:16000" )
194
+ && r . Method == HttpMethod . POST
195
+ )
196
+ . InjectResponse (
197
+ HttpStatusCode . BadRequest ,
198
+ HttpContentTypes . ApplicationJson ,
199
+ injectedResponseBody
200
+ ) ;
201
+ } ;
202
+
203
+ Because of = ( ) =>
204
+ {
205
+ httpGetResponse = httpClient . Get ( "http://localhost:16000" ) ;
206
+ httpPostResponse = httpClient . Post ( "http://localhost:16000" , null , HttpContentTypes . ApplicationJson ) ;
207
+ } ;
208
+
209
+ It should_return_OK_for_the_Get =
210
+ ( ) => httpGetResponse . StatusCode . ShouldEqual ( HttpStatusCode . OK ) ;
211
+
212
+ It should_return_BadRequest_for_the_Post =
213
+ ( ) => httpPostResponse . StatusCode . ShouldEqual ( HttpStatusCode . BadRequest ) ;
214
+
215
+ It should_return_the_injected_content_type_for_the_Post =
216
+ ( ) => httpPostResponse . ContentType . ShouldEndWith ( HttpContentTypes . ApplicationJson ) ;
217
+
218
+ It should_return_the_injected_body_content_for_the_Post =
219
+ ( ) =>
220
+ {
221
+ // have to explicitly cast this dynamic member so that the
222
+ // Should extensions can bind properly at run-time.
223
+ var characterNames = httpPostResponse . DynamicBody . characterNames as string [ ] ;
224
+
225
+ characterNames . ShouldNotBeNull ( ) ;
226
+ characterNames . Length . ShouldEqual ( 3 ) ;
227
+ characterNames [ 1 ] . ShouldEqual ( "Wash" ) ;
228
+ } ;
229
+
230
+ static HttpClient httpClient ;
231
+ static HttpResponse httpGetResponse ;
232
+ static HttpResponse httpPostResponse ;
233
+ }
234
+
149
235
[ Subject ( "HttpClient" ) ]
150
236
public class when_making_a_GET_request_with_valid_uri_and__and_valid_parameters
151
237
{
@@ -302,7 +388,7 @@ public class when_making_a_GET_request_with_valid_uri_and_content_type_set_to_ap
302
388
static HttpResponse response ;
303
389
}
304
390
305
-
391
+
306
392
[ Subject ( "HttpClient" ) ]
307
393
public class when_making_a_HEAD_request_with_valid_uri
308
394
{
0 commit comments