From 7fa9bb3cdc0a605fcb1afd22a1766049e109d1b5 Mon Sep 17 00:00:00 2001 From: JT Date: Wed, 30 Nov 2022 11:32:54 +0800 Subject: [PATCH] Remove baseline dependency (#127) * Remove baseline dependency * Minor tweaks --- .../specs_against_aspnet_core_app.cs | 4 +- src/Alba.Testing/FormDataExtensionsTests.cs | 8 +- src/Alba.Testing/StringExtensionsTests.cs | 2 +- src/Alba/Alba.csproj | 3 +- src/Alba/AlbaHost.cs | 2 - src/Alba/AlbaWebApplicationFactory.cs | 4 +- .../HasSingleHeaderValueAssertion.cs | 3 +- src/Alba/Assertions/HeaderMatchAssertion.cs | 3 +- .../Assertions/HeaderMultiValueAssertion.cs | 5 +- src/Alba/Assertions/HeaderValueAssertion.cs | 3 +- src/Alba/CommaTokenParser.cs | 2 + src/Alba/DirectoryFinder.cs | 18 +- src/Alba/FormDataExtensions.cs | 28 +-- src/Alba/HttpContextExtensions.cs | 4 +- src/Alba/Internal/LightweightCache.cs | 218 ++++++++++++++++++ src/Alba/Internal/StreamExtensions.cs | 34 +++ src/Alba/Internal/StringExtensions.cs | 10 + src/Alba/MimeType.cs | 21 +- src/Alba/NoHeaderValueAssertion.cs | 3 +- src/Alba/Scenario.cs | 16 +- src/Alba/ScenarioAssertionException.cs | 8 +- src/Alba/ScenarioResult.cs | 2 +- .../Security/OpenConnectClientCredentials.cs | 3 +- src/Alba/Security/OpenConnectExtension.cs | 10 +- src/Alba/Security/OpenConnectUserPassword.cs | 2 +- src/Alba/SendExpression.cs | 14 +- src/Alba/Serialization/FormatterSerializer.cs | 2 +- .../Serialization/SystemTextJsonSerializer.cs | 2 +- src/Alba/StringExtensions.cs | 20 +- 29 files changed, 360 insertions(+), 94 deletions(-) create mode 100644 src/Alba/Internal/LightweightCache.cs create mode 100644 src/Alba/Internal/StreamExtensions.cs create mode 100644 src/Alba/Internal/StringExtensions.cs diff --git a/src/Alba.Testing/Acceptance/specs_against_aspnet_core_app.cs b/src/Alba.Testing/Acceptance/specs_against_aspnet_core_app.cs index 59e31295..96f3d65b 100644 --- a/src/Alba.Testing/Acceptance/specs_against_aspnet_core_app.cs +++ b/src/Alba.Testing/Acceptance/specs_against_aspnet_core_app.cs @@ -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?"); }); } diff --git a/src/Alba.Testing/FormDataExtensionsTests.cs b/src/Alba.Testing/FormDataExtensionsTests.cs index 49970127..70fdd6ca 100644 --- a/src/Alba.Testing/FormDataExtensionsTests.cs +++ b/src/Alba.Testing/FormDataExtensionsTests.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.IO; +using System.Net; using Baseline; using Microsoft.AspNetCore.Http; using Shouldly; @@ -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"); } diff --git a/src/Alba.Testing/StringExtensionsTests.cs b/src/Alba.Testing/StringExtensionsTests.cs index d9e1f2bf..74c5281e 100644 --- a/src/Alba.Testing/StringExtensionsTests.cs +++ b/src/Alba.Testing/StringExtensionsTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using Shouldly; using Xunit; diff --git a/src/Alba/Alba.csproj b/src/Alba/Alba.csproj index e6506ed5..503766ee 100644 --- a/src/Alba/Alba.csproj +++ b/src/Alba/Alba.csproj @@ -3,7 +3,7 @@ Supercharged integration testing for ASP.NET Core HTTP endpoints Alba - 7.1.0 + 7.2.0 net6.0;net7.0 Alba Alba @@ -21,7 +21,6 @@ - diff --git a/src/Alba/AlbaHost.cs b/src/Alba/AlbaHost.cs index 1fb53af4..bcd8a789 100644 --- a/src/Alba/AlbaHost.cs +++ b/src/Alba/AlbaHost.cs @@ -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; diff --git a/src/Alba/AlbaWebApplicationFactory.cs b/src/Alba/AlbaWebApplicationFactory.cs index 890a680f..39382f89 100644 --- a/src/Alba/AlbaWebApplicationFactory.cs +++ b/src/Alba/AlbaWebApplicationFactory.cs @@ -1,4 +1,4 @@ -#if NET6_0_OR_GREATER + using System; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; @@ -42,5 +42,3 @@ protected override IHost CreateHost(IHostBuilder builder) } } - -#endif \ No newline at end of file diff --git a/src/Alba/Assertions/HasSingleHeaderValueAssertion.cs b/src/Alba/Assertions/HasSingleHeaderValueAssertion.cs index 5465fef4..b029ed38 100644 --- a/src/Alba/Assertions/HasSingleHeaderValueAssertion.cs +++ b/src/Alba/Assertions/HasSingleHeaderValueAssertion.cs @@ -1,5 +1,4 @@ using System.Linq; -using Baseline; using Microsoft.AspNetCore.Http; namespace Alba.Assertions @@ -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; } diff --git a/src/Alba/Assertions/HeaderMatchAssertion.cs b/src/Alba/Assertions/HeaderMatchAssertion.cs index cfed0a1b..7a11b162 100644 --- a/src/Alba/Assertions/HeaderMatchAssertion.cs +++ b/src/Alba/Assertions/HeaderMatchAssertion.cs @@ -1,6 +1,5 @@ using System.Linq; using System.Text.RegularExpressions; -using Baseline; using Microsoft.AspNetCore.Http; namespace Alba.Assertions @@ -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; } diff --git a/src/Alba/Assertions/HeaderMultiValueAssertion.cs b/src/Alba/Assertions/HeaderMultiValueAssertion.cs index bc713260..7ec566cc 100644 --- a/src/Alba/Assertions/HeaderMultiValueAssertion.cs +++ b/src/Alba/Assertions/HeaderMultiValueAssertion.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Linq; -using Baseline; using Microsoft.AspNetCore.Http; namespace Alba.Assertions @@ -19,7 +18,7 @@ public HeaderMultiValueAssertion(string headerKey, IEnumerable 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) { @@ -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; diff --git a/src/Alba/Assertions/HeaderValueAssertion.cs b/src/Alba/Assertions/HeaderValueAssertion.cs index a9c54466..bddc35e3 100644 --- a/src/Alba/Assertions/HeaderValueAssertion.cs +++ b/src/Alba/Assertions/HeaderValueAssertion.cs @@ -1,5 +1,4 @@ using System.Linq; -using Baseline; using Microsoft.AspNetCore.Http; namespace Alba.Assertions @@ -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; } diff --git a/src/Alba/CommaTokenParser.cs b/src/Alba/CommaTokenParser.cs index bd2fd79c..806ec60a 100644 --- a/src/Alba/CommaTokenParser.cs +++ b/src/Alba/CommaTokenParser.cs @@ -1,7 +1,9 @@ +using System; using System.Collections.Generic; namespace Alba { + [Obsolete] internal class CommaTokenParser { private readonly List _tokens = new(); diff --git a/src/Alba/DirectoryFinder.cs b/src/Alba/DirectoryFinder.cs index 31ea9cd4..64e12fbe 100644 --- a/src/Alba/DirectoryFinder.cs +++ b/src/Alba/DirectoryFinder.cs @@ -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 { /// @@ -12,6 +14,7 @@ internal static class DirectoryFinder /// /// /// + [Obsolete] public static string? FindParallelFolder(string? folderName) { var starting = AppContext.BaseDirectory.ToFullPath(); @@ -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 { path }; + stringList.AddRange(parts); + return Combine(stringList.ToArray()); + } + public static string Combine(params string[] paths) => (paths).Aggregate(Path.Combine); } } \ No newline at end of file diff --git a/src/Alba/FormDataExtensions.cs b/src/Alba/FormDataExtensions.cs index 5c2a90ae..fc6d1fa1 100644 --- a/src/Alba/FormDataExtensions.cs +++ b/src/Alba/FormDataExtensions.cs @@ -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 @@ -20,31 +16,17 @@ public static class FormDataExtensions /// /// /// - public static void WriteFormData(this HttpContext context, - Dictionary values) + public static void WriteFormData(this HttpContext context, Dictionary 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 formData(Dictionary form) - { - foreach (var key in form.Keys) - { - yield return "{0}={1}".ToFormat(key, WebUtility.HtmlEncode(form[key])); - } - - } /// /// Writes the to the provided HttpContext, along with the diff --git a/src/Alba/HttpContextExtensions.cs b/src/Alba/HttpContextExtensions.cs index 8e19d2db..0bed0ac2 100644 --- a/src/Alba/HttpContextExtensions.cs +++ b/src/Alba/HttpContextExtensions.cs @@ -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 diff --git a/src/Alba/Internal/LightweightCache.cs b/src/Alba/Internal/LightweightCache.cs new file mode 100644 index 00000000..f072571a --- /dev/null +++ b/src/Alba/Internal/LightweightCache.cs @@ -0,0 +1,218 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Alba.Internal; + +public class LightweightCache : IEnumerable where TKey : notnull +{ + private readonly IDictionary _values; + + private Func _getKey = delegate { throw new NotImplementedException(); }; + + private Func _onMissing = delegate (TKey key) { + var message = $"Key '{key}' could not be found"; + throw new KeyNotFoundException(message); + }; + + public LightweightCache() + : this(new Dictionary()) + { + } + + public LightweightCache(Func onMissing) + : this(new Dictionary(), onMissing) + { + } + + public LightweightCache(IDictionary dictionary, Func onMissing) + : this(dictionary) + { + _onMissing = onMissing; + } + + public LightweightCache(IDictionary dictionary) + { + _values = dictionary; + } + + + public Func OnMissing + { + set => _onMissing = value; + } + + public Func GetKey + { + get => _getKey; + set => _getKey = value; + } + + public int Count => _values.Count; + + public TValue? First + { + get + { + foreach (var pair in _values) + { + return pair.Value; + } + + return default(TValue); + } + } + + public TValue this[TKey key] + { + get + { + if (!_values.TryGetValue(key, out TValue? value)) + { + value = _onMissing(key); + + if (value != null) + { + _values[key] = value; + } + } + + return value; + } + set + { + if (_values.ContainsKey(key)) + { + _values[key] = value; + } + else + { + _values.Add(key, value); + } + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)this).GetEnumerator(); + } + + public IEnumerator GetEnumerator() + { + return _values.Values.GetEnumerator(); + } + + /// + /// Guarantees that the Cache has the default value for a given key. + /// If it does not already exist, it's created. + /// + /// + public void FillDefault(TKey key) + { + Fill(key, _onMissing(key)); + } + + public void Fill(TKey key, TValue value) + { + if (_values.ContainsKey(key)) + { + return; + } + + _values.Add(key, value); + } + + public bool TryRetrieve(TKey key, [MaybeNullWhen(false)] out TValue value) + { + value = default; + + if (_values.ContainsKey(key)) + { + value = _values[key]; + return true; + } + + return false; + } + + public void Each(Action action) + { + foreach (var pair in _values) + { + action(pair.Value); + } + } + + public void Each(Action action) + { + foreach (var pair in _values) + { + action(pair.Key, pair.Value); + } + } + + public bool Has(TKey key) + { + return _values.ContainsKey(key); + } + + public bool Exists(Predicate predicate) + { + var returnValue = false; + + Each(delegate (TValue value) { returnValue |= predicate(value); }); + + return returnValue; + } + + public TValue? Find(Predicate predicate) + { + foreach (var pair in _values) + { + if (predicate(pair.Value)) + { + return pair.Value; + } + } + + return default; + } + + public TValue[] GetAll() + { + var returnValue = new TValue[Count]; + _values.Values.CopyTo(returnValue, 0); + + return returnValue; + } + + public void Remove(TKey key) + { + if (_values.ContainsKey(key)) + { + _values.Remove(key); + } + } + + public void Clear() + { + _values.Clear(); + } + + public void WithValue(TKey key, Action action) + { + if (_values.ContainsKey(key)) + { + action(this[key]); + } + } + + public void ClearAll() + { + _values.Clear(); + } +} \ No newline at end of file diff --git a/src/Alba/Internal/StreamExtensions.cs b/src/Alba/Internal/StreamExtensions.cs new file mode 100644 index 00000000..22b118ff --- /dev/null +++ b/src/Alba/Internal/StreamExtensions.cs @@ -0,0 +1,34 @@ +using System.IO; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +namespace Alba.Internal; + +internal static class StreamExtensions +{ + public static string ReadAllText(this Stream stream) + { + using var sr = new StreamReader(stream, leaveOpen: true); + return sr.ReadToEnd(); + } + + public static byte[] ReadAllBytes(this Stream stream) + { + using var content = new MemoryStream(); + stream.CopyTo(content); + return content.ToArray(); + } + + public static Task ReadAllTextAsync(this Stream stream) + { + using var sr = new StreamReader(stream, leaveOpen: true); + return sr.ReadToEndAsync(); + } + + public static async Task ReadAllBytesAsync(this Stream stream) + { + using var content = new MemoryStream(); + await stream.CopyToAsync(content); + return content.ToArray(); + } +} \ No newline at end of file diff --git a/src/Alba/Internal/StringExtensions.cs b/src/Alba/Internal/StringExtensions.cs new file mode 100644 index 00000000..371f3f87 --- /dev/null +++ b/src/Alba/Internal/StringExtensions.cs @@ -0,0 +1,10 @@ +using System.Diagnostics.CodeAnalysis; + +namespace Alba.Internal; + +internal static class StringExtensions +{ + public static bool IsEmpty([NotNullWhen(false)] this string? stringValue) => string.IsNullOrEmpty(stringValue); + + public static bool IsNotEmpty([NotNullWhen(true)] this string? stringValue) => !string.IsNullOrEmpty(stringValue); +} \ No newline at end of file diff --git a/src/Alba/MimeType.cs b/src/Alba/MimeType.cs index 31c1ac10..9c696bd6 100644 --- a/src/Alba/MimeType.cs +++ b/src/Alba/MimeType.cs @@ -2,8 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using Baseline; - +using Alba.Internal; + namespace Alba { /// @@ -43,7 +43,7 @@ public class MimeType public static readonly MimeType EmbeddedOpenType = New("application/vnd.ms-fontobject", ".eot"); public static readonly MimeType Svg = New("image/svg+xml", ".svg"); - private readonly IList _extensions = new List(); + private readonly HashSet _extensions = new(); private readonly string _mimeType; private MimeType(string mimeType) @@ -56,7 +56,10 @@ private MimeType(string mimeType) public static MimeType New(string mimeTypeValue, params string[] extensions) { var mimeType = new MimeType(mimeTypeValue); - extensions.Each(mimeType.AddExtension); + foreach (var extension in extensions) + { + mimeType.AddExtension(extension); + } _mimeTypes[mimeTypeValue] = mimeType; return mimeType; @@ -64,7 +67,7 @@ public static MimeType New(string mimeTypeValue, params string[] extensions) public void AddExtension(string extension) { - _extensions.Fill(extension); + _extensions.Add(extension); } public override string ToString() @@ -105,7 +108,11 @@ public bool HasExtension(string extension) static MimeType() { - _fileExtensions.Each(pair => _mimeTypes[pair.Value].AddExtension(pair.Key)); + foreach (var pair in _fileExtensions) + { + _mimeTypes[pair.Value].AddExtension(pair.Key); + } + _mappingFromExtension = new LightweightCache(extension => { return _mimeTypes.GetAll().FirstOrDefault(x => x.HasExtension(extension)); @@ -113,7 +120,7 @@ static MimeType() } private static readonly Dictionary _fileExtensions = - new Dictionary(StringComparer.OrdinalIgnoreCase) + new(StringComparer.OrdinalIgnoreCase) { {".323", "text/h323"}, {".3g2", "video/3gpp2"}, diff --git a/src/Alba/NoHeaderValueAssertion.cs b/src/Alba/NoHeaderValueAssertion.cs index 863f0885..df8c8277 100644 --- a/src/Alba/NoHeaderValueAssertion.cs +++ b/src/Alba/NoHeaderValueAssertion.cs @@ -1,5 +1,4 @@ using System.Linq; -using Baseline; using Microsoft.AspNetCore.Http; namespace Alba @@ -19,7 +18,7 @@ public void Assert(Scenario scenario, HttpContext context, ScenarioAssertionExce if (headers.ContainsKey(_headerKey)) { var values = headers[_headerKey]; - var valueText = values.Select(x => "'" + x + "'").Join(", "); + var valueText = values.Select(x => "'" + x + "'").Aggregate((s1, s2) => $"{s1}, {s2}"); ex.Add($"Expected no value for header '{_headerKey}', but found values {valueText}"); } } diff --git a/src/Alba/Scenario.cs b/src/Alba/Scenario.cs index 69f546ae..9bf727dc 100644 --- a/src/Alba/Scenario.cs +++ b/src/Alba/Scenario.cs @@ -5,13 +5,9 @@ using System.Net; using System.Net.Http; using System.Security.Claims; -using System.Text.Json; using Alba.Assertions; -using Baseline; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; -using Microsoft.AspNetCore.Mvc.Formatters; -using Microsoft.Extensions.DependencyInjection; namespace Alba { @@ -152,19 +148,23 @@ SendExpression IUrlExpression.FormData(T target) { var values = new Dictionary(); - typeof(T).GetProperties().Where(x => x.CanWrite && x.CanRead).Each(prop => + var properties = typeof(T).GetProperties().Where(x => x.CanWrite && x.CanRead); + + foreach (var prop in properties) { var rawValue = prop.GetValue(target, null); values.Add(prop.Name, rawValue?.ToString() ?? string.Empty); - }); + } - typeof(T).GetFields().Each(field => + var fields = typeof(T).GetFields(); + + foreach (var field in fields) { var rawValue = field.GetValue(target); values.Add(field.Name, rawValue?.ToString() ?? string.Empty); - }); + } Body.WriteFormData(values); diff --git a/src/Alba/ScenarioAssertionException.cs b/src/Alba/ScenarioAssertionException.cs index f47ebebe..a90d3ae7 100644 --- a/src/Alba/ScenarioAssertionException.cs +++ b/src/Alba/ScenarioAssertionException.cs @@ -3,7 +3,7 @@ using System.IO; using System.Linq; using System.Text; -using Baseline; +using Alba.Internal; using Microsoft.AspNetCore.Http; @@ -45,7 +45,11 @@ public override string Message get { var writer = new StringWriter(); - _messages.Each(x => writer.WriteLine((string) x)); + + foreach (var message in _messages) + { + writer.WriteLine(message); + } if (Body.IsNotEmpty()) { diff --git a/src/Alba/ScenarioResult.cs b/src/Alba/ScenarioResult.cs index cf7c7b26..9161f6bd 100644 --- a/src/Alba/ScenarioResult.cs +++ b/src/Alba/ScenarioResult.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; -using Baseline; +using Alba.Internal; using Microsoft.AspNetCore.Http; namespace Alba diff --git a/src/Alba/Security/OpenConnectClientCredentials.cs b/src/Alba/Security/OpenConnectClientCredentials.cs index 69e618e3..97fe85e9 100644 --- a/src/Alba/Security/OpenConnectClientCredentials.cs +++ b/src/Alba/Security/OpenConnectClientCredentials.cs @@ -1,9 +1,8 @@ using System; using System.Net.Http; using System.Threading.Tasks; -using Baseline; +using Alba.Internal; using IdentityModel.Client; -using Microsoft.AspNetCore.Http; namespace Alba.Security { diff --git a/src/Alba/Security/OpenConnectExtension.cs b/src/Alba/Security/OpenConnectExtension.cs index 22ac5c54..d2812ce8 100644 --- a/src/Alba/Security/OpenConnectExtension.cs +++ b/src/Alba/Security/OpenConnectExtension.cs @@ -15,15 +15,11 @@ namespace Alba.Security public abstract class OpenConnectExtension : IAlbaExtension { internal static readonly string OverrideKey = "alba_oidc_override"; - - private HttpClient _client; + + private HttpClient _client = null!; private DiscoveryDocumentResponse? _disco; private TokenResponse? _cached; - - public OpenConnectExtension() - { - // _client = new HttpClient(); - } + void IDisposable.Dispose() { diff --git a/src/Alba/Security/OpenConnectUserPassword.cs b/src/Alba/Security/OpenConnectUserPassword.cs index 8b64d398..5c087d23 100644 --- a/src/Alba/Security/OpenConnectUserPassword.cs +++ b/src/Alba/Security/OpenConnectUserPassword.cs @@ -1,7 +1,7 @@ using System; using System.Net.Http; using System.Threading.Tasks; -using Baseline; +using Alba.Internal; using IdentityModel.Client; namespace Alba.Security diff --git a/src/Alba/SendExpression.cs b/src/Alba/SendExpression.cs index 11fcb152..01e4d9b0 100644 --- a/src/Alba/SendExpression.cs +++ b/src/Alba/SendExpression.cs @@ -1,9 +1,7 @@ using System; using System.Linq; -using Baseline; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.WebUtilities; - namespace Alba { @@ -92,18 +90,20 @@ public SendExpression QueryString(string paramName, string paramValue) /// public SendExpression QueryString(T target) { - typeof(T).GetProperties().Where(x => x.CanRead).Each(prop => + var properties = typeof(T).GetProperties().Where(x => x.CanRead); + + foreach (var prop in properties) { var rawValue = prop.GetValue(target, null); QueryString(prop.Name, rawValue?.ToString() ?? string.Empty); - }); + } + var fields = typeof(T).GetFields(); - typeof(T).GetFields().Each(field => + foreach (var field in fields) { var rawValue = field.GetValue(target); QueryString(field.Name, rawValue?.ToString() ?? string.Empty); - }); - + } return this; } diff --git a/src/Alba/Serialization/FormatterSerializer.cs b/src/Alba/Serialization/FormatterSerializer.cs index ada1af53..3dd019bf 100644 --- a/src/Alba/Serialization/FormatterSerializer.cs +++ b/src/Alba/Serialization/FormatterSerializer.cs @@ -1,7 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; -using Baseline; +using Alba.Internal; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.AspNetCore.Mvc.ModelBinding; diff --git a/src/Alba/Serialization/SystemTextJsonSerializer.cs b/src/Alba/Serialization/SystemTextJsonSerializer.cs index 607f7fe5..fb7719e2 100644 --- a/src/Alba/Serialization/SystemTextJsonSerializer.cs +++ b/src/Alba/Serialization/SystemTextJsonSerializer.cs @@ -1,7 +1,7 @@ using System.IO; using System.Text.Json; using System.Threading.Tasks; -using Baseline; +using Alba.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; diff --git a/src/Alba/StringExtensions.cs b/src/Alba/StringExtensions.cs index c124a267..330f544b 100644 --- a/src/Alba/StringExtensions.cs +++ b/src/Alba/StringExtensions.cs @@ -2,8 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.Net; -using Baseline; - + namespace Alba { public static class StringExtensions @@ -14,6 +13,7 @@ public static class StringExtensions /// /// /// + [Obsolete("Use HeaderDictionary.GetCommaSeparatedHeaderValues instead")] public static IEnumerable GetCommaSeparatedHeaderValues(this IEnumerable enumerable) { foreach (var content in enumerable) @@ -22,7 +22,12 @@ public static IEnumerable GetCommaSeparatedHeaderValues(this IEnumerable if (searchString.Length == 0) break; var parser = new CommaTokenParser(); - content.ToCharArray().Each(parser.Read); + var array = content.ToCharArray(); + + foreach (var c in array) + { + parser.Read(c); + } // Gotta force the parser to know it's done parser.Read(','); @@ -36,23 +41,24 @@ public static IEnumerable GetCommaSeparatedHeaderValues(this IEnumerable } + [Obsolete("Use WebUtility.UrlEncode directly")] public static string UrlEncoded(this string value) { return WebUtility.UrlEncode(value); } + [Obsolete("Copy this extension into your own codebase if you wish to continue using it.")] public static string Quoted(this string value) { return $"\"{value}\""; } + [Obsolete("Copy this extension into your own codebase if you wish to continue using it.")] public static DateTime? TryParseHttpDate(this string dateString) { - DateTime date; - - return DateTime.TryParseExact(dateString, "r", CultureInfo.InvariantCulture, DateTimeStyles.None, out date) + return DateTime.TryParseExact(dateString, "r", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) ? date - : null as DateTime?; + : null; } }