@@ -226,6 +226,87 @@ void testExecutePost_ConnectException() throws Exception {
226226 assertEquals ("Cannot connect to https://example.com/resource" , exception .getMessage ());
227227 }
228228
229+ @ Test
230+ void testExecuteGet_OauthErrorInSuccessResponse () throws Exception {
231+ // Given
232+ String url = "https://example.com/token" ;
233+ String errorResponse = "{\" error\" :\" invalid_grant\" ,\" error_description\" :\" The authorization code has expired\" }" ;
234+ HttpResponse <String > httpResponseMock = mock (HttpResponse .class );
235+ when (httpClientMock .send (any (HttpRequest .class ), any (HttpResponse .BodyHandler .class ))).thenReturn (httpResponseMock );
236+ when (httpResponseMock .statusCode ()).thenReturn (200 );
237+ when (httpResponseMock .body ()).thenReturn (errorResponse );
238+
239+ // When
240+ HttpException exception = assertThrows (HttpException .class ,
241+ () -> resourceAuthorizationClient .executeGet (url , TestResponse .class ));
242+
243+ // Then
244+ assertEquals (400 , exception .getStatus ().getCode ());
245+ assertTrue (exception .getMessage ().contains ("invalid_grant" ));
246+ assertTrue (exception .getMessage ().contains ("The authorization code has expired" ));
247+ }
248+
249+ @ Test
250+ void testExecutePost_OauthErrorInSuccessResponse () throws Exception {
251+ // Given
252+ String url = "https://example.com/token" ;
253+ TestRequest requestPayload = new TestRequest ("testValue" );
254+ String errorResponse = "{\" error\" :\" invalid_client\" ,\" error_description\" :\" Invalid redirect_uri\" }" ;
255+ HttpResponse <String > httpResponseMock = mock (HttpResponse .class );
256+ when (httpClientMock .send (any (HttpRequest .class ), any (HttpResponse .BodyHandler .class ))).thenReturn (httpResponseMock );
257+ when (httpResponseMock .statusCode ()).thenReturn (200 );
258+ when (httpResponseMock .body ()).thenReturn (errorResponse );
259+
260+ // When
261+ HttpException exception = assertThrows (HttpException .class ,
262+ () -> resourceAuthorizationClient .executePost (url , requestPayload ,
263+ ContentType .APPLICATION_JSON .toString (), TestResponse .class ));
264+
265+ // Then
266+ assertEquals (400 , exception .getStatus ().getCode ());
267+ assertTrue (exception .getMessage ().contains ("invalid_client" ));
268+ assertTrue (exception .getMessage ().contains ("Invalid redirect_uri" ));
269+ }
270+
271+ @ Test
272+ void testExecutePost_OauthErrorWithoutDescription () throws Exception {
273+ // Given
274+ String url = "https://example.com/token" ;
275+ TestRequest requestPayload = new TestRequest ("testValue" );
276+ String errorResponse = "{\" error\" :\" server_error\" }" ;
277+ HttpResponse <String > httpResponseMock = mock (HttpResponse .class );
278+ when (httpClientMock .send (any (HttpRequest .class ), any (HttpResponse .BodyHandler .class ))).thenReturn (httpResponseMock );
279+ when (httpResponseMock .statusCode ()).thenReturn (200 );
280+ when (httpResponseMock .body ()).thenReturn (errorResponse );
281+
282+ // When
283+ HttpException exception = assertThrows (HttpException .class ,
284+ () -> resourceAuthorizationClient .executePost (url , requestPayload ,
285+ ContentType .APPLICATION_JSON .toString (), TestResponse .class ));
286+
287+ // Then
288+ assertEquals (400 , exception .getStatus ().getCode ());
289+ assertTrue (exception .getMessage ().contains ("server_error" ));
290+ assertTrue (exception .getMessage ().contains ("no description" ));
291+ }
292+
293+ @ Test
294+ void testExecuteGet_ValidResponseNotTreatedAsOauthError () throws Exception {
295+ String url = "https://example.com/resource" ;
296+ String jsonResponse = "{\" key\" :\" value\" }" ;
297+ HttpResponse <String > httpResponseMock = mock (HttpResponse .class );
298+ when (httpClientMock .send (any (HttpRequest .class ), any (HttpResponse .BodyHandler .class ))).thenReturn (httpResponseMock );
299+ when (httpResponseMock .statusCode ()).thenReturn (200 );
300+ when (httpResponseMock .body ()).thenReturn (jsonResponse );
301+
302+ // When
303+ TestResponse actualResponse = resourceAuthorizationClient .executeGet (url , TestResponse .class );
304+
305+ // Then
306+ assertNotNull (actualResponse );
307+ assertEquals ("value" , actualResponse .getKey ());
308+ }
309+
229310 @ Data
230311 @ AllArgsConstructor
231312 @ NoArgsConstructor
0 commit comments