11using System ;
2- using System . Collections . Generic ;
32using System . Net . Http ;
4- using System . Net . Http . Headers ;
53using System . Threading . Tasks ;
6- using JsonApiDotNetCore . Middleware ;
74using Microsoft . AspNetCore . Hosting ;
85using Microsoft . AspNetCore . Mvc . Testing ;
96using Microsoft . EntityFrameworkCore ;
107using Microsoft . Extensions . DependencyInjection ;
118using Microsoft . Extensions . Hosting ;
129using Microsoft . Extensions . Logging ;
13- using Newtonsoft . Json ;
1410
1511namespace TestBuildingBlocks
1612{
@@ -23,7 +19,7 @@ namespace TestBuildingBlocks
2319 /// <typeparam name="TStartup">The server Startup class, which can be defined in the test project.</typeparam>
2420 /// <typeparam name="TRemoteStartup">The base class for <typeparamref name="TStartup"/>, which MUST be defined in the API project.</typeparam>
2521 /// <typeparam name="TDbContext">The EF Core database context, which can be defined in the test project.</typeparam>
26- public abstract class BaseIntegrationTestContext < TStartup , TRemoteStartup , TDbContext > : IDisposable
22+ public abstract class BaseIntegrationTestContext < TStartup , TRemoteStartup , TDbContext > : IntegrationTest , IDisposable
2723 where TStartup : class
2824 where TRemoteStartup : class
2925 where TDbContext : DbContext
@@ -35,6 +31,11 @@ public abstract class BaseIntegrationTestContext<TStartup, TRemoteStartup, TDbCo
3531
3632 public WebApplicationFactory < TRemoteStartup > Factory => _lazyFactory . Value ;
3733
34+ protected override HttpClient CreateClient ( )
35+ {
36+ return Factory . CreateClient ( ) ;
37+ }
38+
3839 protected BaseIntegrationTestContext ( )
3940 {
4041 _lazyFactory = new Lazy < WebApplicationFactory < TRemoteStartup > > ( CreateFactory ) ;
@@ -103,102 +104,6 @@ public async Task RunOnDatabaseAsync(Func<TDbContext, Task> asyncAction)
103104 await asyncAction ( dbContext ) ;
104105 }
105106
106- public async Task < ( HttpResponseMessage httpResponse , TResponseDocument responseDocument ) >
107- ExecuteGetAsync < TResponseDocument > ( string requestUrl ,
108- IEnumerable < MediaTypeWithQualityHeaderValue > acceptHeaders = null )
109- {
110- return await ExecuteRequestAsync < TResponseDocument > ( HttpMethod . Get , requestUrl , null , null , acceptHeaders ) ;
111- }
112-
113- public async Task < ( HttpResponseMessage httpResponse , TResponseDocument responseDocument ) >
114- ExecutePostAsync < TResponseDocument > ( string requestUrl , object requestBody ,
115- string contentType = HeaderConstants . MediaType ,
116- IEnumerable < MediaTypeWithQualityHeaderValue > acceptHeaders = null )
117- {
118- return await ExecuteRequestAsync < TResponseDocument > ( HttpMethod . Post , requestUrl , requestBody , contentType ,
119- acceptHeaders ) ;
120- }
121-
122- public async Task < ( HttpResponseMessage httpResponse , TResponseDocument responseDocument ) >
123- ExecutePatchAsync < TResponseDocument > ( string requestUrl , object requestBody ,
124- string contentType = HeaderConstants . MediaType ,
125- IEnumerable < MediaTypeWithQualityHeaderValue > acceptHeaders = null )
126- {
127- return await ExecuteRequestAsync < TResponseDocument > ( HttpMethod . Patch , requestUrl , requestBody , contentType ,
128- acceptHeaders ) ;
129- }
130-
131- public async Task < ( HttpResponseMessage httpResponse , TResponseDocument responseDocument ) >
132- ExecuteDeleteAsync < TResponseDocument > ( string requestUrl , object requestBody = null ,
133- string contentType = HeaderConstants . MediaType ,
134- IEnumerable < MediaTypeWithQualityHeaderValue > acceptHeaders = null )
135- {
136- return await ExecuteRequestAsync < TResponseDocument > ( HttpMethod . Delete , requestUrl , requestBody , contentType ,
137- acceptHeaders ) ;
138- }
139-
140- private async Task < ( HttpResponseMessage httpResponse , TResponseDocument responseDocument ) >
141- ExecuteRequestAsync < TResponseDocument > ( HttpMethod method , string requestUrl , object requestBody ,
142- string contentType , IEnumerable < MediaTypeWithQualityHeaderValue > acceptHeaders )
143- {
144- var request = new HttpRequestMessage ( method , requestUrl ) ;
145- string requestText = SerializeRequest ( requestBody ) ;
146-
147- if ( ! string . IsNullOrEmpty ( requestText ) )
148- {
149- request . Content = new StringContent ( requestText ) ;
150-
151- if ( contentType != null )
152- {
153- request . Content . Headers . ContentType = MediaTypeHeaderValue . Parse ( contentType ) ;
154- }
155- }
156-
157- using HttpClient client = Factory . CreateClient ( ) ;
158-
159- if ( acceptHeaders != null )
160- {
161- foreach ( var acceptHeader in acceptHeaders )
162- {
163- client . DefaultRequestHeaders . Accept . Add ( acceptHeader ) ;
164- }
165- }
166-
167- HttpResponseMessage responseMessage = await client . SendAsync ( request ) ;
168-
169- string responseText = await responseMessage . Content . ReadAsStringAsync ( ) ;
170- var responseDocument = DeserializeResponse < TResponseDocument > ( responseText ) ;
171-
172- return ( responseMessage , responseDocument ) ;
173- }
174-
175- private string SerializeRequest ( object requestBody )
176- {
177- return requestBody == null
178- ? null
179- : requestBody is string stringRequestBody
180- ? stringRequestBody
181- : JsonConvert . SerializeObject ( requestBody ) ;
182- }
183-
184- private TResponseDocument DeserializeResponse < TResponseDocument > ( string responseText )
185- {
186- if ( typeof ( TResponseDocument ) == typeof ( string ) )
187- {
188- return ( TResponseDocument ) ( object ) responseText ;
189- }
190-
191- try
192- {
193- return JsonConvert . DeserializeObject < TResponseDocument > ( responseText ,
194- IntegrationTestConfiguration . DeserializationSettings ) ;
195- }
196- catch ( JsonException exception )
197- {
198- throw new FormatException ( $ "Failed to deserialize response body to JSON:\n { responseText } ", exception ) ;
199- }
200- }
201-
202107 private sealed class IntegrationTestWebApplicationFactory : WebApplicationFactory < TRemoteStartup >
203108 {
204109 private Action < ILoggingBuilder > _loggingConfiguration ;
0 commit comments