Skip to content

Commit 4b85eb8

Browse files
authored
Add unit test for Unauthenticated error unit. (#2229)
1 parent b485649 commit 4b85eb8

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

firebase-ml-modeldownloader/src/test/java/com/google/firebase/ml/modeldownloader/internal/CustomModelDownloadServiceTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,5 +266,37 @@ public void downloadService_modelNotFound() throws Exception {
266266
equalTo(INSTALLATION_TOKEN)));
267267
}
268268

269-
// TODO(annz) add test matching bad token response when BE is ready.
269+
@Test
270+
public void downloadService_unauthenticatedToken() throws Exception {
271+
String downloadPath =
272+
String.format(CustomModelDownloadService.DOWNLOAD_MODEL_REGEX, "", PROJECT_ID, MODEL_NAME);
273+
stubFor(
274+
get(urlEqualTo(downloadPath))
275+
.withHeader(
276+
CustomModelDownloadService.INSTALLATIONS_AUTH_TOKEN_HEADER,
277+
equalTo(INSTALLATION_TOKEN))
278+
.withHeader(
279+
CustomModelDownloadService.CONTENT_TYPE,
280+
equalTo(CustomModelDownloadService.APPLICATION_JSON))
281+
.withHeader(CustomModelDownloadService.IF_NONE_MATCH_HEADER_KEY, equalTo(MODEL_HASH))
282+
.willReturn(
283+
aResponse()
284+
.withStatus(401) // not found
285+
.withBody(
286+
"{\"status\":\"UNAUTHENTICATED\",\"message\":\"Request is missing required authentication credential.\"}")));
287+
288+
CustomModelDownloadService service =
289+
new CustomModelDownloadService(
290+
installationsApiMock, directExecutor, API_KEY, TEST_ENDPOINT);
291+
292+
Task<CustomModel> modelTask = service.getCustomModelDetails(PROJECT_ID, MODEL_NAME, MODEL_HASH);
293+
294+
Assert.assertTrue(modelTask.getException().getMessage().contains("401"));
295+
296+
verify(
297+
getRequestedFor(urlEqualTo(downloadPath))
298+
.withHeader(
299+
CustomModelDownloadService.INSTALLATIONS_AUTH_TOKEN_HEADER,
300+
equalTo(INSTALLATION_TOKEN)));
301+
}
270302
}

0 commit comments

Comments
 (0)