1+ // Copyright (C) 2021 Xtensive LLC.
2+ // This code is distributed under MIT license terms.
3+ // See the License.txt file in the project root for more information.
4+
5+ using Microsoft . AspNetCore . TestHost ;
6+ using Microsoft . AspNetCore . Hosting ;
7+ using Microsoft . Extensions . Hosting ;
8+ using Microsoft . Extensions . DependencyInjection ;
9+ using Microsoft . AspNetCore . Builder ;
10+ using System ;
11+ using Xtensive . Orm . Tests ;
12+ using Xtensive . Orm . Configuration ;
13+
14+ namespace Xtensive . Orm . Web . Tests
15+ {
16+ [ HierarchyRoot ]
17+ public class DummyEntity : Entity
18+ {
19+ [ Field , Key ]
20+ public int Id { get ; private set ; }
21+
22+ [ Field ( Length = 50 ) ]
23+ public string Name { get ; set ; }
24+
25+ public DummyEntity ( Session session )
26+ : base ( session )
27+ {
28+ }
29+ }
30+
31+ public abstract class WebTestBase : AutoBuildTest
32+ {
33+ protected override DomainConfiguration BuildConfiguration ( )
34+ {
35+ var config = DomainConfigurationFactory . CreateWithoutSessionConfigurations ( ) ;
36+ config . Types . Register ( typeof ( DummyEntity ) ) ;
37+ config . UpgradeMode = DomainUpgradeMode . Recreate ;
38+ return config ;
39+ }
40+
41+ protected virtual void AddTestRequiredServices ( IServiceCollection services )
42+ {
43+
44+ }
45+
46+ protected virtual void AddControllers ( IServiceCollection services )
47+ {
48+ _ = services . AddControllers ( )
49+ . AddApplicationPart ( typeof ( ActionFilterTest ) . Assembly ) ;
50+ }
51+
52+ protected virtual IApplicationBuilder ConfigurePreRoutingPart ( IApplicationBuilder app )
53+ {
54+ return app ;
55+ }
56+
57+ protected virtual IApplicationBuilder ConfigurePostRoutingPart ( IApplicationBuilder app )
58+ {
59+ return app ;
60+ }
61+
62+ protected virtual IApplicationBuilder ConfigureRouting ( IApplicationBuilder app , string controller , string action )
63+ {
64+ var controllerPattern = "{controller=" + controller + "}" ;
65+ var actionPattern = "{action=" + action + "}" ;
66+
67+ return app . UseRouting ( )
68+ . UseEndpoints (
69+ endpoints => endpoints
70+ . MapControllerRoute (
71+ name : "default" ,
72+ pattern : controllerPattern + "/" + actionPattern + "/{id?}" ) ) ;
73+
74+ }
75+
76+ protected virtual IApplicationBuilder ConfigureApp ( IApplicationBuilder app , string controller , string action )
77+ {
78+ return ConfigurePostRoutingPart (
79+ ConfigureRouting (
80+ ConfigurePreRoutingPart ( app ) , controller , action ) ) ;
81+ }
82+
83+ protected IHostBuilder GetConfiguredHostBuilder < TController > ( string action )
84+ {
85+ var controller = typeof ( TController ) . Name . Replace ( "Controller" , "" ) ;
86+ return new HostBuilder ( )
87+ . ConfigureWebHost ( webBuilder => {
88+ _ = webBuilder . UseTestServer ( )
89+ . ConfigureServices ( services => {
90+ _ = services . AddSingleton < Domain > ( Domain ) ;
91+ AddTestRequiredServices ( services ) ;
92+ AddControllers ( services ) ;
93+ } )
94+ . Configure ( app =>
95+ ConfigureApp ( app , controller , action ) ) ;
96+ } ) ;
97+ }
98+
99+ protected IHostBuilder GetConfiguredHostBuilder < TController > ( string action ,
100+ Action < IServiceCollection > configureServicesAction )
101+ {
102+ var controller = typeof ( TController ) . Name . Replace ( "Controller" , "" ) ;
103+ return new HostBuilder ( )
104+ . ConfigureWebHost ( webBuilder => {
105+ _ = webBuilder . UseTestServer ( )
106+ . ConfigureServices ( services => {
107+ configureServicesAction ( services ) ;
108+ } )
109+ . Configure ( app =>
110+ ConfigureApp ( app , controller , action ) ) ;
111+ } ) ;
112+ }
113+
114+ protected IHostBuilder GetConfiguredHostBuilder < TController > ( string action ,
115+ Action < IApplicationBuilder , string , string > configureAppAction )
116+ {
117+ var controller = typeof ( TController ) . Name . Replace ( "Controller" , "" ) ;
118+ return new HostBuilder ( )
119+ . ConfigureWebHost ( webBuilder => {
120+ _ = webBuilder . UseTestServer ( )
121+ . ConfigureServices ( services => {
122+ _ = services . AddSingleton < Domain > ( Domain ) ;
123+ AddTestRequiredServices ( services ) ;
124+ AddControllers ( services ) ;
125+ } )
126+ . Configure ( app =>
127+ configureAppAction ( app , controller , action ) ) ;
128+ } ) ;
129+ }
130+
131+ protected IHostBuilder GetConfiguredHostBuilder < TController > ( string action ,
132+ Action < IServiceCollection > configureServicesAction ,
133+ Action < IApplicationBuilder , string , string > configureAppAction )
134+ {
135+ var controller = typeof ( TController ) . Name . Replace ( "Controller" , "" ) ;
136+ return new HostBuilder ( )
137+ . ConfigureWebHost ( webBuilder => {
138+ _ = webBuilder . UseTestServer ( )
139+ . ConfigureServices ( services => {
140+ configureServicesAction ( services ) ;
141+ } )
142+ . Configure ( app =>
143+ configureAppAction ( app , controller , action ) ) ;
144+ } ) ;
145+ }
146+ }
147+ }
0 commit comments