@@ -11,38 +11,36 @@ namespace ARMEmulator.Tests.Services;
1111
1212public sealed class ApiClientTests : IDisposable
1313{
14- private readonly HttpClient _httpClient ;
15- private readonly TestHttpMessageHandler _handler ;
16- private readonly ApiClient _apiClient ;
14+ private readonly HttpClient httpClient ;
15+ private readonly TestHttpMessageHandler handler ;
16+ private readonly ApiClient apiClient ;
1717
1818 public ApiClientTests ( )
1919 {
20- _handler = new TestHttpMessageHandler ( ) ;
21- _httpClient = new HttpClient ( _handler ) {
22- BaseAddress = new Uri ( "http://localhost:8080" )
23- } ;
24- _apiClient = new ApiClient ( _httpClient ) ;
20+ handler = new TestHttpMessageHandler ( ) ;
21+ httpClient = new HttpClient ( handler ) { BaseAddress = new Uri ( "http://localhost:8080" ) } ;
22+ apiClient = new ApiClient ( httpClient ) ;
2523 }
2624
2725 [ Fact ]
2826 public async Task CreateSessionAsync_WithSuccessResponse_ReturnsSessionInfo ( )
2927 {
3028 var sessionInfo = new SessionInfo ( "session-123" ) ;
31- _handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( sessionInfo , ApiJsonContext . Default . SessionInfo ) ) ;
29+ handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( sessionInfo , ApiJsonContext . Default . SessionInfo ) ) ;
3230
33- var result = await _apiClient . CreateSessionAsync ( ) ;
31+ var result = await apiClient . CreateSessionAsync ( ) ;
3432
3533 _ = result . SessionId . Should ( ) . Be ( "session-123" ) ;
36- _ = _handler . LastRequest ! . RequestUri ! . PathAndQuery . Should ( ) . Be ( "/api/v1/session" ) ;
37- _ = _handler . LastRequest . Method . Should ( ) . Be ( HttpMethod . Post ) ;
34+ _ = handler . LastRequest ! . RequestUri ! . PathAndQuery . Should ( ) . Be ( "/api/v1/session" ) ;
35+ _ = handler . LastRequest . Method . Should ( ) . Be ( HttpMethod . Post ) ;
3836 }
3937
4038 [ Fact ]
4139 public async Task CreateSessionAsync_WhenBackendUnreachable_ThrowsBackendUnavailableException ( )
4240 {
43- _handler . SetException ( new HttpRequestException ( "Connection refused" ) ) ;
41+ handler . SetException ( new HttpRequestException ( "Connection refused" ) ) ;
4442
45- var act = async ( ) => await _apiClient . CreateSessionAsync ( ) ;
43+ var act = async ( ) => await apiClient . CreateSessionAsync ( ) ;
4644
4745 _ = await act . Should ( ) . ThrowAsync < BackendUnavailableException > ( )
4846 . WithMessage ( "*Cannot connect to backend*" ) ;
@@ -52,9 +50,9 @@ public async Task CreateSessionAsync_WhenBackendUnreachable_ThrowsBackendUnavail
5250 public async Task GetStatusAsync_WithValidSession_ReturnsStatus ( )
5351 {
5452 var status = new VMStatus ( VMState . Idle , 0x8000 , 0 ) ;
55- _handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( status , ApiJsonContext . Default . VMStatus ) ) ;
53+ handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( status , ApiJsonContext . Default . VMStatus ) ) ;
5654
57- var result = await _apiClient . GetStatusAsync ( "session-123" ) ;
55+ var result = await apiClient . GetStatusAsync ( "session-123" ) ;
5856
5957 _ = result . State . Should ( ) . Be ( VMState . Idle ) ;
6058 _ = result . PC . Should ( ) . Be ( 0x8000u ) ;
@@ -63,9 +61,9 @@ public async Task GetStatusAsync_WithValidSession_ReturnsStatus()
6361 [ Fact ]
6462 public async Task GetStatusAsync_WithInvalidSession_ThrowsSessionNotFoundException ( )
6563 {
66- _handler . SetResponse ( HttpStatusCode . NotFound , "{\" error\" :\" session not found\" }" ) ;
64+ handler . SetResponse ( HttpStatusCode . NotFound , "{\" error\" :\" session not found\" }" ) ;
6765
68- var act = async ( ) => await _apiClient . GetStatusAsync ( "invalid-session" ) ;
66+ var act = async ( ) => await apiClient . GetStatusAsync ( "invalid-session" ) ;
6967
7068 _ = await act . Should ( ) . ThrowAsync < SessionNotFoundException > ( )
7169 . Where ( ex => ex . SessionId == "invalid-session" ) ;
@@ -75,13 +73,13 @@ public async Task GetStatusAsync_WithInvalidSession_ThrowsSessionNotFoundExcepti
7573 public async Task LoadProgramAsync_WithValidProgram_ReturnsLoadResponse ( )
7674 {
7775 var response = new LoadProgramResponse ( true , [ ] , 0x8000 ) ;
78- _handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( response , ApiJsonContext . Default . LoadProgramResponse ) ) ;
76+ handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( response , ApiJsonContext . Default . LoadProgramResponse ) ) ;
7977
80- var result = await _apiClient . LoadProgramAsync ( "session-123" , "MOV R0, #1" ) ;
78+ var result = await apiClient . LoadProgramAsync ( "session-123" , "MOV R0, #1" ) ;
8179
8280 _ = result . Success . Should ( ) . BeTrue ( ) ;
8381 _ = result . EntryPoint . Should ( ) . Be ( 0x8000u ) ;
84- _ = _handler . LastRequest ! . Content . Should ( ) . NotBeNull ( ) ;
82+ _ = handler . LastRequest ! . Content . Should ( ) . NotBeNull ( ) ;
8583 }
8684
8785 [ Fact ]
@@ -94,9 +92,9 @@ public async Task LoadProgramAsync_WithParseErrors_ThrowsProgramLoadException()
9492 new ParseError ( 2 , 10 , "Unknown register" )
9593 ]
9694 ) ;
97- _handler . SetResponse ( HttpStatusCode . BadRequest , JsonSerializer . Serialize ( errorResponse , ApiJsonContext . Default . ApiErrorResponse ) ) ;
95+ handler . SetResponse ( HttpStatusCode . BadRequest , JsonSerializer . Serialize ( errorResponse , ApiJsonContext . Default . ApiErrorResponse ) ) ;
9896
99- var act = async ( ) => await _apiClient . LoadProgramAsync ( "session-123" , "INVALID" ) ;
97+ var act = async ( ) => await apiClient . LoadProgramAsync ( "session-123" , "INVALID" ) ;
10098
10199 var exception = await act . Should ( ) . ThrowAsync < ProgramLoadException > ( ) ;
102100 _ = exception . Which . Errors . Should ( ) . HaveCount ( 2 ) ;
@@ -108,22 +106,22 @@ public async Task LoadProgramAsync_WithParseErrors_ThrowsProgramLoadException()
108106 public async Task StepAsync_WithValidSession_ReturnsRegisters ( )
109107 {
110108 var registers = RegisterState . Create ( r0 : 42 ) ;
111- _handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( registers , ApiJsonContext . Default . RegisterState ) ) ;
109+ handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( registers , ApiJsonContext . Default . RegisterState ) ) ;
112110
113- var result = await _apiClient . StepAsync ( "session-123" ) ;
111+ var result = await apiClient . StepAsync ( "session-123" ) ;
114112
115113 _ = result . R0 . Should ( ) . Be ( 42u ) ;
116- _ = _handler . LastRequest ! . RequestUri ! . PathAndQuery . Should ( ) . Be ( "/api/v1/session/session-123/step" ) ;
117- _ = _handler . LastRequest . Method . Should ( ) . Be ( HttpMethod . Post ) ;
114+ _ = handler . LastRequest ! . RequestUri ! . PathAndQuery . Should ( ) . Be ( "/api/v1/session/session-123/step" ) ;
115+ _ = handler . LastRequest . Method . Should ( ) . Be ( HttpMethod . Post ) ;
118116 }
119117
120118 [ Fact ]
121119 public async Task EvaluateExpressionAsync_WithValidExpression_ReturnsValue ( )
122120 {
123121 var response = new EvaluationResponse ( 42u ) ;
124- _handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( response , ApiJsonContext . Default . EvaluationResponse ) ) ;
122+ handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( response , ApiJsonContext . Default . EvaluationResponse ) ) ;
125123
126- var result = await _apiClient . EvaluateExpressionAsync ( "session-123" , "r0 + r1" ) ;
124+ var result = await apiClient . EvaluateExpressionAsync ( "session-123" , "r0 + r1" ) ;
127125
128126 _ = result . Should ( ) . Be ( 42u ) ;
129127 }
@@ -132,9 +130,9 @@ public async Task EvaluateExpressionAsync_WithValidExpression_ReturnsValue()
132130 public async Task EvaluateExpressionAsync_WithInvalidExpression_ThrowsExpressionEvaluationException ( )
133131 {
134132 var errorResponse = new ApiErrorResponse ( "Invalid syntax" ) ;
135- _handler . SetResponse ( HttpStatusCode . BadRequest , JsonSerializer . Serialize ( errorResponse , ApiJsonContext . Default . ApiErrorResponse ) ) ;
133+ handler . SetResponse ( HttpStatusCode . BadRequest , JsonSerializer . Serialize ( errorResponse , ApiJsonContext . Default . ApiErrorResponse ) ) ;
136134
137- var act = async ( ) => await _apiClient . EvaluateExpressionAsync ( "session-123" , "invalid" ) ;
135+ var act = async ( ) => await apiClient . EvaluateExpressionAsync ( "session-123" , "invalid" ) ;
138136
139137 var exception = await act . Should ( ) . ThrowAsync < ExpressionEvaluationException > ( ) ;
140138 _ = exception . Which . Expression . Should ( ) . Be ( "invalid" ) ;
@@ -145,31 +143,31 @@ public async Task GetMemoryAsync_ReturnsMemoryData()
145143 {
146144 var memory = new byte [ ] { 0x01 , 0x02 , 0x03 , 0x04 } ;
147145 var response = new MemoryResponse ( memory ) ;
148- _handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( response , ApiJsonContext . Default . MemoryResponse ) ) ;
146+ handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( response , ApiJsonContext . Default . MemoryResponse ) ) ;
149147
150- var result = await _apiClient . GetMemoryAsync ( "session-123" , 0x10000 , 4 ) ;
148+ var result = await apiClient . GetMemoryAsync ( "session-123" , 0x10000 , 4 ) ;
151149
152150 _ = result . Should ( ) . Equal ( memory ) ;
153151 }
154152
155153 [ Fact ]
156154 public async Task AddBreakpointAsync_SendsCorrectRequest ( )
157155 {
158- _handler . SetResponse ( HttpStatusCode . OK , "{}" ) ;
156+ handler . SetResponse ( HttpStatusCode . OK , "{}" ) ;
159157
160- await _apiClient . AddBreakpointAsync ( "session-123" , 0x8000 ) ;
158+ await apiClient . AddBreakpointAsync ( "session-123" , 0x8000 ) ;
161159
162- _ = _handler . LastRequest ! . RequestUri ! . PathAndQuery . Should ( ) . Contain ( "/breakpoint" ) ;
163- _ = _handler . LastRequest . Method . Should ( ) . Be ( HttpMethod . Post ) ;
160+ _ = handler . LastRequest ! . RequestUri ! . PathAndQuery . Should ( ) . Contain ( "/breakpoint" ) ;
161+ _ = handler . LastRequest . Method . Should ( ) . Be ( HttpMethod . Post ) ;
164162 }
165163
166164 [ Fact ]
167165 public async Task AddWatchpointAsync_ReturnsWatchpoint ( )
168166 {
169167 var watchpoint = new Watchpoint ( 1 , 0x10000 , WatchpointType . Write ) ;
170- _handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( watchpoint , ApiJsonContext . Default . Watchpoint ) ) ;
168+ handler . SetResponse ( HttpStatusCode . OK , JsonSerializer . Serialize ( watchpoint , ApiJsonContext . Default . Watchpoint ) ) ;
171169
172- var result = await _apiClient . AddWatchpointAsync ( "session-123" , 0x10000 , WatchpointType . Write ) ;
170+ var result = await apiClient . AddWatchpointAsync ( "session-123" , 0x10000 , WatchpointType . Write ) ;
173171
174172 _ = result . Id . Should ( ) . Be ( 1 ) ;
175173 _ = result . Address . Should ( ) . Be ( 0x10000u ) ;
@@ -178,8 +176,8 @@ public async Task AddWatchpointAsync_ReturnsWatchpoint()
178176
179177 public void Dispose ( )
180178 {
181- _httpClient . Dispose ( ) ;
182- _handler . Dispose ( ) ;
179+ httpClient . Dispose ( ) ;
180+ handler . Dispose ( ) ;
183181 }
184182}
185183
@@ -188,35 +186,33 @@ public void Dispose()
188186/// </summary>
189187internal sealed class TestHttpMessageHandler : HttpMessageHandler
190188{
191- private HttpStatusCode _statusCode = HttpStatusCode . OK ;
192- private string _content = "{}" ;
193- private Exception ? _exception ;
189+ private HttpStatusCode statusCode = HttpStatusCode . OK ;
190+ private string content = "{}" ;
191+ private Exception ? exception ;
194192
195193 public HttpRequestMessage ? LastRequest { get ; private set ; }
196194
197195 public void SetResponse ( HttpStatusCode statusCode , string content )
198196 {
199- _statusCode = statusCode ;
200- _content = content ;
201- _exception = null ;
197+ this . statusCode = statusCode ;
198+ this . content = content ;
199+ exception = null ;
202200 }
203201
204202 public void SetException ( Exception exception )
205203 {
206- _exception = exception ;
204+ this . exception = exception ;
207205 }
208206
209207 protected override Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
210208 {
211209 LastRequest = request ;
212210
213- if ( _exception is not null ) {
214- throw _exception ;
211+ if ( exception is not null ) {
212+ throw exception ;
215213 }
216214
217- var response = new HttpResponseMessage ( _statusCode ) {
218- Content = new StringContent ( _content , Encoding . UTF8 , "application/json" )
219- } ;
215+ var response = new HttpResponseMessage ( statusCode ) { Content = new StringContent ( content , Encoding . UTF8 , "application/json" ) } ;
220216
221217 return Task . FromResult ( response ) ;
222218 }
0 commit comments