Skip to content

Commit 0fa720c

Browse files
authored
Merge pull request #5 from y-code/restructure-api-endpoints
restructure API endpoints
2 parents 7749b94 + 00190d6 commit 0fa720c

9 files changed

+327
-107
lines changed

RestApi.Test/ApiTests/ExampleV1Test.cs renamed to RestApi.Test/ApiTests/WeatherForecastV1Test.cs

+47-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,52 @@
99
namespace RestApi.Test.ApiTests
1010
{
1111
[TestFixture]
12-
public class ExampleV1Test : TestBase
12+
public class WeatherForecastV1Test : TestBase
1313
{
14+
[TestCase("")]
15+
public async Task TestResponseType(string route)
16+
{
17+
await CatchWebException(async () =>
18+
{
19+
using (var client = new WebClient())
20+
using (var stream = await client.OpenReadTaskAsync(
21+
new Uri(SetUp.UrlToV1Example + route)))
22+
{
23+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1_1[]>(stream);
24+
25+
Assert.That(data.Length, Is.EqualTo(5/* days */));
26+
Assert.That(data[0].TemperatureC, Is.EqualTo(0));
27+
}
28+
});
29+
}
30+
31+
[TestCase("New York")]
32+
public async Task TestWithInvalidEndpoint(string area)
33+
{
34+
try
35+
{
36+
using (var client = new WebClient())
37+
using (var stream = await client.OpenReadTaskAsync(
38+
new Uri($"{SetUp.UrlToV1Example}/Area/{area}")))
39+
{
40+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
41+
}
42+
43+
Assert.Fail("This test should have ended up with an error response.");
44+
}
45+
catch (WebException e)
46+
{
47+
Assert.That((e.Response as HttpWebResponse)?.StatusCode,
48+
Is.EqualTo(HttpStatusCode.BadRequest));
49+
var err = await JsonSerializer.DeserializeAsync<BadRequestResponseModel>(e.Response.GetResponseStream());
50+
Assert.That(err.Error.Message,
51+
Does.Match(@"The HTTP resource that matches the request URI 'https://localhost:[0-9]+/api/v1/WeatherForecast/Area/New%20York' does not support the API version '1'\."));
52+
}
53+
}
54+
1455
[TestCase("")]
1556
[TestCase("/")]
16-
public async Task TestExample(string route)
57+
public async Task TestWithEmptyDate(string route)
1758
{
1859
await CatchWebException(async () =>
1960
{
@@ -23,13 +64,13 @@ await CatchWebException(async () =>
2364
{
2465
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
2566

26-
Assert.That(data.Length, Is.EqualTo(5));
67+
Assert.That(data.Length, Is.EqualTo(5/* days */));
2768
}
2869
});
2970
}
3071

3172
[TestCase("20200303", new[] { "3 Mar, 2020", "4 Mar, 2020", "5 Mar, 2020", "6 Mar, 2020", "7 Mar, 2020" })]
32-
public async Task TestExampleWithDate(string date, string[] dates)
73+
public async Task TestWithDate(string date, string[] dates)
3374
{
3475
await CatchWebException(async () =>
3576
{
@@ -40,7 +81,7 @@ await CatchWebException(async () =>
4081
{
4182
data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
4283

43-
Assert.That(data.Length, Is.EqualTo(5));
84+
Assert.That(data.Length, Is.EqualTo(5/* days */));
4485
Assert.That(data[0].Date, Is.EqualTo(dates[0]));
4586
Assert.That(data[1].Date, Is.EqualTo(dates[1]));
4687
Assert.That(data[2].Date, Is.EqualTo(dates[2]));
@@ -53,7 +94,7 @@ await CatchWebException(async () =>
5394
[TestCase("202003030")]
5495
[TestCase("2020033")]
5596
[TestCase("test")]
56-
public async Task TestExampleWithInvalidDate(string date)
97+
public async Task TestWithInvalidDate(string date)
5798
{
5899
try
59100
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using System.Net;
3+
using System.Text.Json;
4+
using System.Threading.Tasks;
5+
using NUnit.Framework;
6+
using RestApi.Models;
7+
using RestApi.Test.Models;
8+
9+
namespace RestApi.Test.ApiTests
10+
{
11+
[TestFixture]
12+
public class WeatherForecastV1_1Test : TestBase
13+
{
14+
[TestCase("")]
15+
public async Task TestResponseType(string route)
16+
{
17+
await CatchWebException(async () =>
18+
{
19+
using (var client = new WebClient())
20+
using (var stream = await client.OpenReadTaskAsync(
21+
new Uri(SetUp.UrlToV1_1Example + route)))
22+
{
23+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1_1[]>(stream);
24+
25+
Assert.That(data.Length, Is.EqualTo(5));
26+
Assert.That(data[0].TemperatureC, Is.Not.EqualTo(0));
27+
}
28+
});
29+
}
30+
31+
[TestCase("New York")]
32+
public async Task TestWithInvalidEndpoint(string area)
33+
{
34+
try
35+
{
36+
using (var client = new WebClient())
37+
using (var stream = await client.OpenReadTaskAsync(
38+
new Uri($"{SetUp.UrlToV1_1Example}/Area/{area}")))
39+
{
40+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
41+
}
42+
43+
Assert.Fail("This test should have ended up with an error response.");
44+
}
45+
catch (WebException e)
46+
{
47+
Assert.That((e.Response as HttpWebResponse)?.StatusCode,
48+
Is.EqualTo(HttpStatusCode.BadRequest));
49+
var err = await JsonSerializer.DeserializeAsync<BadRequestResponseModel>(e.Response.GetResponseStream());
50+
Assert.That(err.Error.Message,
51+
Does.Match(@"The HTTP resource that matches the request URI 'https://localhost:[0-9]+/api/v1.1/WeatherForecast/Area/New%20York' does not support the API version '1.1'\."));
52+
}
53+
}
54+
55+
[TestCase("")]
56+
[TestCase("/")]
57+
public async Task TestWithEmptyDate(string route)
58+
{
59+
await CatchWebException(async () =>
60+
{
61+
using (var client = new WebClient())
62+
using (var stream = await client.OpenReadTaskAsync(
63+
new Uri(SetUp.UrlToV1_1Example + route)))
64+
{
65+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
66+
67+
Assert.That(data.Length, Is.EqualTo(5));
68+
}
69+
});
70+
}
71+
72+
[TestCase("20200303", new[] { "3 Mar, 2020", "4 Mar, 2020", "5 Mar, 2020", "6 Mar, 2020", "7 Mar, 2020" })]
73+
public async Task TestWithDate(string date, string[] dates)
74+
{
75+
await CatchWebException(async () =>
76+
{
77+
WeatherForecastV1[] data;
78+
using (var client = new WebClient())
79+
using (var stream = await client.OpenReadTaskAsync(
80+
new Uri($"{SetUp.UrlToV1_1Example}/{date}")))
81+
{
82+
data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
83+
84+
Assert.That(data.Length, Is.EqualTo(5));
85+
Assert.That(data[0].Date, Is.EqualTo(dates[0]));
86+
Assert.That(data[1].Date, Is.EqualTo(dates[1]));
87+
Assert.That(data[2].Date, Is.EqualTo(dates[2]));
88+
Assert.That(data[3].Date, Is.EqualTo(dates[3]));
89+
Assert.That(data[4].Date, Is.EqualTo(dates[4]));
90+
}
91+
});
92+
}
93+
94+
[TestCase("202003030")]
95+
[TestCase("2020033")]
96+
[TestCase("test")]
97+
public async Task TestWithInvalidDate(string date)
98+
{
99+
try
100+
{
101+
using (var client = new WebClient())
102+
using (var stream = await client.OpenReadTaskAsync(
103+
new Uri($"{SetUp.UrlToV1_1Example}/{date}")))
104+
{
105+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
106+
}
107+
108+
Assert.Fail("This test should have ended up with an error response.");
109+
}
110+
catch (WebException e)
111+
{
112+
Assert.That((e.Response as HttpWebResponse)?.StatusCode,
113+
Is.EqualTo(HttpStatusCode.InternalServerError));
114+
var err = await JsonSerializer.DeserializeAsync<ValidationErrorResponseModel>(e.Response.GetResponseStream());
115+
Assert.That(err.Title,
116+
Is.EqualTo($"'{date}' in URL should be in yyyyMMdd format. (Parameter 'date')"));
117+
}
118+
}
119+
}
120+
}

RestApi.Test/ApiTests/ExampleV2Test.cs renamed to RestApi.Test/ApiTests/WeatherForecastV2Test.cs

+49-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Net;
45
using System.Text.Json;
@@ -10,65 +11,56 @@
1011
namespace RestApi.Test.ApiTests
1112
{
1213
[TestFixture]
13-
public class ExampleV2Test : TestBase
14+
public class WeatherForecastV2Test : TestBase
1415
{
1516
[TestCase("")]
16-
[TestCase("/20200303")]
17-
public async Task TestExample(string route)
17+
public async Task TestResponseType(string route)
1818
{
19-
try
19+
await CatchWebException(async () =>
2020
{
2121
using (var client = new WebClient())
2222
using (var stream = await client.OpenReadTaskAsync(
2323
new Uri(SetUp.UrlToV2Example + route)))
2424
{
2525
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));
2629
}
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+
});
3931
}
4032

4133
[TestCase("")]
4234
[TestCase("/")]
43-
public async Task TestExampleWF(string route)
35+
public async Task TestWithEmptyDate(string route)
4436
{
4537
await CatchWebException(async () =>
4638
{
4739
using (var client = new WebClient())
4840
using (var stream = await client.OpenReadTaskAsync(
49-
new Uri(SetUp.UrlToV2ExampleWF + route)))
41+
new Uri(SetUp.UrlToV2Example + route)))
5042
{
5143
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
5244

53-
Assert.That(data.Length, Is.EqualTo(5 * 3));
45+
Assert.That(data.Length, Is.EqualTo(5/* days */ * 3/* areas */));
5446
}
5547
});
5648
}
5749

5850
[TestCase("20200303", "2020-03-03")]
5951
[TestCase("", null)]
60-
public async Task TestExampleWFWithValidDate(string date, DateTime? firstDate)
52+
public async Task TestWithValidDate(string date, DateTime? firstDate)
6153
{
6254
await CatchWebException(async () =>
6355
{
6456
var now = DateTime.UtcNow;
6557
using (var client = new WebClient())
6658
using (var stream = await client.OpenReadTaskAsync(
67-
new Uri($"{SetUp.UrlToV2ExampleWF}?from={date}")))
59+
new Uri($"{SetUp.UrlToV2Example}?from={date}")))
6860
{
6961
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
7062

71-
Assert.That(data.Length, Is.EqualTo(5 * 3));
63+
Assert.That(data.Length, Is.EqualTo(5/* days */ * 3/* areas */));
7264
Assert.That(data.Min(d => d.Date),
7365
Is.EqualTo(firstDate ?? new DateTime(now.Year, now.Month, now.Day)));
7466
}
@@ -78,13 +70,13 @@ await CatchWebException(async () =>
7870
[TestCase("202003030")]
7971
[TestCase("2020033")]
8072
[TestCase("New York")]
81-
public async Task TestExampleWFWithInvalidDate(string date)
73+
public async Task TestWithInvalidDate(string date)
8274
{
8375
try
8476
{
8577
using (var client = new WebClient())
8678
using (var stream = await client.OpenReadTaskAsync(
87-
new Uri($"{SetUp.UrlToV2ExampleWF}?from={date}")))
79+
new Uri($"{SetUp.UrlToV2Example}?from={date}")))
8880
{
8981
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
9082
}
@@ -102,36 +94,62 @@ public async Task TestExampleWFWithInvalidDate(string date)
10294
}
10395
}
10496

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+
105123
[TestCase("New York", "20200305", 4, "2020-03-05")]
106124
[TestCase("New York", "20200305", null, "2020-03-05")]
107125
[TestCase("New York", "", 3, null)]
108126
[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)
110128
{
111129
await CatchWebException(async () =>
112130
{
113131
var now = DateTime.UtcNow;
114132
using (var client = new WebClient())
115133
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}")}")))
117135
{
118136
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
119137

120-
Assert.That(data.Length, Is.EqualTo(days ?? 5));
138+
Assert.That(data.Length, Is.EqualTo(days ?? 5/* days */));
121139
Assert.That(data.Min(d => d.Date),
122140
Is.EqualTo(firstDate ?? new DateTime(now.Year, now.Month, now.Day)));
123141
}
124142
});
125143
}
126144

127145
[TestCase("Auckland", "20200303")]
128-
public async Task TestExampleWFWithInvalidAreaAndValidDate(string area, string date)
146+
public async Task TestWithInvalidAreaAndValidDate(string area, string date)
129147
{
130148
try
131149
{
132150
using (var client = new WebClient())
133151
using (var stream = await client.OpenReadTaskAsync(
134-
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}")))
152+
new Uri($"{SetUp.UrlToV2Example}/Area/{area}?from={date}")))
135153
{
136154
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
137155
}
@@ -151,13 +169,13 @@ public async Task TestExampleWFWithInvalidAreaAndValidDate(string area, string d
151169
[TestCase("New York", "202003030")]
152170
[TestCase("New York", "2020033")]
153171
[TestCase("New York", "test")]
154-
public async Task TestExampleWFWithValidAreaAndInvalidDate(string area, string date)
172+
public async Task TestWithValidAreaAndInvalidDate(string area, string date)
155173
{
156174
try
157175
{
158176
using (var client = new WebClient())
159177
using (var stream = await client.OpenReadTaskAsync(
160-
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}")))
178+
new Uri($"{SetUp.UrlToV2Example}/Area/{area}?from={date}")))
161179
{
162180
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
163181
}

0 commit comments

Comments
 (0)