Search in sources :

Example 1 with JSON_UTF_8

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);
}
Also used : Response(io.airlift.http.client.Response) TestingResponse.mockResponse(io.airlift.http.client.testing.TestingResponse.mockResponse) HttpClient(io.airlift.http.client.HttpClient) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PublicKey(java.security.PublicKey) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) JSON_UTF_8(com.google.common.net.MediaType.JSON_UTF_8) Duration(io.airlift.units.Duration) HttpStatus(io.airlift.http.client.HttpStatus) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) DAYS(java.util.concurrent.TimeUnit.DAYS) Response(io.airlift.http.client.Response) Assert.assertTrue(org.testng.Assert.assertTrue) TestingResponse.mockResponse(io.airlift.http.client.testing.TestingResponse.mockResponse) URI(java.net.URI) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(io.airlift.units.Duration) Test(org.testng.annotations.Test)

Example 2 with JSON_UTF_8

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();
    }
}
Also used : HttpClient(io.airlift.http.client.HttpClient) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PublicKey(java.security.PublicKey) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) JSON_UTF_8(com.google.common.net.MediaType.JSON_UTF_8) Duration(io.airlift.units.Duration) HttpStatus(io.airlift.http.client.HttpStatus) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) DAYS(java.util.concurrent.TimeUnit.DAYS) Response(io.airlift.http.client.Response) Assert.assertTrue(org.testng.Assert.assertTrue) TestingResponse.mockResponse(io.airlift.http.client.testing.TestingResponse.mockResponse) URI(java.net.URI) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) Supplier(java.util.function.Supplier) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(io.airlift.units.Duration) Test(org.testng.annotations.Test)

Example 3 with JSON_UTF_8

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);
}
Also used : Response(io.airlift.http.client.Response) TestingResponse.mockResponse(io.airlift.http.client.testing.TestingResponse.mockResponse) HttpClient(io.airlift.http.client.HttpClient) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PublicKey(java.security.PublicKey) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) JSON_UTF_8(com.google.common.net.MediaType.JSON_UTF_8) Duration(io.airlift.units.Duration) HttpStatus(io.airlift.http.client.HttpStatus) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) DAYS(java.util.concurrent.TimeUnit.DAYS) Response(io.airlift.http.client.Response) Assert.assertTrue(org.testng.Assert.assertTrue) TestingResponse.mockResponse(io.airlift.http.client.testing.TestingResponse.mockResponse) URI(java.net.URI) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(io.airlift.units.Duration) Test(org.testng.annotations.Test)

Example 4 with JSON_UTF_8

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);
}
Also used : Response(io.airlift.http.client.Response) TestingResponse.mockResponse(io.airlift.http.client.testing.TestingResponse.mockResponse) HttpClient(io.airlift.http.client.HttpClient) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PublicKey(java.security.PublicKey) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) JSON_UTF_8(com.google.common.net.MediaType.JSON_UTF_8) Duration(io.airlift.units.Duration) HttpStatus(io.airlift.http.client.HttpStatus) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) DAYS(java.util.concurrent.TimeUnit.DAYS) Response(io.airlift.http.client.Response) Assert.assertTrue(org.testng.Assert.assertTrue) TestingResponse.mockResponse(io.airlift.http.client.testing.TestingResponse.mockResponse) URI(java.net.URI) TestingHttpClient(io.airlift.http.client.testing.TestingHttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(io.airlift.units.Duration) Test(org.testng.annotations.Test)

Aggregations

JSON_UTF_8 (com.google.common.net.MediaType.JSON_UTF_8)4 HttpClient (io.airlift.http.client.HttpClient)4 HttpStatus (io.airlift.http.client.HttpStatus)4 Response (io.airlift.http.client.Response)4 TestingHttpClient (io.airlift.http.client.testing.TestingHttpClient)4 TestingResponse.mockResponse (io.airlift.http.client.testing.TestingResponse.mockResponse)4 Duration (io.airlift.units.Duration)4 URI (java.net.URI)4 PublicKey (java.security.PublicKey)4 Map (java.util.Map)4 DAYS (java.util.concurrent.TimeUnit.DAYS)4 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Supplier (java.util.function.Supplier)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4 Assert.assertEquals (org.testng.Assert.assertEquals)4 Assert.assertTrue (org.testng.Assert.assertTrue)4 Test (org.testng.annotations.Test)4