Skip to content

Commit

Permalink
Move to file-scoped namespaces (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawxy authored Jul 24, 2024
1 parent d0c313b commit 846282a
Show file tree
Hide file tree
Showing 47 changed files with 2,329 additions and 2,455 deletions.
647 changes: 322 additions & 325 deletions src/Alba/AlbaHost.cs

Large diffs are not rendered by default.

201 changes: 100 additions & 101 deletions src/Alba/AlbaHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,123 +5,122 @@
using Microsoft.Extensions.Hosting;


namespace Alba
namespace Alba;

public static class AlbaHostExtensions
{
public static class AlbaHostExtensions
{

/// <summary>
/// Start an AlbaHost for a configured WebApplicationBuilder and WebApplication
/// </summary>
/// <param name="builder"></param>
/// <param name="configureRoutes">Configure the WebApplication for routing and/or middleware</param>
/// <param name="extensions"></param>
/// <returns></returns>
public static Task<IAlbaHost> StartAlbaAsync(this WebApplicationBuilder builder,
Action<WebApplication> configureRoutes,
params IAlbaExtension[] extensions)
{
return AlbaHost.For(builder, configureRoutes, extensions);
}
/// <summary>
/// Start an AlbaHost for a configured WebApplicationBuilder and WebApplication
/// </summary>
/// <param name="builder"></param>
/// <param name="configureRoutes">Configure the WebApplication for routing and/or middleware</param>
/// <param name="extensions"></param>
/// <returns></returns>
public static Task<IAlbaHost> StartAlbaAsync(this WebApplicationBuilder builder,
Action<WebApplication> configureRoutes,
params IAlbaExtension[] extensions)
{
return AlbaHost.For(builder, configureRoutes, extensions);
}


/// <summary>
/// Start an AlbaHost for the supplied IHostBuilder
/// </summary>
/// <param name="builder"></param>
/// <param name="extensions"></param>
/// <returns></returns>
public static Task<IAlbaHost> StartAlbaAsync(this IHostBuilder builder, params IAlbaExtension[] extensions)
{
return AlbaHost.For(builder, extensions);
}
/// <summary>
/// Start an AlbaHost for the supplied IHostBuilder
/// </summary>
/// <param name="builder"></param>
/// <param name="extensions"></param>
/// <returns></returns>
public static Task<IAlbaHost> StartAlbaAsync(this IHostBuilder builder, params IAlbaExtension[] extensions)
{
return AlbaHost.For(builder, extensions);
}

public static IAlbaHost StartAlba(this IHostBuilder builder, params IAlbaExtension[] extensions)
{
return new AlbaHost(builder, extensions);
}
public static IAlbaHost StartAlba(this IHostBuilder builder, params IAlbaExtension[] extensions)
{
return new AlbaHost(builder, extensions);
}

/// <summary>
/// Shortcut to issue a POST with a Json serialized request body and a Json serialized
/// response body
/// </summary>
/// <param name="system"></param>
/// <param name="request"></param>
/// <param name="url"></param>
/// <param name="jsonStyle"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static ResponseExpression PostJson<T>(this IAlbaHost system, T request, [StringSyntax(StringSyntaxAttribute.Uri)]string url, JsonStyle? jsonStyle = null) where T : class
/// <summary>
/// Shortcut to issue a POST with a Json serialized request body and a Json serialized
/// response body
/// </summary>
/// <param name="system"></param>
/// <param name="request"></param>
/// <param name="url"></param>
/// <param name="jsonStyle"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static ResponseExpression PostJson<T>(this IAlbaHost system, T request, [StringSyntax(StringSyntaxAttribute.Uri)]string url, JsonStyle? jsonStyle = null) where T : class
{
return new(system, s =>
{
return new(system, s =>
{
s.WriteJson(request, jsonStyle);
s.Post.Json(request, jsonStyle).ToUrl(url);
});
}
s.WriteJson(request, jsonStyle);
s.Post.Json(request, jsonStyle).ToUrl(url);
});
}

/// <summary>
/// Shortcut to issue a PUT with a Json serialized request body and a Json serialized
/// response body
/// </summary>
/// <param name="system"></param>
/// <param name="request"></param>
/// <param name="url"></param>
/// <param name="jsonStyle"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static ResponseExpression PutJson<T>(this IAlbaHost system, T request, [StringSyntax(StringSyntaxAttribute.Uri)]string url, JsonStyle? jsonStyle = null) where T : class
{
return new(system, s => { s.Put.Json(request, jsonStyle).ToUrl(url); });
}
/// <summary>
/// Shortcut to issue a PUT with a Json serialized request body and a Json serialized
/// response body
/// </summary>
/// <param name="system"></param>
/// <param name="request"></param>
/// <param name="url"></param>
/// <param name="jsonStyle"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static ResponseExpression PutJson<T>(this IAlbaHost system, T request, [StringSyntax(StringSyntaxAttribute.Uri)]string url, JsonStyle? jsonStyle = null) where T : class
{
return new(system, s => { s.Put.Json(request, jsonStyle).ToUrl(url); });
}

/// <summary>
/// Shortcut to just retrieve the contents of an HTTP GET as JSON and deserialize the resulting
/// response to the type "T"
/// </summary>
/// <param name="system"></param>
/// <param name="url"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async Task<T?> GetAsJson<T>(this IAlbaHost system, [StringSyntax(StringSyntaxAttribute.Uri)]string url)
{
var response = await system.Scenario(x => x.Get.Url(url).Accepts("application/json;text/json"));
return await response.ReadAsJsonAsync<T>();
}
/// <summary>
/// Shortcut to just retrieve the contents of an HTTP GET as JSON and deserialize the resulting
/// response to the type "T"
/// </summary>
/// <param name="system"></param>
/// <param name="url"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async Task<T?> GetAsJson<T>(this IAlbaHost system, [StringSyntax(StringSyntaxAttribute.Uri)]string url)
{
var response = await system.Scenario(x => x.Get.Url(url).Accepts("application/json;text/json"));
return await response.ReadAsJsonAsync<T>();
}

/// <summary>
/// Issue a GET to the supplied url and read the response text as a string
/// </summary>
/// <param name="host"></param>
/// <param name="url"></param>
/// <returns></returns>
public static async Task<string> GetAsText(this IAlbaHost host, [StringSyntax(StringSyntaxAttribute.Uri)]string url)
/// <summary>
/// Issue a GET to the supplied url and read the response text as a string
/// </summary>
/// <param name="host"></param>
/// <param name="url"></param>
/// <returns></returns>
public static async Task<string> GetAsText(this IAlbaHost host, [StringSyntax(StringSyntaxAttribute.Uri)]string url)
{
var response = await host.Scenario(x =>
{
var response = await host.Scenario(x =>
{
x.Get.Url(url);
});
x.Get.Url(url);
});

return await response.ReadAsTextAsync();
}
return await response.ReadAsTextAsync();
}

public class ResponseExpression
{
private readonly Action<Scenario> _configure;
private readonly IAlbaHost _system;
public class ResponseExpression
{
private readonly Action<Scenario> _configure;
private readonly IAlbaHost _system;


public ResponseExpression(IAlbaHost system, Action<Scenario> configure)
{
_system = system;
_configure = configure;
}
public ResponseExpression(IAlbaHost system, Action<Scenario> configure)
{
_system = system;
_configure = configure;
}

public async Task<TResponse?> Receive<TResponse>()
{
var response = await _system.Scenario(_configure);
return await response.ReadAsJsonAsync<TResponse>();
}
public async Task<TResponse?> Receive<TResponse>()
{
var response = await _system.Scenario(_configure);
return await response.ReadAsJsonAsync<TResponse>();
}
}
}
9 changes: 4 additions & 5 deletions src/Alba/AlbaJsonFormatterException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;

namespace Alba
namespace Alba;

public class AlbaJsonFormatterException : Exception
{
public class AlbaJsonFormatterException : Exception
public AlbaJsonFormatterException(string json) : base($"The JSON formatter was unable to process the raw JSON:\n{json}")
{
public AlbaJsonFormatterException(string json) : base($"The JSON formatter was unable to process the raw JSON:\n{json}")
{
}
}
}
51 changes: 25 additions & 26 deletions src/Alba/AlbaWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,39 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Alba
namespace Alba;

/// <inheritdoc cref="WebApplicationFactory{TEntryPoint}"/>
public class AlbaWebApplicationFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint>, IAlbaWebApplicationFactory where TEntryPoint : class
{
/// <inheritdoc cref="WebApplicationFactory{TEntryPoint}"/>
public class AlbaWebApplicationFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint>, IAlbaWebApplicationFactory where TEntryPoint : class
private readonly Action<IWebHostBuilder> _configuration;
private readonly IAlbaExtension[] _extensions;
public AlbaWebApplicationFactory(Action<IWebHostBuilder> configuration, IAlbaExtension[] extensions)
{
private readonly Action<IWebHostBuilder> _configuration;
private readonly IAlbaExtension[] _extensions;
public AlbaWebApplicationFactory(Action<IWebHostBuilder> configuration, IAlbaExtension[] extensions)
{
_configuration = configuration;
_extensions = extensions;
}
_configuration = configuration;
_extensions = extensions;
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
builder.ConfigureServices(services =>
{
services.AddHttpContextAccessor();
});
services.AddHttpContextAccessor();
});

_configuration(builder);
_configuration(builder);

base.ConfigureWebHost(builder);
}
base.ConfigureWebHost(builder);
}

