@@ -154,7 +154,6 @@ public void fetchTokensShouldWorkCorrectly() throws TagMyCodeException {
154154 @ Test
155155 public void refreshOauthTokenReceiveNewAccessToken () throws Exception {
156156 stubFor (post (urlMatching ("/oauth2/token.*" ))
157- .withRequestBody ((matching (".*refresh_token.*" )))
158157 .willReturn (aResponse ()
159158 .withStatus (200 )
160159 .withHeader ("Content-Type" , "text/plain" )
@@ -168,22 +167,24 @@ public void refreshOauthTokenReceiveNewAccessToken() throws Exception {
168167 }
169168
170169 @ Test
171- public void refreshOauthTokenWithInvalidTokenThrowsException () throws Exception {
170+ public void failedRefreshTokenThrownTagMyCodeUnauthorizedException () {
172171 stubFor (post (urlMatching ("/oauth2/token.*" ))
173- .withRequestBody ((matching (".*refresh_token.*" )))
174172 .willReturn (aResponse ()
175173 .withStatus (401 )
176174 .withHeader ("Content-Type" , "text/plain" )
177175 .withBody ("{\" error\" :\" invalid_grant\" ,\" error_description\" :\" Invalid refresh token\" }"
178176 )));
179- assertEquals (new OauthToken ("xxx" , "yyy" ), client .getOauthToken ());
177+ Client spyClient = spy (client );
178+ assertEquals (new OauthToken ("xxx" , "yyy" ), spyClient .getOauthToken ());
180179
181180 try {
182- client .refreshOauthToken ();
181+ spyClient .refreshOauthToken ();
183182 fail ("Expected exception" );
184- } catch (TagMyCodeException ignore ) {
183+ } catch (TagMyCodeException e ) {
184+ assertTrue (e instanceof TagMyCodeUnauthorizedException );
185185 }
186- assertEquals (new OauthToken ("xxx" , "yyy" ), client .getOauthToken ());
186+ assertEquals (new OauthToken ("xxx" , "yyy" ), spyClient .getOauthToken ());
187+ verify (spyClient , times (1 )).responseIsUnauthorized (anyString ());
187188 }
188189
189190 @ Test
@@ -196,7 +197,6 @@ public void expiredAccessTokenShouldBeRefreshed() throws Exception {
196197 )));
197198
198199 stubFor (post (urlMatching ("/oauth2/token.*" ))
199- .withRequestBody ((matching (".*refresh_token.*" )))
200200 .willReturn (aResponse ()
201201 .withStatus (200 )
202202 .withHeader ("Content-Type" , "text/plain" )
@@ -213,26 +213,12 @@ public void expiredAccessTokenShouldBeRefreshed() throws Exception {
213213 }
214214
215215 @ Test
216- public void failedRefreshTokenThrownTagMyCodeUnauthorizedException () throws TagMyCodeException {
217- stubFor (get (urlMatching ("/account.*" ))
218- .willReturn (aResponse ()
219- .withStatus (401 )
220- .withHeader ("Content-Type" , "text/plain" )
221- .withBody (""
222- )));
216+ public void testResponseIsUnauthorized () {
217+ assertTrue (client .responseIsUnauthorized ("{\" error\" :\" invalid_grant\" ,\" error_description\" :\" Invalid refresh token\" }" ));
218+ assertTrue (client .responseIsUnauthorized ("message contains refresh_token " ));
223219
224- stubFor (post (urlMatching ("/oauth2/token.*" ))
225- .withRequestBody ((matching (".*refresh_token.*" )))
226- .willReturn (aResponse ()
227- .withStatus (401 )
228- .withHeader ("Content-Type" , "text/plain" )
229- .withBody ("{}"
230- )));
231- try {
232- new TagMyCode (client ).fetchAccount ();
233- fail ("Expected exception" );
234- } catch (TagMyCodeException ignored ) {
235- }
220+ assertFalse (client .responseIsUnauthorized ("" ));
221+ assertFalse (client .responseIsUnauthorized ("{}" ));
236222 }
237223
238224 @ Test
0 commit comments