AuthApiClient { @NonNull public Response<AuthToken> getToken(@NonNull final String channelId) throws RequestException { URL url = runtimeConfig.getUrlConfig() .deviceUrl() .appendEncodedPath(AUTH_PATH) .build(); String bearerToken; try { bearerToken = createBearerToken(channelId); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { throw new RequestException("Unable to create bearer token.", e); } final long requestTime = clock.currentTimeMillis(); return requestFactory.createRequest() .setOperation("GET", url) .setAirshipJsonAcceptsHeader() .setHeader("X-UA-App-Key", runtimeConfig.getConfigOptions().appKey) .setHeader("X-UA-Channel", channelId) .setHeader("Authorization", "Bearer " + bearerToken) .execute(new ResponseParser<AuthToken>() { @Override public AuthToken parseResponse(int status, @Nullable Map<String, List<String>> headers, @Nullable String responseBody) throws Exception { if (UAHttpStatusUtil.inSuccessRange(status)) { return AuthApiClient.parseResponse(responseBody, channelId, requestTime); } return null; } }); } AuthApiClient(AirshipRuntimeConfig runtimeConfig); @VisibleForTesting  AuthApiClient(AirshipRuntimeConfig runtimeConfig, Clock clock, RequestFactory requestFactory); @NonNull Response<AuthToken> getToken(@NonNull final String channelId);  }
@Test public void test200Response() throws RequestException { clock.currentTimeMillis = 100; testRequest.responseBody = JsonMap.newBuilder() .put("token", "some token") .put("expires_in", 300) .build().toString(); testRequest.responseStatus = 200; Response<AuthToken> response = client.getToken("some channel"); assertEquals(response.getResult().getChannelId(), "some channel"); assertEquals(response.getResult().getToken(), "some token"); assertEquals(response.getResult().getExpiration(), 400); }
@Test public void testRequest() throws RequestException { client.getToken("some channel"); assertEquals("https: assertEquals("GET", testRequest.getRequestMethod()); assertEquals("Bearer VWtkZq18HZM3GWzD/q27qPSVszysSyoQfQ6tDEAcAko=\n", testRequest.getRequestHeaders().get("Authorization")); assertEquals("appKey", testRequest.getRequestHeaders().get("X-UA-App-Key")); assertEquals("some channel", testRequest.getRequestHeaders().get("X-UA-Channel")); }
@Test(expected = RequestException.class) public void testInvalidResponse() throws RequestException { testRequest.responseBody = "what"; testRequest.responseStatus = 200; client.getToken("some channel"); }