-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcad739
commit f0e9dde
Showing
5 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using FakeAuth; | ||
|
||
|
||
namespace Calebs.WebAPI.Tests; | ||
|
||
internal class WebAPIApplication : WebApplicationFactory<Program> | ||
{ | ||
private readonly string _environment; | ||
|
||
public WebAPIApplication(string environment = "Development") | ||
{ | ||
_environment = environment; | ||
} | ||
|
||
protected override IHost CreateHost(IHostBuilder builder) | ||
{ | ||
builder.UseEnvironment(_environment); | ||
|
||
// Add mock/test services to the builder here | ||
builder.ConfigureServices(services => | ||
{ | ||
// nothing to change | ||
}); | ||
|
||
return base.CreateHost(builder); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Calebs.WebAPI; | ||
using Microsoft.VisualStudio.TestPlatform.TestHost; | ||
using System.Net; | ||
using System.Xml.Linq; | ||
|
||
|
||
namespace Calebs.WebAPI.Tests; | ||
|
||
public class WebAPITests | ||
{ | ||
private WebAPIApplication _server; | ||
private HttpClient _client; | ||
private const string _token = "fakeToken123xyz"; | ||
|
||
public WebAPITests() | ||
{ | ||
_server = new WebAPIApplication(); | ||
_client = _server.CreateClient(); | ||
} | ||
|
||
[Fact] | ||
public async Task Hello_Should_Return_HelloWorld() | ||
{ | ||
var response = await _client.GetAsync("/hello/"); | ||
|
||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
var value = await response.Content.ReadAsStringAsync(); | ||
value.Should().Be("hello world"); | ||
} | ||
[Theory] | ||
[InlineData ("Joe")] | ||
[InlineData ("Bill")] | ||
[InlineData ("Fred the great!")] | ||
[InlineData ("Elizabeth")] | ||
public async Task Hello_NAME_Should_return_name(string Name) | ||
{ | ||
var response = await _client.GetAsync($"/hello/{ Name }/"); | ||
|
||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
var value = await response.Content.ReadAsStringAsync(); | ||
value.Should().Be($"hello {Name}"); | ||
} | ||
|
||
[Fact] | ||
public async Task Secure_No_Token_Should_Deny() | ||
{ | ||
var response = await _client.GetAsync($"/secure"); | ||
response.StatusCode.Should().Be(HttpStatusCode.Forbidden); | ||
} | ||
|
||
[Fact] | ||
public async Task FakeToken_Should_return_token() | ||
{ | ||
var response = await _client.GetAsync($"/FakeToken"); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
var value = await response.Content.ReadAsStringAsync(); | ||
value.Should().Be(_token); | ||
} | ||
|
||
[Fact] | ||
public async Task Secure_with_Token_should_return_token() | ||
{ | ||
var newClient = _server.CreateClient(); | ||
newClient.DefaultRequestHeaders.Add("bearer", _token); | ||
var response = await newClient.GetAsync($"/Secure"); | ||
|
||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
var value = await response.Content.ReadAsStringAsync(); | ||
value.Should().Contain("Success!"); | ||
} | ||
|
||
|
||
} |