1
1
using System ;
2
+ using System . IO ;
2
3
using System . Linq ;
3
4
using System . Net ;
4
5
using System . Text . Json ;
10
11
namespace RestApi . Test . ApiTests
11
12
{
12
13
[ TestFixture ]
13
- public class ExampleV2Test : TestBase
14
+ public class WeatherForecastV2Test : TestBase
14
15
{
15
16
[ TestCase ( "" ) ]
16
- [ TestCase ( "/20200303" ) ]
17
- public async Task TestExample ( string route )
17
+ public async Task TestResponseType ( string route )
18
18
{
19
- try
19
+ await CatchWebException ( async ( ) =>
20
20
{
21
21
using ( var client = new WebClient ( ) )
22
22
using ( var stream = await client . OpenReadTaskAsync (
23
23
new Uri ( SetUp . UrlToV2Example + route ) ) )
24
24
{
25
25
var data = await JsonSerializer . DeserializeAsync < WeatherForecastV2 [ ] > ( stream ) ;
26
+
27
+ Assert . That ( data . Length , Is . EqualTo ( 5 /* days */ * 3 /* areas */ ) ) ;
28
+ Assert . That ( data [ 0 ] . TemperatureC , Is . Not . EqualTo ( 0 ) ) ;
26
29
}
27
-
28
- Assert . Fail ( "This test should have ended up with an error response." ) ;
29
- }
30
- catch ( WebException e )
31
- {
32
- Assert . That ( ( e . Response as HttpWebResponse ) ? . StatusCode ,
33
- Is . EqualTo ( HttpStatusCode . BadRequest ) ) ;
34
- var data = await JsonSerializer . DeserializeAsync < BadRequestResponseModel > (
35
- e . Response . GetResponseStream ( ) ) ;
36
- Assert . That ( data ? . Error ? . Message ,
37
- Is . EqualTo ( $ "The HTTP resource that matches the request URI 'https://localhost:62184/api/v2/Example{ route } ' does not support the API version '2'.") ) ;
38
- }
30
+ } ) ;
39
31
}
40
32
41
33
[ TestCase ( "" ) ]
42
34
[ TestCase ( "/" ) ]
43
- public async Task TestExampleWF ( string route )
35
+ public async Task TestWithEmptyDate ( string route )
44
36
{
45
37
await CatchWebException ( async ( ) =>
46
38
{
47
39
using ( var client = new WebClient ( ) )
48
40
using ( var stream = await client . OpenReadTaskAsync (
49
- new Uri ( SetUp . UrlToV2ExampleWF + route ) ) )
41
+ new Uri ( SetUp . UrlToV2Example + route ) ) )
50
42
{
51
43
var data = await JsonSerializer . DeserializeAsync < WeatherForecastV2 [ ] > ( stream ) ;
52
44
53
- Assert . That ( data . Length , Is . EqualTo ( 5 * 3 ) ) ;
45
+ Assert . That ( data . Length , Is . EqualTo ( 5 /* days */ * 3 /* areas */ ) ) ;
54
46
}
55
47
} ) ;
56
48
}
57
49
58
50
[ TestCase ( "20200303" , "2020-03-03" ) ]
59
51
[ TestCase ( "" , null ) ]
60
- public async Task TestExampleWFWithValidDate ( string date , DateTime ? firstDate )
52
+ public async Task TestWithValidDate ( string date , DateTime ? firstDate )
61
53
{
62
54
await CatchWebException ( async ( ) =>
63
55
{
64
56
var now = DateTime . UtcNow ;
65
57
using ( var client = new WebClient ( ) )
66
58
using ( var stream = await client . OpenReadTaskAsync (
67
- new Uri ( $ "{ SetUp . UrlToV2ExampleWF } ?from={ date } ") ) )
59
+ new Uri ( $ "{ SetUp . UrlToV2Example } ?from={ date } ") ) )
68
60
{
69
61
var data = await JsonSerializer . DeserializeAsync < WeatherForecastV2 [ ] > ( stream ) ;
70
62
71
- Assert . That ( data . Length , Is . EqualTo ( 5 * 3 ) ) ;
63
+ Assert . That ( data . Length , Is . EqualTo ( 5 /* days */ * 3 /* areas */ ) ) ;
72
64
Assert . That ( data . Min ( d => d . Date ) ,
73
65
Is . EqualTo ( firstDate ?? new DateTime ( now . Year , now . Month , now . Day ) ) ) ;
74
66
}
@@ -78,13 +70,13 @@ await CatchWebException(async () =>
78
70
[ TestCase ( "202003030" ) ]
79
71
[ TestCase ( "2020033" ) ]
80
72
[ TestCase ( "New York" ) ]
81
- public async Task TestExampleWFWithInvalidDate ( string date )
73
+ public async Task TestWithInvalidDate ( string date )
82
74
{
83
75
try
84
76
{
85
77
using ( var client = new WebClient ( ) )
86
78
using ( var stream = await client . OpenReadTaskAsync (
87
- new Uri ( $ "{ SetUp . UrlToV2ExampleWF } ?from={ date } ") ) )
79
+ new Uri ( $ "{ SetUp . UrlToV2Example } ?from={ date } ") ) )
88
80
{
89
81
var data = await JsonSerializer . DeserializeAsync < WeatherForecastV2 [ ] > ( stream ) ;
90
82
}
@@ -102,36 +94,62 @@ public async Task TestExampleWFWithInvalidDate(string date)
102
94
}
103
95
}
104
96
97
+ [ TestCase ( "" , "20200305" , 4 , "2020-03-05" ) ]
98
+ [ TestCase ( "" , "20200305" , null , "2020-03-05" ) ]
99
+ [ TestCase ( "" , "" , 3 , null ) ]
100
+ [ TestCase ( "" , "" , null , null ) ]
101
+ [ TestCase ( "/" , "20200305" , 4 , "2020-03-05" ) ]
102
+ [ TestCase ( "/" , "20200305" , null , "2020-03-05" ) ]
103
+ [ TestCase ( "/" , "" , 3 , null ) ]
104
+ [ TestCase ( "/" , "" , null , null ) ]
105
+ public async Task TestWithEmptyAreaAndValidDate ( string route , string date , int ? days , DateTime ? firstDate )
106
+ {
107
+ await CatchWebException ( async ( ) =>
108
+ {
109
+ var now = DateTime . UtcNow ;
110
+ using ( var client = new WebClient ( ) )
111
+ using ( var stream = await client . OpenReadTaskAsync (
112
+ new Uri ( $ "{ SetUp . UrlToV2Example } /Area{ route } ?from={ date } { ( days == null ? "" : $ "&days={ days } ") } ") ) )
113
+ {
114
+ var data = await JsonSerializer . DeserializeAsync < WeatherForecastV2 [ ] > ( stream ) ;
115
+
116
+ Assert . That ( data . Length , Is . EqualTo ( ( days ?? 5 /* days */ ) * 3 /* areas */ ) ) ;
117
+ Assert . That ( data . Min ( d => d . Date ) ,
118
+ Is . EqualTo ( firstDate ?? new DateTime ( now . Year , now . Month , now . Day ) ) ) ;
119
+ }
120
+ } ) ;
121
+ }
122
+
105
123
[ TestCase ( "New York" , "20200305" , 4 , "2020-03-05" ) ]
106
124
[ TestCase ( "New York" , "20200305" , null , "2020-03-05" ) ]
107
125
[ TestCase ( "New York" , "" , 3 , null ) ]
108
126
[ TestCase ( "New York" , "" , null , null ) ]
109
- public async Task TestExampleWFWithValidAreaAndValidDate ( string area , string date , int ? days , DateTime ? firstDate )
127
+ public async Task TestWithValidAreaAndValidDate ( string area , string date , int ? days , DateTime ? firstDate )
110
128
{
111
129
await CatchWebException ( async ( ) =>
112
130
{
113
131
var now = DateTime . UtcNow ;
114
132
using ( var client = new WebClient ( ) )
115
133
using ( var stream = await client . OpenReadTaskAsync (
116
- new Uri ( $ "{ SetUp . UrlToV2ExampleWF } / { area } ?from={ date } { ( days == null ? "" : $ "&days={ days } ") } ") ) )
134
+ new Uri ( $ "{ SetUp . UrlToV2Example } /Area/ { area } ?from={ date } { ( days == null ? "" : $ "&days={ days } ") } ") ) )
117
135
{
118
136
var data = await JsonSerializer . DeserializeAsync < WeatherForecastV2 [ ] > ( stream ) ;
119
137
120
- Assert . That ( data . Length , Is . EqualTo ( days ?? 5 ) ) ;
138
+ Assert . That ( data . Length , Is . EqualTo ( days ?? 5 /* days */ ) ) ;
121
139
Assert . That ( data . Min ( d => d . Date ) ,
122
140
Is . EqualTo ( firstDate ?? new DateTime ( now . Year , now . Month , now . Day ) ) ) ;
123
141
}
124
142
} ) ;
125
143
}
126
144
127
145
[ TestCase ( "Auckland" , "20200303" ) ]
128
- public async Task TestExampleWFWithInvalidAreaAndValidDate ( string area , string date )
146
+ public async Task TestWithInvalidAreaAndValidDate ( string area , string date )
129
147
{
130
148
try
131
149
{
132
150
using ( var client = new WebClient ( ) )
133
151
using ( var stream = await client . OpenReadTaskAsync (
134
- new Uri ( $ "{ SetUp . UrlToV2ExampleWF } /{ area } ?from={ date } ") ) )
152
+ new Uri ( $ "{ SetUp . UrlToV2Example } /Area /{ area } ?from={ date } ") ) )
135
153
{
136
154
var data = await JsonSerializer . DeserializeAsync < WeatherForecastV2 [ ] > ( stream ) ;
137
155
}
@@ -151,13 +169,13 @@ public async Task TestExampleWFWithInvalidAreaAndValidDate(string area, string d
151
169
[ TestCase ( "New York" , "202003030" ) ]
152
170
[ TestCase ( "New York" , "2020033" ) ]
153
171
[ TestCase ( "New York" , "test" ) ]
154
- public async Task TestExampleWFWithValidAreaAndInvalidDate ( string area , string date )
172
+ public async Task TestWithValidAreaAndInvalidDate ( string area , string date )
155
173
{
156
174
try
157
175
{
158
176
using ( var client = new WebClient ( ) )
159
177
using ( var stream = await client . OpenReadTaskAsync (
160
- new Uri ( $ "{ SetUp . UrlToV2ExampleWF } /{ area } ?from={ date } ") ) )
178
+ new Uri ( $ "{ SetUp . UrlToV2Example } /Area /{ area } ?from={ date } ") ) )
161
179
{
162
180
var data = await JsonSerializer . DeserializeAsync < WeatherForecastV2 [ ] > ( stream ) ;
163
181
}
0 commit comments