-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathAgentTesterT.cs
97 lines (84 loc) · 4.78 KB
/
AgentTesterT.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using CoreEx;
using Microsoft.AspNetCore.TestHost;
using System;
using System.Net.Http;
using UnitTestEx;
using UnitTestEx.AspNetCore;
namespace Beef.Test.NUnit
{
/// <summary>
/// Provides existing <c>Test</c> methods to support backwards compatibility.
/// </summary>
public sealed class AgentTester<TEntryPoint> : IDisposable where TEntryPoint : class
{
private readonly ApiTester<TEntryPoint> _parent;
/// <summary>
/// Initializes a new instance of the <see cref="AgentTester{TEntryPoint}"/> class.
/// </summary>
/// <param name="parent">The parent <see cref="ApiTester{TEntryPoint}"/>.</param>
internal AgentTester(ApiTester<TEntryPoint> parent) => _parent = parent;
/// <inheritdoc/>
public void Dispose() => _parent.Dispose();
/// <summary>
/// Gets the parent (owning) <see cref="ApiTester{TEntryPoint}"/>.
/// </summary>
public ApiTester<TEntryPoint> Parent => _parent;
/// <summary>
/// Enables an agent (<see cref="CoreEx.Http.TypedHttpClientBase"/>) to be used to send a <see cref="HttpRequestMessage"/> to the underlying <see cref="TestServer"/>.
/// </summary>
/// <typeparam name="TAgent">The <see cref="CoreEx.Http.TypedHttpClientBase"/> <see cref="Type"/>.</typeparam>
/// <param name="userName">The overridding test user name.</param>
/// <returns>The <see cref="UnitTestEx.AspNetCore.AgentTester{TAgent}"/>.</returns>
public UnitTestEx.AspNetCore.AgentTester<TAgent> Test<TAgent>(string? userName = null) where TAgent : CoreEx.Http.TypedHttpClientBase
{
var at = _parent.Agent().With<TAgent>();
userName ??= (ExecutionContext.HasCurrent ? ExecutionContext.Current.UserName : null);
if (userName != null)
at.WithUser(userName);
return at;
}
/// <summary>
/// Enables an agent (<see cref="CoreEx.Http.TypedHttpClientBase"/>) to be used to send a <see cref="HttpRequestMessage"/> to the underlying <see cref="TestServer"/>.
/// </summary>
/// <typeparam name="TAgent">The <see cref="CoreEx.Http.TypedHttpClientBase"/> <see cref="Type"/>.</typeparam>
/// <param name="userIdentifier">The overridding test user identifier.</param>
/// <returns>The <see cref="UnitTestEx.AspNetCore.AgentTester{TAgent}"/>.</returns>
public UnitTestEx.AspNetCore.AgentTester<TAgent> Test<TAgent>(object? userIdentifier) where TAgent : CoreEx.Http.TypedHttpClientBase
{
var at = _parent.Agent().With<TAgent>();
if (userIdentifier != null)
at.WithUser(userIdentifier);
return at;
}
/// <summary>
/// Enables an agent (<see cref="CoreEx.Http.TypedHttpClientBase"/>) to be used to send a <see cref="HttpRequestMessage"/> to the underlying <see cref="TestServer"/>.
/// </summary>
/// <typeparam name="TAgent">The <see cref="CoreEx.Http.TypedHttpClientBase"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TResponse">The response value <see cref="Type"/>.</typeparam>
/// <param name="userName">The overridding test user name.</param>
/// <returns>The <see cref="UnitTestEx.AspNetCore.AgentTester{TAgent, TValue}"/>.</returns>
public UnitTestEx.AspNetCore.AgentTester<TAgent, TResponse> Test<TAgent, TResponse>(string? userName = null) where TAgent : CoreEx.Http.TypedHttpClientBase
{
var at = _parent.Agent().With<TAgent, TResponse>();
userName ??= (ExecutionContext.HasCurrent ? ExecutionContext.Current.UserName : null);
if (userName != null)
at.WithUser(userName);
return at;
}
/// <summary>
/// Enables an agent (<see cref="CoreEx.Http.TypedHttpClientBase"/>) to be used to send a <see cref="HttpRequestMessage"/> to the underlying <see cref="TestServer"/>.
/// </summary>
/// <typeparam name="TAgent">The <see cref="CoreEx.Http.TypedHttpClientBase"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TResponse">The response value <see cref="Type"/>.</typeparam>
/// <param name="userIdentifier">The overridding test user identifier.</param>
/// <returns>The <see cref="UnitTestEx.AspNetCore.AgentTester{TAgent, TValue}"/>.</returns>
public UnitTestEx.AspNetCore.AgentTester<TAgent, TResponse> Test<TAgent, TResponse>(object? userIdentifier) where TAgent : CoreEx.Http.TypedHttpClientBase
{
var at = _parent.Agent().With<TAgent, TResponse>();
if (userIdentifier != null)
at.WithUser(userIdentifier);
return at;
}
}
}