Skip to content

Commit

Permalink
Remove baseline dependency (#127)
Browse files Browse the repository at this point in the history
* Remove baseline dependency

* Minor tweaks
  • Loading branch information
Hawxy authored Nov 30, 2022
1 parent 553b6e1 commit 7fa9bb3
Show file tree
Hide file tree
Showing 29 changed files with 360 additions and 94 deletions.
4 changes: 2 additions & 2 deletions src/Alba.Testing/Acceptance/specs_against_aspnet_core_app.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ public Task send_body_to_aspnetcore_with_FromBody()
return run(_ =>
{
_.Post.Url("/sendbody");
_.Body.TextIs("some stuff");
_.Body.TextIs("some stuff?");

_.ContentShouldContain("some stuff");
_.ContentShouldContain("some stuff?");
});
}

Expand Down
8 changes: 7 additions & 1 deletion src/Alba.Testing/FormDataExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Net;
using Baseline;
using Microsoft.AspNetCore.Http;
using Shouldly;
Expand All @@ -20,11 +22,15 @@ public void round_trip_writing_and_parsing()
};

var context = new DefaultHttpContext();
using var stream = new MemoryStream();
context.Request.Body = stream;

context.WriteFormData(form1);

context.Request.Body.Position = 0;

context.Request.Body.ReadAllText()
.ShouldBe("a=what?&b=now?&c=really?");
.ShouldBe("a=what%3F&b=now%3F&c=really%3F");

}

Expand Down
2 changes: 1 addition & 1 deletion src/Alba.Testing/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Shouldly;
using Xunit;

Expand Down
3 changes: 1 addition & 2 deletions src/Alba/Alba.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Supercharged integration testing for ASP.NET Core HTTP endpoints</Description>
<AssemblyTitle>Alba</AssemblyTitle>
<Version>7.1.0</Version>
<Version>7.2.0</Version>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<AssemblyName>Alba</AssemblyName>
<PackageId>Alba</PackageId>
Expand All @@ -21,7 +21,6 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Baseline" Version="4.1.0" />
<PackageReference Include="IdentityModel" Version="6.0.0" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
2 changes: 0 additions & 2 deletions src/Alba/AlbaHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using Alba;
using Alba.Serialization;
using Baseline;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
Expand Down
4 changes: 1 addition & 3 deletions src/Alba/AlbaWebApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if NET6_0_OR_GREATER

using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
Expand Down Expand Up @@ -42,5 +42,3 @@ protected override IHost CreateHost(IHostBuilder builder)

}
}

#endif
3 changes: 1 addition & 2 deletions src/Alba/Assertions/HasSingleHeaderValueAssertion.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using Baseline;
using Microsoft.AspNetCore.Http;

namespace Alba.Assertions
Expand Down Expand Up @@ -28,7 +27,7 @@ public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionExce
break;

default:
var valueText = values.Select(x => "'" + x + "'").Join(", ");
var valueText = values.Select(x => "'" + x + "'").Aggregate((s1, s2) => $"{s1}, {s2}");
ex.Add($"Expected a single header value of '{_headerKey}', but found multiple values on the response: {valueText}");
break;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Alba/Assertions/HeaderMatchAssertion.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq;
using System.Text.RegularExpressions;
using Baseline;
using Microsoft.AspNetCore.Http;

namespace Alba.Assertions
Expand Down Expand Up @@ -35,7 +34,7 @@ public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionExce
break;

default:
var valueText = values.Select(x => "'" + x + "'").Join(", ");
var valueText = values.Select(x => "'" + x + "'").Aggregate((s1, s2) => $"{s1}, {s2}");
ex.Add($"Expected a single header value of '{_headerKey}' matching '{_regex}', but the actual values were {valueText}");
break;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Alba/Assertions/HeaderMultiValueAssertion.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Baseline;
using Microsoft.AspNetCore.Http;

