11using FoodStorageApi . Application . Common . Interfaces ;
22using FoodStorageApi . Infrastructure . Services ;
3- using Microsoft . AspNetCore . Builder ;
4- using Microsoft . AspNetCore . Hosting ;
5- using Microsoft . AspNetCore . Http ;
6- using Microsoft . AspNetCore . Mvc . Testing ;
7- using Microsoft . Extensions . DependencyInjection ;
83using Microsoft . Extensions . Logging ;
4+ using System . Net ;
95
106namespace FoodStorageApi . Infrastructure . IntegrationTests . Services ;
117
12- public class BaseWebServiceClientIntegrationTests : IClassFixture < WebApplicationFactory < TestStartup > >
8+ public class BaseWebServiceClientIntegrationTests : IDisposable
139{
14- private readonly WebApplicationFactory < TestStartup > _factory ;
1510 private readonly HttpClient _httpClient ;
1611 private readonly IBaseWebServiceClient _webServiceClient ;
12+ private readonly ILogger < BaseWebServiceClient > _logger ;
1713
18- public BaseWebServiceClientIntegrationTests ( WebApplicationFactory < TestStartup > factory )
14+ public BaseWebServiceClientIntegrationTests ( )
1915 {
20- _factory = factory ;
21-
22- // Use the factory directly for the test endpoints
23- _httpClient = _factory . CreateClient ( ) ;
24-
25- // Get logger from the factory's service provider
26- using var scope = _factory . Services . CreateScope ( ) ;
27- var logger = scope . ServiceProvider . GetService < ILogger < BaseWebServiceClient > > ( )
28- ?? new LoggerFactory ( ) . CreateLogger < BaseWebServiceClient > ( ) ;
29- _webServiceClient = new BaseWebServiceClient ( _httpClient , logger ) ;
16+ _httpClient = new HttpClient ( ) ;
17+ _logger = new LoggerFactory ( ) . CreateLogger < BaseWebServiceClient > ( ) ;
18+ _webServiceClient = new BaseWebServiceClient ( _httpClient , _logger ) ;
3019 }
3120
3221 [ Fact ]
3322 public async Task GetAsync_WithRealHttpClient_ReturnsExpectedResponse ( )
3423 {
35- // Arrange
36- const string expectedResponse = "Hello from GET endpoint " ;
24+ // Arrange - using httpbin.org for testing
25+ const string endpoint = "https://httpbin.org/json " ;
3726
3827 // Act
39- var result = await _webServiceClient . GetAsync ( "/test-get" ) ;
28+ var result = await _webServiceClient . GetAsync ( endpoint ) ;
4029
4130 // Assert
42- Assert . Equal ( expectedResponse , result ) ;
31+ Assert . NotNull ( result ) ;
32+ Assert . Contains ( "slideshow" , result ) ; // httpbin.org/json returns a JSON with slideshow data
4333 }
4434
4535 [ Fact ]
4636 public async Task PostAsync_WithRealHttpClient_ReturnsExpectedResponse ( )
4737 {
48- // Arrange
38+ // Arrange - using httpbin.org for testing
39+ const string endpoint = "https://httpbin.org/post" ;
4940 const string content = "test content" ;
50- const string expectedResponse = "Hello from POST endpoint" ;
5141
5242 // Act
53- var result = await _webServiceClient . PostAsync ( "/test-post" , content ) ;
43+ var result = await _webServiceClient . PostAsync ( endpoint , content ) ;
5444
5545 // Assert
56- Assert . Equal ( expectedResponse , result ) ;
46+ Assert . NotNull ( result ) ;
47+ Assert . Contains ( "\" data\" : \" test content\" " , result ) ; // httpbin echoes back the data
5748 }
5849
5950 [ Fact ]
6051 public async Task PutAsync_WithRealHttpClient_ReturnsExpectedResponse ( )
6152 {
62- // Arrange
53+ // Arrange - using httpbin.org for testing
54+ const string endpoint = "https://httpbin.org/put" ;
6355 const string content = "test content" ;
64- const string expectedResponse = "Hello from PUT endpoint" ;
6556
6657 // Act
67- var result = await _webServiceClient . PutAsync ( "/test-put" , content ) ;
58+ var result = await _webServiceClient . PutAsync ( endpoint , content ) ;
6859
6960 // Assert
70- Assert . Equal ( expectedResponse , result ) ;
61+ Assert . NotNull ( result ) ;
62+ Assert . Contains ( "\" data\" : \" test content\" " , result ) ; // httpbin echoes back the data
7163 }
7264
7365 [ Fact ]
7466 public async Task DeleteAsync_WithRealHttpClient_ReturnsExpectedResponse ( )
7567 {
76- // Arrange
77- const string expectedResponse = "Hello from DELETE endpoint " ;
68+ // Arrange - using httpbin.org for testing
69+ const string endpoint = "https://httpbin.org/delete " ;
7870
7971 // Act
80- var result = await _webServiceClient . DeleteAsync ( "/test-delete" ) ;
72+ var result = await _webServiceClient . DeleteAsync ( endpoint ) ;
8173
8274 // Assert
83- Assert . Equal ( expectedResponse , result ) ;
75+ Assert . NotNull ( result ) ;
76+ Assert . Contains ( "\" url\" : \" https://httpbin.org/delete\" " , result ) ;
8477 }
8578
8679 [ Fact ]
8780 public async Task GetAsync_WithNonExistentEndpoint_ThrowsHttpRequestException ( )
8881 {
8982 // Act & Assert
9083 await Assert . ThrowsAsync < HttpRequestException > (
91- ( ) => _webServiceClient . GetAsync ( "/non-existent " ) ) ;
84+ ( ) => _webServiceClient . GetAsync ( "https://httpbin.org/status/404 " ) ) ;
9285 }
9386
9487 [ Fact ]
9588 public async Task PostAsync_WithJsonContent_SendsCorrectContentType ( )
9689 {
97- // Arrange
90+ // Arrange - using httpbin.org which echoes headers
91+ const string endpoint = "https://httpbin.org/post" ;
9892 const string jsonContent = "{\" name\" :\" test\" ,\" value\" :123}" ;
9993
10094 // Act
101- var result = await _webServiceClient . PostAsync ( "/test-post-json" , jsonContent ) ;
95+ var result = await _webServiceClient . PostAsync ( endpoint , jsonContent ) ;
10296
10397 // Assert
98+ Assert . NotNull ( result ) ;
10499 Assert . Contains ( "application/json" , result ) ;
100+ Assert . Contains ( "\" data\" : \" {\\ \" name\\ \" :\\ \" test\\ \" ,\\ \" value\\ \" :123}\" " , result ) ;
105101 }
106102
107103 [ Fact ]
108104 public async Task GetAsync_WithTimeout_ThrowsTaskCanceledException ( )
109105 {
110- // Arrange
106+ // Arrange - using httpbin delay endpoint with short timeout
111107 using var cts = new CancellationTokenSource ( TimeSpan . FromMilliseconds ( 100 ) ) ;
112108
113109 // Act & Assert
114110 await Assert . ThrowsAnyAsync < OperationCanceledException > (
115- ( ) => _webServiceClient . GetAsync ( "/test-delay" , cts . Token ) ) ;
116- }
117- }
118-
119- // Test startup class for creating mock endpoints
120- public class TestStartup
121- {
122- public void ConfigureServices ( IServiceCollection services )
123- {
124- services . AddRouting ( ) ;
125- services . AddLogging ( ) ;
111+ ( ) => _webServiceClient . GetAsync ( "https://httpbin.org/delay/5" , cts . Token ) ) ;
126112 }
127113
128- public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
114+ public void Dispose ( )
129115 {
130- app . UseRouting ( ) ;
131-
132- app . UseEndpoints ( endpoints =>
133- {
134- endpoints . MapGet ( "/test-get" , async context =>
135- {
136- await context . Response . WriteAsync ( "Hello from GET endpoint" ) ;
137- } ) ;
138-
139- endpoints . MapPost ( "/test-post" , async context =>
140- {
141- await context . Response . WriteAsync ( "Hello from POST endpoint" ) ;
142- } ) ;
143-
144- endpoints . MapPut ( "/test-put" , async context =>
145- {
146- await context . Response . WriteAsync ( "Hello from PUT endpoint" ) ;
147- } ) ;
148-
149- endpoints . MapDelete ( "/test-delete" , async context =>
150- {
151- await context . Response . WriteAsync ( "Hello from DELETE endpoint" ) ;
152- } ) ;
153-
154- endpoints . MapPost ( "/test-post-json" , async context =>
155- {
156- var contentType = context . Request . ContentType ;
157- await context . Response . WriteAsync ( $ "Content-Type: { contentType } ") ;
158- } ) ;
159-
160- endpoints . MapGet ( "/test-delay" , async context =>
161- {
162- await Task . Delay ( 2000 ) ; // Simulate slow endpoint
163- await context . Response . WriteAsync ( "Delayed response" ) ;
164- } ) ;
165- } ) ;
116+ _httpClient ? . Dispose ( ) ;
166117 }
167118}
0 commit comments