1
+ using System ;
2
+ using System . IO ;
3
+ using System . Net . Http ;
4
+ using System . Net . Http . Headers ;
5
+ using System . Reflection ;
6
+ using Microsoft . AspNetCore . Hosting ;
7
+ using Microsoft . AspNetCore . Mvc . ApplicationParts ;
8
+ using Microsoft . AspNetCore . Mvc . Controllers ;
9
+ using Microsoft . AspNetCore . Mvc . ViewComponents ;
10
+ using Microsoft . AspNetCore . TestHost ;
11
+ using Microsoft . Extensions . Configuration ;
12
+ using Microsoft . Extensions . DependencyInjection ;
13
+
14
+ namespace TesteBackendEnContact . Api . IntegrationTests
15
+ {
16
+ public class TestFixture < TStartup > : IDisposable
17
+ {
18
+ public static string GetProjectPath ( string projectRelativePath , Assembly startupAssembly )
19
+ {
20
+ var projectName = startupAssembly . GetName ( ) . Name ;
21
+
22
+ var applicationBasePath = AppContext . BaseDirectory ;
23
+
24
+ var directoryInfo = new DirectoryInfo ( applicationBasePath ) ;
25
+
26
+ do
27
+ {
28
+ directoryInfo = directoryInfo . Parent ;
29
+
30
+ var projectDirectoryInfo = new DirectoryInfo ( Path . Combine ( directoryInfo . FullName , projectRelativePath ) ) ;
31
+
32
+ if ( projectDirectoryInfo . Exists )
33
+ if ( new FileInfo ( Path . Combine ( projectDirectoryInfo . FullName , projectName , $ "{ projectName } .csproj") ) . Exists )
34
+ return Path . Combine ( projectDirectoryInfo . FullName , projectName ) ;
35
+ }
36
+ while ( directoryInfo . Parent != null ) ;
37
+
38
+ throw new Exception ( $ "Project root could not be located using the application root { applicationBasePath } .") ;
39
+ }
40
+
41
+ private TestServer Server ;
42
+
43
+ public TestFixture ( )
44
+ : this ( Path . Combine ( "" ) )
45
+ {
46
+ }
47
+
48
+ public HttpClient Client { get ; }
49
+
50
+ public void Dispose ( )
51
+ {
52
+ Client . Dispose ( ) ;
53
+ Server . Dispose ( ) ;
54
+ }
55
+
56
+ protected virtual void InitializeServices ( IServiceCollection services )
57
+ {
58
+ var startupAssembly = typeof ( TStartup ) . GetTypeInfo ( ) . Assembly ;
59
+
60
+ var manager = new ApplicationPartManager
61
+ {
62
+ ApplicationParts =
63
+ {
64
+ new AssemblyPart ( startupAssembly )
65
+ } ,
66
+ FeatureProviders =
67
+ {
68
+ new ControllerFeatureProvider ( ) ,
69
+ new ViewComponentFeatureProvider ( )
70
+ }
71
+ } ;
72
+
73
+ services . AddSingleton ( manager ) ;
74
+ }
75
+
76
+ protected TestFixture ( string relativeTargetProjectParentDir )
77
+ {
78
+ var startupAssembly = typeof ( TStartup ) . GetTypeInfo ( ) . Assembly ;
79
+ var contentRoot = GetProjectPath ( relativeTargetProjectParentDir , startupAssembly ) ;
80
+
81
+ var configurationBuilder = new ConfigurationBuilder ( )
82
+ . SetBasePath ( contentRoot )
83
+ . AddJsonFile ( "appsettings.json" ) ;
84
+
85
+ var webHostBuilder = new WebHostBuilder ( )
86
+ . UseContentRoot ( contentRoot )
87
+ . ConfigureServices ( InitializeServices )
88
+ . UseConfiguration ( configurationBuilder . Build ( ) )
89
+ . UseEnvironment ( "Development" )
90
+ . UseStartup ( typeof ( TStartup ) ) ;
91
+
92
+ // Create instance of test server
93
+ Server = new TestServer ( webHostBuilder ) ;
94
+
95
+ // Add configuration for client
96
+ Client = Server . CreateClient ( ) ;
97
+ Client . BaseAddress = new Uri ( "http://localhost:5000" ) ;
98
+ Client . DefaultRequestHeaders . Accept . Clear ( ) ;
99
+ Client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
100
+ }
101
+ }
102
+ }
0 commit comments