Skip to content

Commit

Permalink
Merge pull request #3 from calebjenkins/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
calebjenkins authored Nov 9, 2024
2 parents 5c34826 + b10b4ae commit 565f29c
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ Please submit all PRs to the `develop` branch
## Version History
- 0.1.0 - intial version. `.NET 7` Installs as a dotnet global tool or run locally as a console web app.
- 1.0.0 - official release. New features coming soon!
- 1.0.1 - added unit tests and version info on launch text
8 changes: 5 additions & 3 deletions src/WebAPI/Calebs.WebAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
<PackAsTool>true</PackAsTool>
<ToolCommandName>Calebs.WebAPI</ToolCommandName>
<PackageOutputPath>../../nupkg</PackageOutputPath>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<IsPackable>true</IsPackable>
<PackageProjectUrl>https://github.com/calebjenkins/calebs.webapi/</PackageProjectUrl>
<RepositoryUrl>https://github.com/calebjenkins/Calebs.WebAPI.git</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
Expand All @@ -33,8 +35,8 @@
</ItemGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath=""/>
<None Include="..\..\LICENSE" Pack="true" PackagePath=""/>
<None Include="..\..\README.md" Pack="true" PackagePath="" />
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />
</ItemGroup>

</Project>
13 changes: 12 additions & 1 deletion src/WebAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using FakeAuth; // Using FakeAuth so I can send a 403 without setting up all the needed auth infrastructure
using Calebs.Extensions.Console;
using Calebs.Extensions;

//namespace Calebs.WebAPI;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication().AddFakeAuth();
Expand All @@ -25,8 +28,12 @@
app.MapGet("/Secure", Check_Secure);

var url = app.Urls.FirstOrDefault();
var ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;


ConsoleColor.Red.WriteLine(" -==::Caleb's Web API::==-");
ConsoleColor.Blue.Write($"version: {ver}");
ConsoleColor.Blue.WriteLine(" --- more info at https://github.com/calebjenkins/calebs.webapi/ ");
Console.WriteLine("");
Console.WriteLine("Available Endpoints:");
Console.WriteLine($" - GET {url}/hello");
Expand All @@ -50,7 +57,7 @@ IResult Check_Secure(HttpContext context)
{
if (context.Request.Headers.Keys.Contains("bearer")) // && context.Request.Headers["bearer"] == TOKEN)
{
return Results.Ok(new { Results = "Suceess!" });
return Results.Ok(new { Results = "Success!" });
}

return Results.Forbid();
Expand All @@ -77,3 +84,7 @@ async Task<string> StreamToString(Stream stream)
var result = await reader.ReadToEndAsync();
return result;
}

public partial class Program
{ } // needed for test visability

14 changes: 0 additions & 14 deletions src/WebAPITests/UnitTest1.cs

This file was deleted.

30 changes: 30 additions & 0 deletions src/WebAPITests/WebAPIApplication.cs
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);
}
}
74 changes: 74 additions & 0 deletions src/WebAPITests/WebAPITests.cs
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!");
}


}

0 comments on commit 565f29c

Please sign in to comment.