/
WebApplicationFactoryClientOptions.cs
80 lines (70 loc) · 3 KB
/
WebApplicationFactoryClientOptions.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Linq;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc.Testing.Handlers;
namespace Microsoft.AspNetCore.Mvc.Testing;
/// <summary>
/// The default options to use to when creating
/// <see cref="HttpClient"/> instances by calling
/// <see cref="WebApplicationFactory{TEntryPoint}.CreateClient(WebApplicationFactoryClientOptions)"/>.
/// </summary>
public class WebApplicationFactoryClientOptions
{
/// <summary>
/// Initializes a new instance of <see cref="WebApplicationFactoryClientOptions"/>.
/// </summary>
public WebApplicationFactoryClientOptions()
{
}
// Copy constructor
internal WebApplicationFactoryClientOptions(WebApplicationFactoryClientOptions clientOptions)
{
BaseAddress = clientOptions.BaseAddress;
AllowAutoRedirect = clientOptions.AllowAutoRedirect;
MaxAutomaticRedirections = clientOptions.MaxAutomaticRedirections;
HandleCookies = clientOptions.HandleCookies;
}
/// <summary>
/// Gets or sets the base address of <see cref="HttpClient"/> instances created by calling
/// <see cref="WebApplicationFactory{TEntryPoint}.CreateClient(WebApplicationFactoryClientOptions)"/>.
/// The default is <c>http://localhost</c>.
/// </summary>
public Uri BaseAddress { get; set; } = new Uri("http://localhost");
/// <summary>
/// Gets or sets whether or not <see cref="HttpClient"/> instances created by calling
/// <see cref="WebApplicationFactory{TEntryPoint}.CreateClient(WebApplicationFactoryClientOptions)"/>
/// should automatically follow redirect responses.
/// The default is <c>true</c>.
/// </summary>
public bool AllowAutoRedirect { get; set; } = true;
/// <summary>
/// Gets or sets the maximum number of redirect responses that <see cref="HttpClient"/> instances
/// created by calling <see cref="WebApplicationFactory{TEntryPoint}.CreateClient(WebApplicationFactoryClientOptions)"/>
/// should follow.
/// The default is <c>7</c>.
/// </summary>
public int MaxAutomaticRedirections { get; set; } = RedirectHandler.DefaultMaxRedirects;
/// <summary>
/// Gets or sets whether <see cref="HttpClient"/> instances created by calling
/// <see cref="WebApplicationFactory{TEntryPoint}.CreateClient(WebApplicationFactoryClientOptions)"/>
/// should handle cookies.
/// The default is <c>true</c>.
/// </summary>
public bool HandleCookies { get; set; } = true;
internal DelegatingHandler[] CreateHandlers()
{
return CreateHandlersCore().ToArray();
IEnumerable<DelegatingHandler> CreateHandlersCore()
{
if (AllowAutoRedirect)
{
yield return new RedirectHandler(MaxAutomaticRedirections);
}
if (HandleCookies)
{
yield return new CookieContainerHandler();
}
}
}
}