Search in sources :

Example 56 with ClientRequestContext

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

the class JAXRSClientIT method getMediaTypeTest.

/*
   * @testName: getMediaTypeTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:442; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Get the media type of the entity. Returns: the media type
   * or null if not specified (e.g. there's no request entity).
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getMediaTypeTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            MediaType media = context.getMediaType();
            String entity = media == null ? "NULL" : media.toString();
            Response r = Response.ok(entity).build();
            context.abortWith(r);
        }
    };
    Entity<String> entity = Entity.entity("TEST", MediaType.APPLICATION_FORM_URLENCODED);
    Invocation invocation = buildBuilder(provider).buildPost(entity);
    Response response = invoke(invocation);
    String body = response.readEntity(String.class);
    assertContains(body, MediaType.APPLICATION_FORM_URLENCODED);
}
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 57 with ClientRequestContext

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

the class JAXRSClientIT method getAcceptableLanguagesTest.

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

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            List<Locale> locales = context.getAcceptableLanguages();
            String languages = JaxrsUtil.iterableToString(";", locales);
            Response r = Response.ok(languages).build();
            context.abortWith(r);
        }
    };
    Invocation.Builder builder = buildBuilder(provider);
    Invocation invocation;
    invocation = builder.acceptLanguage(Locale.CANADA_FRENCH).acceptLanguage(Locale.PRC).buildGet();
    Response response = invoke(invocation);
    String entity = response.readEntity(String.class);
    assertContains(entity, Locale.CANADA_FRENCH.toString());
    assertContains(entity, Locale.PRC.toString());
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Locale(java.util.Locale) Response(jakarta.ws.rs.core.Response) Invocation(jakarta.ws.rs.client.Invocation) Test(org.junit.jupiter.api.Test)

Example 58 with ClientRequestContext

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

the class JAXRSClientIT method getCookiesIsImmutableTest.

/*
   * @testName: getCookiesIsImmutableTest
   * 
   * @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 getCookiesIsImmutableTest() throws Fault {
    final Cookie cts = new Cookie("cts", "cts");
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            Map<String, Cookie> cookies = context.getCookies();
            try {
                cookies.put("test", cts);
            } catch (Exception e) {
            // either exception is thrown or put does nothing
            }
            cookies = context.getCookies();
            Cookie cookie = cookies.get("test");
            assertTrue(cookie == null, "getCookies is not read-only");
            Response r = Response.ok().build();
            context.abortWith(r);
        }
    };
    Invocation invocation = buildBuilder(provider).cookie(cts).buildGet();
    Response response = invoke(invocation);
    assertStatus(response, Status.OK);
}
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 59 with ClientRequestContext

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

the class JAXRSClientIT method getPropertyIsNullTest.

/*
   * @testName: getPropertyIsNullTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:444; JAXRS:JAVADOC:455; JAXRS:JAVADOC:456;
   * JAXRS:SPEC:85; JAXRS:JAVADOC:427;
   * 
   * @test_Strategy: Returns the property with the given name registered in the
   * current request/response exchange context, or null if there is no property
   * by that name.
   * 
   * ClientRequestFilter.abortWith
   */
@Test
public void getPropertyIsNullTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext context) throws Fault {
            Object property = context.getProperty("PROPERTY");
            String entity = property == null ? "NULL" : property.toString();
            Response r = Response.ok(entity).build();
            context.abortWith(r);
        }
    };
    Invocation invocation = buildBuilder(provider).buildGet();
    Response response = invoke(invocation);
    String body = response.readEntity(String.class);
    assertContains(body, "NULL");
}
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 60 with ClientRequestContext

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

the class JAXRSClientIT method hasEntityWhenEntityTest.

/*
   * @testName: hasEntityWhenEntityTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:474; JAXRS:JAVADOC:479; JAXRS:JAVADOC:480;
   * 
   * @test_Strategy: Check if there is a non-empty entity input stream is
   * available in the response message. ClientResponseFilter.filter
   */
@Test
public void hasEntityWhenEntityTest() throws Fault {
    final String entity = "eNtitY";
    ContextProvider in = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext requestContext, ClientResponseContext responseContext) throws Fault {
            boolean has = responseContext.hasEntity();
            assertTrue(has, "the #hasEntity did not found the given entity");
            logMsg("Found #hasEntity()=true");
        }
    };
    Response response = Response.ok(entity).build();
    invokeWithResponseAndAssertStatus(response, Status.OK, in);
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext) 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