Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,52 @@
namespace RestApi.Test.ApiTests
{
[TestFixture]
public class ExampleV1Test : TestBase
public class WeatherForecastV1Test : TestBase
{
[TestCase("")]
public async Task TestResponseType(string route)
{
await CatchWebException(async () =>
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri(SetUp.UrlToV1Example + route)))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1_1[]>(stream);

Assert.That(data.Length, Is.EqualTo(5/* days */));
Assert.That(data[0].TemperatureC, Is.EqualTo(0));
}
});
}

[TestCase("New York")]
public async Task TestWithInvalidEndpoint(string area)
{
try
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV1Example}/Area/{area}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
}

Assert.Fail("This test should have ended up with an error response.");
}
catch (WebException e)
{
Assert.That((e.Response as HttpWebResponse)?.StatusCode,
Is.EqualTo(HttpStatusCode.BadRequest));
var err = await JsonSerializer.DeserializeAsync<BadRequestResponseModel>(e.Response.GetResponseStream());
Assert.That(err.Error.Message,
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'\."));
}
}

[TestCase("")]
[TestCase("/")]
public async Task TestExample(string route)
public async Task TestWithEmptyDate(string route)
{
await CatchWebException(async () =>
{
Expand All @@ -23,13 +64,13 @@ await CatchWebException(async () =>
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);

Assert.That(data.Length, Is.EqualTo(5));
Assert.That(data.Length, Is.EqualTo(5/* days */));
}
});
}

[TestCase("20200303", new[] { "3 Mar, 2020", "4 Mar, 2020", "5 Mar, 2020", "6 Mar, 2020", "7 Mar, 2020" })]
public async Task TestExampleWithDate(string date, string[] dates)
public async Task TestWithDate(string date, string[] dates)
{
await CatchWebException(async () =>
{
Expand All @@ -40,7 +81,7 @@ await CatchWebException(async () =>
{
data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);

Assert.That(data.Length, Is.EqualTo(5));
Assert.That(data.Length, Is.EqualTo(5/* days */));
Assert.That(data[0].Date, Is.EqualTo(dates[0]));
Assert.That(data[1].Date, Is.EqualTo(dates[1]));
Assert.That(data[2].Date, Is.EqualTo(dates[2]));
Expand All @@ -53,7 +94,7 @@ await CatchWebException(async () =>
[TestCase("202003030")]
[TestCase("2020033")]
[TestCase("test")]
public async Task TestExampleWithInvalidDate(string date)
public async Task TestWithInvalidDate(string date)
{
try
{
Expand Down
120 changes: 120 additions & 0 deletions RestApi.Test/ApiTests/WeatherForecastV1_1Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using NUnit.Framework;
using RestApi.Models;
using RestApi.Test.Models;

namespace RestApi.Test.ApiTests
{
[TestFixture]
public class WeatherForecastV1_1Test : TestBase
{
[TestCase("")]
public async Task TestResponseType(string route)
{
await CatchWebException(async () =>
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri(SetUp.UrlToV1_1Example + route)))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1_1[]>(stream);

Assert.That(data.Length, Is.EqualTo(5));
Assert.That(data[0].TemperatureC, Is.Not.EqualTo(0));
}
});
}

[TestCase("New York")]
public async Task TestWithInvalidEndpoint(string area)
{
try
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV1_1Example}/Area/{area}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
}

Assert.Fail("This test should have ended up with an error response.");
}
catch (WebException e)
{
Assert.That((e.Response as HttpWebResponse)?.StatusCode,
Is.EqualTo(HttpStatusCode.BadRequest));
var err = await JsonSerializer.DeserializeAsync<BadRequestResponseModel>(e.Response.GetResponseStream());
Assert.That(err.Error.Message,
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'\."));
}
}

[TestCase("")]
[TestCase("/")]
public async Task TestWithEmptyDate(string route)
{
await CatchWebException(async () =>
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri(SetUp.UrlToV1_1Example + route)))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);

Assert.That(data.Length, Is.EqualTo(5));
}
});
}

[TestCase("20200303", new[] { "3 Mar, 2020", "4 Mar, 2020", "5 Mar, 2020", "6 Mar, 2020", "7 Mar, 2020" })]
public async Task TestWithDate(string date, string[] dates)
{
await CatchWebException(async () =>
{
WeatherForecastV1[] data;
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV1_1Example}/{date}")))
{
data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);

Assert.That(data.Length, Is.EqualTo(5));
Assert.That(data[0].Date, Is.EqualTo(dates[0]));
Assert.That(data[1].Date, Is.EqualTo(dates[1]));
Assert.That(data[2].Date, Is.EqualTo(dates[2]));
Assert.That(data[3].Date, Is.EqualTo(dates[3]));
Assert.That(data[4].Date, Is.EqualTo(dates[4]));
}
});
}

[TestCase("202003030")]
[TestCase("2020033")]
[TestCase("test")]
public async Task TestWithInvalidDate(string date)
{
try
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV1_1Example}/{date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
}

