Search in sources :

Example 1 with ClientRequestContext

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

the class JAXRSClientIT method abortWithTest.

/*
   * @testName: abortWithTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:427; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85;
   * 
   * @test_Strategy: Abort the filter chain with a response. This method breaks
   * the filter chain processing and returns the provided response back to the
   * client. The provided response goes through the chain of applicable response
   * filters.
   * 
   * ClientRequestFilter.filter ClientRequestFilter.abortWith
   */
@Test
public void abortWithTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            Response r = Response.status(Status.CREATED).build();
            context.abortWith(r);
        }
    };
    Invocation i = buildInvocation(provider);
    Response r = invoke(i);
    assertStatus(r, Status.CREATED);
}
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 2 with ClientRequestContext

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

the class JAXRSClientIT method getCookiesTest.

/*
   * @testName: getCookiesTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:432; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get any cookies that accompanied the request. Returns a
   * read-only map of cookie name (String) to Cookie.
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getCookiesTest() throws Fault {
    Cookie cts = new Cookie("cts", "cts");
    Cookie tck = new Cookie("tck", "tck");
    Cookie jee = new Cookie("jee", "jee");
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            String cookies = JaxrsUtil.iterableToString(";", context.getCookies().values());
            Response r = Response.ok(cookies).build();
            context.abortWith(r);
        }
    };
    Invocation invocation = buildBuilder(provider).cookie(cts).cookie(tck).cookie(jee).buildGet();
    Response response = invoke(invocation);
    String entity = response.readEntity(String.class);
    assertContains(entity, "cts");
    assertContains(entity, "tck");
    assertContains(entity, "jee");
}
Also used : Cookie(jakarta.ws.rs.core.Cookie) 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 3 with ClientRequestContext

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

the class JAXRSClientIT method getEntityAnnotationsTest.

/*
   * @testName: getEntityAnnotationsTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:435; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get the annotations attached to the entity. Note that the
   * returned annotations array contains only those annotations explicitly
   * attached to entity instance (such as the ones attached using
   * Entity.Entity(Object, jakarta.ws.rs.core.MediaType,
   * java.lang.annotation.Annotation[]) method).
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getEntityAnnotationsTest() throws Fault {
    Annotation[] annotations = ContextProvider.class.getAnnotations();
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            Annotation[] annotations = context.getEntityAnnotations();
            String first = annotations == null ? "NULL" : annotations.length == 0 ? "0" : annotations[0].annotationType().getName();
            Response r = Response.ok(first).build();
            context.abortWith(r);
        }
    };
    Entity<String> post = Entity.entity("test", MediaType.WILDCARD_TYPE, annotations);
    Invocation invocation = buildBuilder(provider).buildPost(post);
    Response response = invoke(invocation);
    String entity = response.readEntity(String.class);
    assertContains(entity, annotations[0].annotationType().getName());
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) Annotation(java.lang.annotation.Annotation) StringBeanWithAnnotation(ee.jakarta.tck.ws.rs.common.provider.StringBeanWithAnnotation) Test(org.junit.jupiter.api.Test)

Example 4 with ClientRequestContext

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

the class JAXRSClientIT method getAcceptableMediaTypesIsSortedTest.

/*
   * @testName: getAcceptableMediaTypesIsSortedTest
   * 
   * @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 getAcceptableMediaTypesIsSortedTest() 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/plain;q=0.3, text/html;q=0.7, text/xml;level=1, text/java;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).toLowerCase();
    int indexXml = entity.indexOf(MediaType.TEXT_XML);
    int indexHtml = entity.indexOf(MediaType.TEXT_HTML);
    int indexAny = entity.indexOf(MediaType.WILDCARD);
    int indexJava = entity.indexOf("text/java");
    int indexPlain = entity.indexOf(MediaType.TEXT_PLAIN);
    assertTrue(indexXml < indexHtml && indexHtml < indexAny && indexAny < indexJava && indexJava < indexPlain, "Media Types " + entity + " are not sorted");
}
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 5 with ClientRequestContext

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

the class JAXRSClientIT method getDateTest.

/*
   * @testName: getDateTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:433; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get message date. Returns: the message date, otherwise null
   * if not present.
   *
   * ClientRequestFilter.abortWith
   */
@Test
public void getDateTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            Date date = context.getDate();
            Response r = Response.ok(date.toString()).build();
            context.abortWith(r);
        }
    };
    Invocation invocation = buildBuilder(provider).header("Date", "Tue, 15 Nov 1994 08:12:31 GMT").buildGet();
    Response response = invoke(invocation);
    String entity = response.readEntity(String.class);
    assertContains(entity, "Nov");
    assertContains(entity, "1994");
    assertContains(entity, "31");
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) Date(java.util.Date) 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