protected override IHost CreateHost(IHostBuilder builder)
protected override IHost CreateHost(IHostBuilder builder)
{
foreach (var extension in _extensions)
{
foreach (var extension in _extensions)
{
extension.Configure(builder);
}

return base.CreateHost(builder);
extension.Configure(builder);
}

return base.CreateHost(builder);
}
}

}
35 changes: 17 additions & 18 deletions src/Alba/Assertions/BodyContainsAssertion.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
using Microsoft.AspNetCore.Http;

namespace Alba.Assertions
namespace Alba.Assertions;

#region sample_BodyContainsAssertion
internal sealed class BodyContainsAssertion : IScenarioAssertion
{
#region sample_BodyContainsAssertion
internal class BodyContainsAssertion : IScenarioAssertion
{
public string Text { get; set; }
public string Text { get; set; }

public BodyContainsAssertion(string text)
{
Text = text;
}
public BodyContainsAssertion(string text)
{
Text = text;
}

public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionException ex)
public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionException ex)
{
var body = ex.ReadBody(context);
if (!body.Contains(Text))
{
var body = ex.ReadBody(context);
if (!body.Contains(Text))
{
// Add the failure message to the exception. This exception only
// gets thrown if there are failures.
ex.Add($"Expected text '{Text}' was not found in the response body");
}
// Add the failure message to the exception. This exception only
// gets thrown if there are failures.
ex.Add($"Expected text '{Text}' was not found in the response body");
}
}
#endregion
}
#endregion
27 changes: 13 additions & 14 deletions src/Alba/Assertions/BodyDoesNotContainAssertion.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using Microsoft.AspNetCore.Http;

namespace Alba.Assertions
namespace Alba.Assertions;

internal sealed class BodyDoesNotContainAssertion : IScenarioAssertion
{
internal class BodyDoesNotContainAssertion : IScenarioAssertion
{
public string Text { get; set; }
public string Text { get; set; }

public BodyDoesNotContainAssertion(string text)
{
Text = text;
}
public BodyDoesNotContainAssertion(string text)
{
Text = text;
}

public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionException ex)
public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionException ex)
{
var body = ex.ReadBody(context);
if (body.Contains(Text))
{
var body = ex.ReadBody(context);
if (body.Contains(Text))
{
ex.Add($"Text '{Text}' should not be found in the response body");
}
ex.Add($"Text '{Text}' should not be found in the response body");
}
}
}
Loading

0 comments on commit 846282a

Please sign in to comment.