Assert.Fail("This test should have ended up with an error response.");
}
catch (WebException e)
{
Assert.That((e.Response as HttpWebResponse)?.StatusCode,
Is.EqualTo(HttpStatusCode.InternalServerError));
var err = await JsonSerializer.DeserializeAsync<ValidationErrorResponseModel>(e.Response.GetResponseStream());
Assert.That(err.Title,
Is.EqualTo($"'{date}' in URL should be in yyyyMMdd format. (Parameter 'date')"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json;
Expand All @@ -10,65 +11,56 @@
namespace RestApi.Test.ApiTests
{
[TestFixture]
public class ExampleV2Test : TestBase
public class WeatherForecastV2Test : TestBase
{
[TestCase("")]
[TestCase("/20200303")]
public async Task TestExample(string route)
public async Task TestResponseType(string route)
{
try
await CatchWebException(async () =>
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri(SetUp.UrlToV2Example + route)))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);

Assert.That(data.Length, Is.EqualTo(5/* days */ * 3/* areas */));
Assert.That(data[0].TemperatureC, Is.Not.EqualTo(0));
}

Assert.Fail("This test should have ended up with an error response.");
}
catch (WebException e)
{
Assert.That((e.Response as HttpWebResponse)?.StatusCode,
Is.EqualTo(HttpStatusCode.BadRequest));
var data = await JsonSerializer.DeserializeAsync<BadRequestResponseModel>(
e.Response.GetResponseStream());
Assert.That(data?.Error?.Message,
Is.EqualTo($"The HTTP resource that matches the request URI 'https://localhost:62184/api/v2/Example{route}' does not support the API version '2'."));
}
});
}

[TestCase("")]
[TestCase("/")]
public async Task TestExampleWF(string route)
public async Task TestWithEmptyDate(string route)
{
await CatchWebException(async () =>
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri(SetUp.UrlToV2ExampleWF + route)))
new Uri(SetUp.UrlToV2Example + route)))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);

Assert.That(data.Length, Is.EqualTo(5 * 3));
Assert.That(data.Length, Is.EqualTo(5/* days */ * 3/* areas */));
}
});
}

[TestCase("20200303", "2020-03-03")]
[TestCase("", null)]
public async Task TestExampleWFWithValidDate(string date, DateTime? firstDate)
public async Task TestWithValidDate(string date, DateTime? firstDate)
{
await CatchWebException(async () =>
{
var now = DateTime.UtcNow;
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}?from={date}")))
new Uri($"{SetUp.UrlToV2Example}?from={date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);

Assert.That(data.Length, Is.EqualTo(5 * 3));
Assert.That(data.Length, Is.EqualTo(5/* days */ * 3/* areas */));
Assert.That(data.Min(d => d.Date),
Is.EqualTo(firstDate ?? new DateTime(now.Year, now.Month, now.Day)));
}
Expand All @@ -78,13 +70,13 @@ await CatchWebException(async () =>
[TestCase("202003030")]
[TestCase("2020033")]
[TestCase("New York")]
public async Task TestExampleWFWithInvalidDate(string date)
public async Task TestWithInvalidDate(string date)
{
try
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}?from={date}")))
new Uri($"{SetUp.UrlToV2Example}?from={date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
}
Expand All @@ -102,36 +94,62 @@ public async Task TestExampleWFWithInvalidDate(string date)
}
}

[TestCase("", "20200305", 4, "2020-03-05")]
[TestCase("", "20200305", null, "2020-03-05")]
[TestCase("", "", 3, null)]
[TestCase("", "", null, null)]
[TestCase("/", "20200305", 4, "2020-03-05")]
[TestCase("/", "20200305", null, "2020-03-05")]
[TestCase("/", "", 3, null)]
[TestCase("/", "", null, null)]
public async Task TestWithEmptyAreaAndValidDate(string route, string date, int? days, DateTime? firstDate)
{
await CatchWebException(async () =>
{
var now = DateTime.UtcNow;
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2Example}/Area{route}?from={date}{(days == null ? "" : $"&days={days}")}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);

Assert.That(data.Length, Is.EqualTo((days ?? 5/* days */) * 3/* areas */));
Assert.That(data.Min(d => d.Date),
Is.EqualTo(firstDate ?? new DateTime(now.Year, now.Month, now.Day)));
}
});
}

[TestCase("New York", "20200305", 4, "2020-03-05")]
[TestCase("New York", "20200305", null, "2020-03-05")]
[TestCase("New York", "", 3, null)]
[TestCase("New York", "", null, null)]
public async Task TestExampleWFWithValidAreaAndValidDate(string area, string date, int? days, DateTime? firstDate)
public async Task TestWithValidAreaAndValidDate(string area, string date, int? days, DateTime? firstDate)
{
await CatchWebException(async () =>
{
var now = DateTime.UtcNow;
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}{(days == null ? "" : $"&days={days}")}")))
new Uri($"{SetUp.UrlToV2Example}/Area/{area}?from={date}{(days == null ? "" : $"&days={days}")}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);

Assert.That(data.Length, Is.EqualTo(days ?? 5));
Assert.That(data.Length, Is.EqualTo(days ?? 5/* days */));
Assert.That(data.Min(d => d.Date),
Is.EqualTo(firstDate ?? new DateTime(now.Year, now.Month, now.Day)));
}
});
}

[TestCase("Auckland", "20200303")]
public async Task TestExampleWFWithInvalidAreaAndValidDate(string area, string date)
public async Task TestWithInvalidAreaAndValidDate(string area, string date)
{
try
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}")))
new Uri($"{SetUp.UrlToV2Example}/Area/{area}?from={date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
}
Expand All @@ -151,13 +169,13 @@ public async Task TestExampleWFWithInvalidAreaAndValidDate(string area, string d
[TestCase("New York", "202003030")]
[TestCase("New York", "2020033")]
[TestCase("New York", "test")]
public async Task TestExampleWFWithValidAreaAndInvalidDate(string area, string date)
public async Task TestWithValidAreaAndInvalidDate(string area, string date)
{
try
{
using (var client = new WebClient())
using (var stream = await client.OpenReadTaskAsync(
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}")))
new Uri($"{SetUp.UrlToV2Example}/Area/{area}?from={date}")))
{
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
}
Expand Down
Loading