Search in sources :

Example 6 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method getStringHeadersTest.

/*
   * @testName: getStringHeadersTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:446; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get a string view of header values associated with the
   * message.
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getStringHeadersTest() throws Fault {
    final String TCK = "cts";
    final String DATE = "Tue, 15 Nov 1994 08:12:31 GMT";
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            MultivaluedMap<String, String> map;
            map = context.getStringHeaders();
            StringBuilder value = new StringBuilder();
            value.append(map.getFirst("Accept")).append(" ");
            value.append(map.getFirst("tck")).append(" ");
            value.append(map.getFirst("Date"));
            Response r = Response.ok(value.toString()).build();
            context.abortWith(r);
        }
    };
    Invocation invocation = buildBuilder(provider).header("Accept", MediaType.TEXT_HTML).header("tck", TCK).header("Date", DATE).buildGet();
    Response response = invoke(invocation);
    String body = response.readEntity(String.class);
    assertContains(body, MediaType.TEXT_HTML);
    assertContains(body, TCK);
    assertContains(body, DATE);
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) Test(org.junit.jupiter.api.Test)

Example 7 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method getUriTest.

/*
   * @testName: getUriTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:447; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get the request URI.
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getUriTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            URI uri = context.getUri();
            String entity = uri.toASCIIString();
            Response r = Response.ok(entity).build();
            context.abortWith(r);
        }
    };
    Invocation invocation = buildInvocation(provider);
    Response response = invoke(invocation);
    String body = response.readEntity(String.class);
    assertContains(body, getUrl());
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 8 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method getAcceptableLanguagesIsImmutableTest.

/*
   * @testName: getAcceptableLanguagesIsImmutableTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:428; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: a read-only list of requested response media types sorted
   * according to their q-value, with highest preference first.
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getAcceptableLanguagesIsImmutableTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            List<Locale> locales = context.getAcceptableLanguages();
            try {
                locales.add(Locale.JAPAN);
            } catch (Exception e) {
            // either exception is thrown, or add does nothing
            }
            locales = context.getAcceptableLanguages();
            boolean b = locales.contains(Locale.JAPAN);
            assertTrue(!b, "getAcceptableLanguages is not read-only");
            Response r = Response.ok().build();
            context.abortWith(r);
        }
    };
    WebTarget target = buildTarget(provider);
    Invocation.Builder builder = target.request();
    Invocation invocation;
    invocation = builder.header("Accept-Language", "da, en-gb;q=0.6, en-us;q=0.7").buildGet();
    Response response = invoke(invocation);
    assertStatus(response, Status.OK);
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Locale(java.util.Locale) Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.jupiter.api.Test)

Example 9 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method getStringHeadersUsingHeaderDelegateTest.

/*
   * @testName: getStringHeadersUsingHeaderDelegateTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:446; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get a string view of header values associated with the
   * message. The method converts the non-string header values to strings using
   * a RuntimeDelegate.HeaderDelegate
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getStringHeadersUsingHeaderDelegateTest() throws Fault {
    final String TCK = "cts";
    final StringBean bean = new StringBean(TCK);
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            MultivaluedMap<String, String> map;
            map = context.getStringHeaders();
            StringBuilder value = new StringBuilder();
            value.append(map.getFirst(TCK));
            Response r = Response.ok(value.toString()).build();
            context.abortWith(r);
        }
    };
    RuntimeDelegate delegate = RuntimeDelegate.getInstance();
    RuntimeDelegate.setInstance(new StringBeanRuntimeDelegate(delegate));
    try {
        Invocation invocation = buildBuilder(provider).header(TCK, bean).buildGet();
        Response response = invoke(invocation);
        String body = response.readEntity(String.class);
        assertContains(body, TCK);
    } finally {
        RuntimeDelegate.setInstance(delegate);
        StringBeanRuntimeDelegate.assertNotStringBeanRuntimeDelegate();
    }
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) StringBeanRuntimeDelegate(ee.jakarta.tck.ws.rs.common.provider.StringBeanRuntimeDelegate) Invocation(jakarta.ws.rs.client.Invocation) StringBean(ee.jakarta.tck.ws.rs.common.provider.StringBean) RuntimeDelegate(jakarta.ws.rs.ext.RuntimeDelegate) StringBeanRuntimeDelegate(ee.jakarta.tck.ws.rs.common.provider.StringBeanRuntimeDelegate) Test(org.junit.jupiter.api.Test)

Example 10 with ClientRequestContext

use of jakarta.ws.rs.client.ClientRequestContext in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method getClientTest.

/*
   * @testName: getClientTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:430; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get the client instance associated with the request.
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getClientTest() throws Fault {
    final Client client = ClientBuilder.newClient();
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            Client contextClient = context.getClient();
            assertTrue(client == contextClient, "the client instance is different from the context one");
            Response r = Response.ok().build();
            context.abortWith(r);
        }
    };
    client.register(provider);
    WebTarget target = client.target(getUrl());
    Invocation invocation = target.request().buildGet();
    Response response = invoke(invocation);
    assertStatus(response, Status.OK);
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) WebTarget(jakarta.ws.rs.client.WebTarget) JAXRSCommonClient(ee.jakarta.tck.ws.rs.common.JAXRSCommonClient) Client(jakarta.ws.rs.client.Client) Test(org.junit.jupiter.api.Test)

Aggregations

ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)77 Response (jakarta.ws.rs.core.Response)71 Test (org.junit.jupiter.api.Test)70 Invocation (jakarta.ws.rs.client.Invocation)38 ClientResponseContext (jakarta.ws.rs.client.ClientResponseContext)29 ClientRequestFilter (jakarta.ws.rs.client.ClientRequestFilter)10 MediaType (jakarta.ws.rs.core.MediaType)8 Locale (java.util.Locale)6 WebTarget (jakarta.ws.rs.client.WebTarget)5 ContextProvider (ee.jakarta.tck.ws.rs.api.client.clientrequestcontext.ContextProvider)4 StringBean (ee.jakarta.tck.ws.rs.common.provider.StringBean)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Date (java.util.Date)4 JAXRSCommonClient (ee.jakarta.tck.ws.rs.common.JAXRSCommonClient)3 StringBeanRuntimeDelegate (ee.jakarta.tck.ws.rs.common.provider.StringBeanRuntimeDelegate)3 StringBeanWithAnnotation (ee.jakarta.tck.ws.rs.common.provider.StringBeanWithAnnotation)3 Client (jakarta.ws.rs.client.Client)3 Link (jakarta.ws.rs.core.Link)3 RuntimeDelegate (jakarta.ws.rs.ext.RuntimeDelegate)3 IOException (java.io.IOException)3