use of com.google.common.net.MediaType.JSON_UTF_8 in project airlift by airlift.
the class TestJwksService method testRequestFailure.
@Test
public void testRequestFailure() {
AtomicReference<Response> response = new AtomicReference<>(mockResponse(HttpStatus.OK, JSON_UTF_8, TEST_JWKS_RESPONSE));
JwksService service = new JwksService(URI.create("http://example.com"), new TestingHttpClient(request -> {
Response value = response.get();
if (value == null) {
throw new IllegalArgumentException("test");
}
return value;
}), new Duration(1, DAYS));
assertTestKeys(service);
// request failure
response.set(null);
assertThatThrownBy(service::refreshKeys).hasMessage("Error reading JWKS keys from http://example.com").isInstanceOf(RuntimeException.class);
assertTestKeys(service);
// valid update
response.set(mockResponse(HttpStatus.OK, JSON_UTF_8, EMPTY_KEYS));
service.refreshKeys();
assertEmptyKeys(service);
}
use of com.google.common.net.MediaType.JSON_UTF_8 in project airlift by airlift.
the class TestJwksService method testTimedReload.
@Test
public void testTimedReload() throws InterruptedException {
AtomicReference<Supplier<Response>> response = new AtomicReference<>(() -> mockResponse(HttpStatus.OK, JSON_UTF_8, EMPTY_KEYS));
JwksService service = new JwksService(URI.create("http://example.com"), new TestingHttpClient(request -> response.get().get()), new Duration(1, MILLISECONDS));
assertEmptyKeys(service);
try {
// start service
response.set(() -> mockResponse(HttpStatus.OK, JSON_UTF_8, EMPTY_KEYS));
service.start();
while (!service.getKeys().isEmpty()) {
// noinspection BusyWait
Thread.sleep(1000);
}
response.set(() -> mockResponse(HttpStatus.OK, JSON_UTF_8, TEST_JWKS_RESPONSE));
while (service.getKeys().isEmpty()) {
// noinspection BusyWait
Thread.sleep(1000);
}
assertTestKeys(service);
} finally {
service.stop();
}
}
use of com.google.common.net.MediaType.JSON_UTF_8 in project airlift by airlift.
the class TestJwksService method testReload.
@Test
public void testReload() {
AtomicReference<Response> response = new AtomicReference<>(mockResponse(HttpStatus.OK, JSON_UTF_8, EMPTY_KEYS));
JwksService service = new JwksService(URI.create("http://example.com"), new TestingHttpClient(request -> response.get()), new Duration(1, DAYS));
assertEmptyKeys(service);
response.set(mockResponse(HttpStatus.OK, JSON_UTF_8, EMPTY_KEYS));
service.refreshKeys();
assertEmptyKeys(service);
response.set(mockResponse(HttpStatus.OK, JSON_UTF_8, TEST_JWKS_RESPONSE));
service.refreshKeys();
assertTestKeys(service);
response.set(mockResponse(HttpStatus.OK, JSON_UTF_8, TEST_JWKS_RESPONSE));
service.refreshKeys();
assertTestKeys(service);
response.set(mockResponse(HttpStatus.OK, JSON_UTF_8, EMPTY_KEYS));
service.refreshKeys();
assertEmptyKeys(service);
}
use of com.google.common.net.MediaType.JSON_UTF_8 in project airlift by airlift.
the class TestJwksService method testBadResponse.
@Test
public void testBadResponse() {
AtomicReference<Response> response = new AtomicReference<>(mockResponse(HttpStatus.OK, JSON_UTF_8, TEST_JWKS_RESPONSE));
JwksService service = new JwksService(URI.create("http://example.com"), new TestingHttpClient(request -> response.get()), new Duration(1, DAYS));
assertTestKeys(service);
// bad response code document
response.set(mockResponse(HttpStatus.CREATED, JSON_UTF_8, ""));
assertThatThrownBy(service::refreshKeys).hasMessage("Unexpected response code 201 from JWKS service at http://example.com").isInstanceOf(RuntimeException.class);
assertTestKeys(service);
// empty document
response.set(mockResponse(HttpStatus.OK, JSON_UTF_8, ""));
assertThatThrownBy(service::refreshKeys).hasMessage("Unable to decode JWKS response from http://example.com").isInstanceOf(RuntimeException.class);
assertTestKeys(service);
// valid update
response.set(mockResponse(HttpStatus.OK, JSON_UTF_8, EMPTY_KEYS));
service.refreshKeys();
assertEmptyKeys(service);
}
Aggregations