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);
}
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;
}
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));
}
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);
}
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));
}
Aggregations