Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop into master #97

Merged
merged 36 commits into from
Jun 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
651326a
If the method is abstract and virtual is a correct method (#72)
Sergio1192 Aug 2, 2022
f65be9c
Fix enumerable parameter (#74)
Sergio1192 Sep 13, 2022
f817b67
Add RemoveQueryParameter extension method (#75)
Sergio1192 Sep 15, 2022
c2f870d
CancellationTOken always is a query paramater (#76)
Sergio1192 Sep 21, 2022
706ab47
Version 3.4.0 (#77)
Sergio1192 Sep 21, 2022
387b500
Action convection name (#79)
Sergio1192 Oct 4, 2022
d169c53
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 Feb 22, 2023
994cb8f
Create get request when it has an object with some list
Sergio1192 Feb 22, 2023
29ab302
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 Feb 22, 2023
99f0c8a
DateTime as a primitive type but using custom formatter (#81)
Sergio1192 Mar 1, 2023
060a58c
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 Mar 1, 2023
ed138ab
Fixing codeQL alerts (#82)
Sergio1192 Mar 12, 2023
a85bb25
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 Mar 12, 2023
de91f82
feature: Get url (#83)
Sergio1192 Mar 19, 2023
35fa8de
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 Mar 20, 2023
15ea0b2
In IncludeContentAsFormUrlEncoded and PrimitiveParameterActionTokeniz…
Sergio1192 May 20, 2023
2563705
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 May 20, 2023
e204431
Add new CreateHttpApiRequest with TActionResponse (#85)
Sergio1192 May 20, 2023
026d100
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 May 20, 2023
92af8db
Add net7.0 Target Version (#86)
Sergio1192 May 20, 2023
ec294cc
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 May 20, 2023
85a2b9c
ReadContentAsAsync allows string type (#87)
Sergio1192 May 21, 2023
e6d41cc
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 May 21, 2023
e18e22f
Allow send a IFormFile (#88)
Sergio1192 May 22, 2023
d035170
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 May 22, 2023
d5aa032
Update version to 3.5.0 (#89)
Sergio1192 May 22, 2023
5b4e075
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 May 22, 2023
2018abc
Add DateTimeOffset to IsDateTime function (#91)
Sergio1192 May 22, 2023
fb8e0f3
Merge branch 'develop' of https://github.com/Xabaril/Acheve.TestHost …
Sergio1192 May 22, 2023
0f613c2
Allow dispatch workflows manually
Sergio1192 May 25, 2023
316d22c
Update to Net8 (#92)
Sergio1192 May 3, 2024
afb09ea
Different froms in object (#93)
Sergio1192 May 13, 2024
97f4d69
Version 4.0.0 (#94)
Sergio1192 May 13, 2024
a4fc44a
Merge with master
Sergio1192 May 14, 2024
d2fef41
Fix IsDateTime (#96)
Sergio1192 May 14, 2024
df4b701
Send ContentType and ContentDisposition (#98)
Sergio1192 Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 46 additions & 47 deletions src/Acheve.TestHost/IncludeContentAsFormUrlEncoded.cs
Original file line number Diff line number Diff line change
@@ -5,63 +5,62 @@
using System.Linq;
using System.Net.Http;

namespace Microsoft.AspNetCore.TestHost
{
/// <summary>
/// An implementation of <see cref="RequestContentOptions"/> that includes
/// the [FromForm] parameter as <see cref="IDictionary<>"/>
/// </summary>
public class IncludeContentAsFormUrlEncoded : RequestContentOptions
{
/// <inheritdoc/>
public override bool IncludeFromBodyAsContent => false;

/// <inheritdoc/>
public override bool IncludeFromFormAsContent => true;
namespace Microsoft.AspNetCore.TestHost;

/// <inheritdoc/>
public override Func<object, HttpContent> ContentBuilder =>
content => new FormUrlEncodedContent(ToKeyValue(content));
/// <summary>
/// An implementation of <see cref="RequestContentOptions"/> that includes
/// the [FromForm] parameter as <see cref="IDictionary<>"/>
/// </summary>
public class IncludeContentAsFormUrlEncoded : RequestContentOptions
{
/// <inheritdoc/>
public override bool IncludeFromBodyAsContent => false;

private IDictionary<string, string> ToKeyValue(object metaToken)
{
if (metaToken == null)
{
return null;
}
/// <inheritdoc/>
public override bool IncludeFromFormAsContent => true;

if (!(metaToken is JToken token))
{
return ToKeyValue(JObject.FromObject(metaToken));
}
/// <inheritdoc/>
public override Func<object, HttpContent> ContentBuilder =>
content => new FormUrlEncodedContent(ToKeyValue(content));

if (token.HasValues)
{
var contentData = new Dictionary<string, string>();
foreach (var child in token.Children().ToList())
{
var childContent = ToKeyValue(child);
if (childContent != null)
{
contentData = contentData.Concat(childContent)
.ToDictionary(k => k.Key, v => v.Value);
}
}
private IDictionary<string, string> ToKeyValue(object metaToken)
{
if (metaToken is null)
{
return null;
}

return contentData;
}
if (metaToken is not JToken token)
{
return ToKeyValue(JObject.FromObject(metaToken));
}

var jValue = token as JValue;
if (jValue?.Value == null)
if (token.HasValues)
{
var contentData = new Dictionary<string, string>();
var childrenContent = token.Children()
.AsEnumerable()
.Select(ToKeyValue)
.Where(childrenContent => childrenContent is not null);
foreach (var childContent in childrenContent)
{
return null;
contentData = contentData.Concat(childContent)
.ToDictionary(k => k.Key, v => v.Value);
}

var value = jValue?.Type == JTokenType.Date ?
jValue?.ToString("o", CultureInfo.InvariantCulture) :
jValue?.ToString(CultureInfo.InvariantCulture);
return contentData;
}

return new Dictionary<string, string> { { token.Path, value } };
var jValue = token as JValue;
if (jValue?.Value == null)
{
return null;
}

var value = jValue?.Type == JTokenType.Date ?
jValue?.ToString("o", CultureInfo.InvariantCulture) :
jValue?.ToString(CultureInfo.InvariantCulture);

return new Dictionary<string, string> { { token.Path, value } };
}
}
89 changes: 37 additions & 52 deletions src/Acheve.TestHost/Routing/Tokenizers/TestServerTokenCollection.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,56 @@
using System.Collections.Generic;
using System.Linq;

namespace Acheve.TestHost.Routing.Tokenizers
namespace Acheve.TestHost.Routing.Tokenizers;

public class TestServerTokenCollection
{
public class TestServerTokenCollection
private readonly Dictionary<string, TestServerToken> _activeTokens = new();

public bool ContainsToken(string tokenName)
{
Dictionary<string, TestServerToken> _activeTokens;
return _activeTokens.ContainsKey(tokenName);
}

public TestServerTokenCollection()
public void AddToken(string tokenName, string tokenValue, bool isConventional = false)
{
if (!ContainsToken(tokenName))
{
_activeTokens = new Dictionary<string, TestServerToken>();
_activeTokens.Add(tokenName,
new TestServerToken(tokenName, tokenValue, isConventional));
}
}

public bool ContainsToken(string tokenName)
{
return _activeTokens.ContainsKey(tokenName);
}
public TestServerToken Find(string tokenName)
=> ContainsToken(tokenName) ? _activeTokens[tokenName] : default;

public void AddToken(string tokenName, string tokenValue, bool isConventional = false)
{
if (!ContainsToken(tokenName))
{
_activeTokens.Add(tokenName,
new TestServerToken(tokenName, tokenValue, isConventional));
}
}
public IEnumerable<TestServerToken> GetConventionalTokens()
{
return _activeTokens.Values
.Where(token => token.IsConventional);
}

public TestServerToken Find(string tokenName)
{
if (ContainsToken(tokenName))
{
return _activeTokens[tokenName];
}
else
{
return default(TestServerToken);
}
}
public IEnumerable<TestServerToken> GetNonConventionalTokens()
{
return _activeTokens.Values
.Where(token => !token.IsConventional);
}

public IEnumerable<TestServerToken> GetConventionalTokens()
{
return _activeTokens.Values
.Where(token => token.IsConventional);
}
public IEnumerable<TestServerToken> GetUnusedTokens()
{
return _activeTokens.Values
.Where(token => !token.IsConventional && !token.Used);
}

public IEnumerable<TestServerToken> GetNonConventionalTokens()
{
return _activeTokens.Values
.Where(token => !token.IsConventional);
}
public static TestServerTokenCollection FromDictionary(Dictionary<string, string> tokenKV)
{
var tokens = new TestServerTokenCollection();

public IEnumerable<TestServerToken> GetUnusedTokens()
foreach (var item in tokenKV)
{
return _activeTokens.Values
.Where(token => !token.IsConventional && !token.Used);
tokens.AddToken(item.Key, item.Value, isConventional: false);
}

public static TestServerTokenCollection FromDictionary(Dictionary<string, string> tokenKV)
{
var tokens = new TestServerTokenCollection();

foreach (var item in tokenKV)
{
tokens.AddToken(item.Key, item.Value, isConventional: false);
}

return tokens;
}
return tokens;
}
}
17 changes: 6 additions & 11 deletions src/Acheve.TestHost/Routing/UriDiscover.cs
Original file line number Diff line number Diff line change
@@ -40,19 +40,14 @@ public static string Discover<TController>(TestServerAction action, object token

var template = (verbsTemplate ?? routeTemplate);

if (template != null)
if (template is null)
{
if (IsTildeOverride(template, out string overrideTemplate))
{
return $"{overrideTemplate}{queryStringTemplate}";
}
else
{
return $"{controllerTemplate}/{template}{queryStringTemplate}";
}
return $"{controllerTemplate}{queryStringTemplate}";
}

return $"{controllerTemplate}{queryStringTemplate}";
return IsTildeOverride(template, out string overrideTemplate) ?
$"{overrideTemplate}{queryStringTemplate}" :
$"{controllerTemplate}/{template}{queryStringTemplate}";
}

static TestServerTokenCollection AddTokens<TController>(TestServerAction action, object tokenValues)
@@ -85,7 +80,7 @@ static bool IsTildeOverride(string template, out string overrideTemplate)

if (isTildeOverride)
{
overrideTemplate = template.Substring(2); // remove ~/
overrideTemplate = template[2..]; // remove ~/
}

return isTildeOverride;
53 changes: 26 additions & 27 deletions src/Acheve.TestHost/Security/DefaultClaimsEncoder.cs
Original file line number Diff line number Diff line change
@@ -4,41 +4,40 @@
using System.Linq;
using System.Security.Claims;

namespace Acheve.TestHost
namespace Acheve.TestHost;

internal static class DefautClaimsEncoder
{
internal static class DefautClaimsEncoder
public static string Encode(IEnumerable<Claim> claims)
{
public static string Encode(IEnumerable<Claim> claims)
{
var ticket = new AuthenticationTicket(
principal: new ClaimsPrincipal(
new ClaimsIdentity(claims)),
authenticationScheme: "TestServer");
var ticket = new AuthenticationTicket(
principal: new ClaimsPrincipal(
new ClaimsIdentity(claims)),
authenticationScheme: "TestServer");

var serializer = new TicketSerializer();
var bytes = serializer.Serialize(ticket);
var serializer = new TicketSerializer();
var bytes = serializer.Serialize(ticket);

return Convert.ToBase64String(bytes);
}
return Convert.ToBase64String(bytes);
}

public static IEnumerable<Claim> Decode(string encodedValue)
public static IEnumerable<Claim> Decode(string encodedValue)
{
if (string.IsNullOrEmpty(encodedValue))
{
if (string.IsNullOrEmpty(encodedValue))
{
return Enumerable.Empty<Claim>();
}
return Enumerable.Empty<Claim>();
}

var serializer = new TicketSerializer();
try
{
var ticket = serializer.Deserialize(Convert.FromBase64String(encodedValue));
var serializer = new TicketSerializer();
try
{
var ticket = serializer.Deserialize(Convert.FromBase64String(encodedValue));

return ticket.Principal.Claims;
}
catch (Exception)
{
return Enumerable.Empty<Claim>();
}
return ticket.Principal.Claims;
}
catch
{
return Enumerable.Empty<Claim>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1671,7 +1671,7 @@ public async Task Create_get_request_with_cancellation_token_parameter()
.Build();

var param = "one";
var source = new CancellationTokenSource();
using var source = new CancellationTokenSource();
var token = source.Token;

var request = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.GetWithCancellationToken(param, token));
@@ -1690,7 +1690,7 @@ public async Task Create_post_request_with_cancellation_token_parameter()
.Build();

var param = "one";
var source = new CancellationTokenSource();
using var source = new CancellationTokenSource();
var token = source.Token;

var request = server.CreateHttpApiRequest<ValuesV5Controller>(controller => controller.PostWithCancellationToken(param, token));