Search in sources :

Example 26 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class TestUtil method stringYAMLFromResponse.

/**
 * Represents the HTTP response payload as a String.
 *
 * @param cnx the HttpURLConnection from which to get the response payload
 * @return String representation of the OpenAPI document as a String
 * @throws IOException in case of errors reading the HTTP response payload
 */
public static String stringYAMLFromResponse(HttpURLConnection cnx) throws IOException {
    MediaType returnedMediaType = mediaTypeFromResponse(cnx);
    assertTrue(MediaType.APPLICATION_OPENAPI_YAML.test(returnedMediaType), "Unexpected returned media type");
    return stringFromResponse(cnx, returnedMediaType);
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 27 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class TestUtil method validateResponseMediaType.

/**
 * Makes sure that the response is 200 and that the content type MediaType
 * is consistent with the expected one, returning the actual MediaType from
 * the response and leaving the payload ready for consumption.
 *
 * @param cnx {@code HttpURLConnection} with the response to validate
 * @param expectedMediaType {@code MediaType} with which the actual one
 * should be consistent
 * @return actual media type
 * @throws Exception in case of errors reading the content type from the
 * response
 */
public static MediaType validateResponseMediaType(HttpURLConnection cnx, MediaType expectedMediaType) throws Exception {
    assertEquals(Http.Status.OK_200.code(), cnx.getResponseCode(), "Unexpected response code");
    MediaType expectedMT = expectedMediaType != null ? expectedMediaType : OpenAPISupport.DEFAULT_RESPONSE_MEDIA_TYPE;
    MediaType actualMT = mediaTypeFromResponse(cnx);
    assertTrue(expectedMT.test(actualMT), "Expected response media type " + expectedMT.toString() + " but received " + actualMT.toString());
    return actualMT;
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 28 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class TestUtil method yamlFromResponse.

/**
 * Returns the response payload from the specified connection as a snakeyaml
 * {@code Yaml} object.
 *
 * @param cnx the {@code HttpURLConnection} containing the response
 * @return the YAML {@code Map<String, Object>} (created by snakeyaml) from
 * the HTTP response payload
 * @throws IOException in case of errors reading the response
 */
@SuppressWarnings(value = "unchecked")
public static Map<String, Object> yamlFromResponse(HttpURLConnection cnx) throws IOException {
    MediaType returnedMediaType = mediaTypeFromResponse(cnx);
    Yaml yaml = new Yaml();
    Charset cs = Charset.defaultCharset();
    if (returnedMediaType.charset().isPresent()) {
        cs = Charset.forName(returnedMediaType.charset().get());
    }
    return (Map<String, Object>) yaml.load(new InputStreamReader(cnx.getInputStream(), cs));
}
Also used : InputStreamReader(java.io.InputStreamReader) MediaType(io.helidon.common.http.MediaType) Charset(java.nio.charset.Charset) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml)

Example 29 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class TestUtil method stringYAMLFromResponse.

/**
 * Represents the HTTP response payload as a String.
 *
 * @param cnx the HttpURLConnection from which to get the response payload
 * @return String representation of the OpenAPI document as a String
 * @throws IOException in case of errors reading the HTTP response payload
 */
public static String stringYAMLFromResponse(HttpURLConnection cnx) throws IOException {
    MediaType returnedMediaType = mediaTypeFromResponse(cnx);
    assertTrue(MediaType.APPLICATION_OPENAPI_YAML.test(returnedMediaType), "Unexpected returned media type");
    return stringFromResponse(cnx, returnedMediaType);
}
Also used : MediaType(io.helidon.common.http.MediaType)

Example 30 with MediaType

use of io.helidon.common.http.MediaType in project helidon by oracle.

the class TestUtil method configFromResponse.

/**
 * Represents an OpenAPI document HTTP response as a {@code Config} instance
 * to simplify access to deeply-nested values.
 *
 * @param cnx the HttpURLConnection which already has the response to
 * process
 * @return Config representing the OpenAPI document content
 * @throws IOException in case of errors reading the returned payload as
 * config
 */
public static Config configFromResponse(HttpURLConnection cnx) throws IOException {
    MediaType mt = mediaTypeFromResponse(cnx);
    String configMT = MediaType.APPLICATION_OPENAPI_YAML.test(mt) ? YamlConfigParser.MEDIA_TYPE_APPLICATION_YAML : MediaType.APPLICATION_JSON.toString();
    String yaml = stringYAMLFromResponse(cnx);
    return Config.create(ConfigSources.create(yaml, configMT));
}
Also used : MediaType(io.helidon.common.http.MediaType)

Aggregations

MediaType (io.helidon.common.http.MediaType)37 Charset (java.nio.charset.Charset)6 Map (java.util.Map)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 GenericType (io.helidon.common.GenericType)5 DataChunk (io.helidon.common.http.DataChunk)5 StandardCharsets (java.nio.charset.StandardCharsets)5 Multi (io.helidon.common.reactive.Multi)4 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)4 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)4 Objects (java.util.Objects)4 Flow (java.util.concurrent.Flow)4 Http (io.helidon.common.http.Http)3 JsonStructureToChunks (io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)3 List (java.util.List)3 Single (io.helidon.common.reactive.Single)2 RoutingChecker (io.helidon.webserver.RoutingTest.RoutingChecker)2 RoutingTest.mockResponse (io.helidon.webserver.RoutingTest.mockResponse)2 JsonString (jakarta.json.JsonString)2 Jsonb (jakarta.json.bind.Jsonb)2