namespace Alba.Assertions
Expand All @@ -19,7 +18,7 @@ public HeaderMultiValueAssertion(string headerKey, IEnumerable<string> expected)
public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionException ex)
{
var values = context.Response.Headers[_headerKey];
var expectedText = _expected.Select(x => "'" + x + "'").Join(", ");
var expectedText = _expected.Select(x => "'" + x + "'").Aggregate((s1, s2) => $"{s1}, {s2}");

switch (values.Count)
{
Expand All @@ -30,7 +29,7 @@ public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionExce
default:
if (!_expected.All(x => values.Contains(x)))
{
var valueText = values.Select(x => "'" + x + "'").Join(", ");
var valueText = values.Select(x => "'" + x + "'").Aggregate((s1, s2) => $"{s1}, {s2}");
ex.Add($"Expected header values of '{_headerKey}'={expectedText}, but the actual values were {valueText}.");
}
break;
Expand Down
3 changes: 1 addition & 2 deletions src/Alba/Assertions/HeaderValueAssertion.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using Baseline;
using Microsoft.AspNetCore.Http;

namespace Alba.Assertions
Expand Down Expand Up @@ -34,7 +33,7 @@ public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionExce
break;

default:
var valueText = values.Select(x => "'" + x + "'").Join(", ");
var valueText = values.Select(x => "'" + x + "'").Aggregate((s1, s2) => $"{s1}, {s2}");
ex.Add($"Expected a single header value of '{_headerKey}'='{_expected}', but the actual values were {valueText}");
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Alba/CommaTokenParser.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;

namespace Alba
{
[Obsolete]
internal class CommaTokenParser
{
private readonly List<string> _tokens = new();
Expand Down
18 changes: 16 additions & 2 deletions src/Alba/DirectoryFinder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using Baseline;
using System.Linq;

namespace Alba
{
// Delete this all in next semver
internal static class DirectoryFinder
{
/// <summary>
Expand All @@ -12,6 +14,7 @@ internal static class DirectoryFinder
/// </summary>
/// <param name="folderName"></param>
/// <returns></returns>
[Obsolete]
public static string? FindParallelFolder(string? folderName)
{
var starting = AppContext.BaseDirectory.ToFullPath();
Expand All @@ -38,5 +41,16 @@ internal static class DirectoryFinder

return Directory.Exists(candidate) ? candidate : null;
}
}

public static string ToFullPath(this string path) => Path.GetFullPath(path);

public static string? ParentDirectory(this string path) => Path.GetDirectoryName(path.TrimEnd(Path.DirectorySeparatorChar));

public static string AppendPath(this string path, params string[] parts)
{
var stringList = new List<string> { path };
stringList.AddRange(parts);
return Combine(stringList.ToArray());
}
public static string Combine(params string[] paths) => (paths).Aggregate(Path.Combine); }
}
28 changes: 5 additions & 23 deletions src/Alba/FormDataExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using Baseline;
using Microsoft.AspNetCore.Http;

namespace Alba
Expand All @@ -20,31 +16,17 @@ public static class FormDataExtensions
/// </summary>
/// <param name="context"></param>
/// <param name="values"></param>
public static void WriteFormData(this HttpContext context,
Dictionary<string, string> values)
public static void WriteFormData(this HttpContext context, Dictionary<string, string> values)
{
var post = formData(values).Join("&");
using var form = new FormUrlEncodedContent(values);

context.Request.ContentLength = post.Length;
context.Request.ContentType = MimeType.HttpFormMimetype;
form.CopyTo(context.Request.Body, null, CancellationToken.None);

var postBytes = Encoding.UTF8.GetBytes(post);
context.Request.Headers.ContentType = form.Headers.ContentType!.ToString();
context.Request.Headers.ContentLength = form.Headers.ContentLength;

var stream = new MemoryStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Position = 0;

context.Request.Body = stream;
}

private static IEnumerable<string> formData(Dictionary<string, string> form)
{
foreach (var key in form.Keys)
{
yield return "{0}={1}".ToFormat(key, WebUtility.HtmlEncode(form[key]));
}

}

/// <summary>
/// Writes the <see cref="MultipartFormDataContent"/> to the provided HttpContext, along with the
Expand Down
4 changes: 1 addition & 3 deletions src/Alba/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Security.Claims;
using System.Text;
using Baseline;
using Alba.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features.Authentication;
using Microsoft.AspNetCore.WebUtilities;

namespace Alba
Expand Down
Loading

0 comments on commit 7fa9bb3

Please sign in to comment.