Search in sources :

Example 71 with ClientRequestContext

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

the class JAXRSClientIT method getStringHeadersReflectsTheUnderlayingMapTest.

/*
   * @testName: getStringHeadersReflectsTheUnderlayingMapTest
   * 
   * @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. Changes in the underlying headers map are reflected in this view.
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getStringHeadersReflectsTheUnderlayingMapTest() throws Fault {
    final String TCK = "cts";
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            context.getHeaders().add(TCK, TCK);
            MultivaluedMap<String, String> map;
            map = context.getStringHeaders();
            String value = map.getFirst(TCK);
            Response r = Response.ok(value).build();
            context.abortWith(r);
        }
    };
    Invocation invocation = buildBuilder(provider).buildGet();
    Response response = invoke(invocation);
    String body = response.readEntity(String.class);
    assertContains(body, TCK);
}
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 72 with ClientRequestContext

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

the class JAXRSClientIT method getAcceptableMediaTypesTest.

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

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            List<MediaType> types = context.getAcceptableMediaTypes();
            String medias = JaxrsUtil.iterableToString(";", types);
            Response r = Response.ok(medias).build();
            context.abortWith(r);
        }
    };
    String media = "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5";
    Invocation.Builder builder = buildBuilder(provider);
    Invocation invocation = builder.header("Accept", media).buildGet();
    Response response = invoke(invocation);
    String entity = response.readEntity(String.class);
    assertContains(entity, "text/*");
    assertContains(entity, "text/html");
    assertContains(entity, "*/*");
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.jupiter.api.Test)

Example 73 with ClientRequestContext

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

the class JAXRSClientIT method getHeaderStringUsingHeaderDelegateTest.

/*
   * @testName: getHeaderStringUsingHeaderDelegateTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:440; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get a message header as a single string value. Each single
   * header value is converted to String using a RuntimeDelegate.HeaderDelegate.
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getHeaderStringUsingHeaderDelegateTest() throws Fault {
    final String name = "BEAN";
    final StringBean bean = new StringBean(name);
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            String value = context.getHeaderString(name);
            Response r = Response.ok(value).build();
            context.abortWith(r);
        }
    };
    RuntimeDelegate original = RuntimeDelegate.getInstance();
    RuntimeDelegate.setInstance(new StringBeanRuntimeDelegate(original));
    try {
        Invocation invocation = buildBuilder(provider).header(name, bean).buildGet();
        Response response = invoke(invocation);
        String body = response.readEntity(String.class);
        assertContains(name.toLowerCase(), body.toLowerCase());
    } finally {
        RuntimeDelegate.setInstance(original);
        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 74 with ClientRequestContext

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

the class JAXRSClientIT method getAcceptableMediaTypesIsImmutableTest.

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

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            List<MediaType> types = context.getAcceptableMediaTypes();
            try {
                types.add(MediaType.APPLICATION_JSON_TYPE);
            } catch (Exception e) {
            // either exception is thrown or add does nothing
            }
            types = context.getAcceptableMediaTypes();
            boolean b = types.contains(MediaType.APPLICATION_JSON_TYPE);
            assertTrue(!b, "getAcceptableMediaTypes is not read only");
            Response r = Response.ok().build();
            context.abortWith(r);
        }
    };
    String media = "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5";
    Invocation.Builder builder = buildBuilder(provider);
    Invocation invocation = builder.header("Accept", media).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) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.jupiter.api.Test)

Example 75 with ClientRequestContext

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

the class JAXRSClientIT method setCorruptedStream.

protected AtomicInteger setCorruptedStream() {
    final AtomicInteger ai = new AtomicInteger(0);
    ClientResponseFilter filter = new ClientResponseFilter() {

        @Override
        public void filter(ClientRequestContext arg0, ClientResponseContext response) throws IOException {
            CorruptedInputStream cis = new CorruptedInputStream(ResponseTest.ENTITY.getBytes(), ai);
            cis.setCorrupted(true);
            response.setEntityStream(cis);
        }
    };
    addProvider(filter);
    // do not use new entity stream in logging filter for the case of priority
    // disfunction, the CorruptedInputStream would be then replaced, wrongly
    // informing about not closing the stream
    super.setPrintEntity(false);
    return ai;
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientResponseFilter(jakarta.ws.rs.client.ClientResponseFilter) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext)

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