mirror of
https://github.com/hapifhir/org.hl7.fhir.core.git
synced 2025-02-10 06:44:44 +00:00
Apply changes to r4b
This commit is contained in:
parent
cbafe2a28f
commit
d8ca1cc144
@ -19,24 +19,20 @@ public class Client {
|
||||
|
||||
public static final String DEFAULT_CHARSET = "UTF-8";
|
||||
private static final long DEFAULT_TIMEOUT = 5000;
|
||||
@Getter
|
||||
@Getter @Setter
|
||||
private ToolingClientLogger logger;
|
||||
private FhirLoggingInterceptor fhirLoggingInterceptor;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private int retryCount;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private long timeout = DEFAULT_TIMEOUT;
|
||||
//private byte[] payload;
|
||||
|
||||
@Setter @Getter
|
||||
private String base;
|
||||
|
||||
public void setLogger(ToolingClientLogger logger) {
|
||||
this.logger = logger;
|
||||
this.fhirLoggingInterceptor = new FhirLoggingInterceptor(logger);
|
||||
}
|
||||
|
||||
public <T extends Resource> ResourceRequest<T> issueOptionsRequest(URI optionsUri, String resourceFormat,
|
||||
String message, long timeout) throws IOException {
|
||||
/*FIXME delete once refactor is done
|
||||
@ -163,7 +159,7 @@ public class Client {
|
||||
|
||||
public <T extends Resource> Bundle executeBundleRequest(HTTPRequest request, String resourceFormat,
|
||||
Iterable<HTTPHeader> headers, String message, int retryCount, long timeout) throws IOException {
|
||||
return new FhirRequestBuilder(request, base).withLogger(fhirLoggingInterceptor).withResourceFormat(resourceFormat)
|
||||
return new FhirRequestBuilder(request, base).withLogger(logger).withResourceFormat(resourceFormat)
|
||||
.withRetryCount(retryCount).withMessage(message)
|
||||
.withHeaders(headers == null ? Collections.emptyList() : headers)
|
||||
.withTimeout(timeout, TimeUnit.MILLISECONDS).executeAsBatch();
|
||||
@ -171,7 +167,7 @@ public class Client {
|
||||
|
||||
public <T extends Resource> ResourceRequest<T> executeFhirRequest(HTTPRequest request, String resourceFormat,
|
||||
Iterable<HTTPHeader> headers, String message, int retryCount, long timeout) throws IOException {
|
||||
return new FhirRequestBuilder(request, base).withLogger(fhirLoggingInterceptor).withResourceFormat(resourceFormat)
|
||||
return new FhirRequestBuilder(request, base).withLogger(logger).withResourceFormat(resourceFormat)
|
||||
.withRetryCount(retryCount).withMessage(message)
|
||||
.withHeaders(headers == null ? Collections.emptyList() : headers)
|
||||
.withTimeout(timeout, TimeUnit.MILLISECONDS).execute();
|
||||
|
@ -1,69 +0,0 @@
|
||||
package org.hl7.fhir.r4b.utils.client.network;
|
||||
|
||||
import okhttp3.*;
|
||||
import okio.Buffer;
|
||||
|
||||
import org.hl7.fhir.utilities.ToolingClientLogger;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class FhirLoggingInterceptor implements Interceptor {
|
||||
|
||||
private ToolingClientLogger logger;
|
||||
|
||||
public FhirLoggingInterceptor(ToolingClientLogger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public FhirLoggingInterceptor setLogger(ToolingClientLogger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(@Nonnull Interceptor.Chain chain) throws IOException {
|
||||
// Log Request
|
||||
Request request = chain.request();
|
||||
List<String> hdrs = new ArrayList<>();
|
||||
for (String s : request.headers().toString().split("\\n")) {
|
||||
hdrs.add(s.trim());
|
||||
}
|
||||
byte[] cnt = null;
|
||||
if (request.body() != null) {
|
||||
Buffer buf = new Buffer();
|
||||
request.body().writeTo(buf);
|
||||
cnt = buf.readByteArray();
|
||||
}
|
||||
if (logger != null) {
|
||||
logger.logRequest(request.method(), request.url().toString(), hdrs, cnt);
|
||||
}
|
||||
|
||||
// Log Response
|
||||
Response response = null;
|
||||
response = chain.proceed(chain.request());
|
||||
|
||||
MediaType contentType = null;
|
||||
byte[] bodyBytes = null;
|
||||
if (response.body() != null) {
|
||||
contentType = response.body().contentType();
|
||||
bodyBytes = response.body().bytes();
|
||||
}
|
||||
|
||||
// Get Headers as List
|
||||
List<String> headerList = new ArrayList<>();
|
||||
Map<String, List<String>> headerMap = response.headers().toMultimap();
|
||||
headerMap.keySet().forEach(key -> headerMap.get(key).forEach(value -> headerList.add(key + ":" + value)));
|
||||
|
||||
if (logger != null) {
|
||||
logger.logResponse(Integer.toString(response.code()), headerList, bodyBytes, 0);
|
||||
}
|
||||
|
||||
// Reading byte[] clears body. Need to recreate.
|
||||
ResponseBody body = ResponseBody.create(bodyBytes, contentType);
|
||||
return response.newBuilder().body(body).build();
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package org.hl7.fhir.r4b.utils.client.network;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -19,8 +20,8 @@ import org.hl7.fhir.r4b.utils.OperationOutcomeUtilities;
|
||||
import org.hl7.fhir.r4b.utils.ResourceUtilities;
|
||||
import org.hl7.fhir.r4b.utils.client.EFhirClientException;
|
||||
import org.hl7.fhir.r4b.utils.client.ResourceFormat;
|
||||
import org.hl7.fhir.utilities.http.HTTPRequest;
|
||||
import org.hl7.fhir.utilities.http.HTTPHeader;
|
||||
import org.hl7.fhir.utilities.ToolingClientLogger;
|
||||
import org.hl7.fhir.utilities.http.*;
|
||||
import org.hl7.fhir.utilities.xhtml.XhtmlUtils;
|
||||
|
||||
public class FhirRequestBuilder {
|
||||
@ -34,8 +35,8 @@ public class FhirRequestBuilder {
|
||||
/**
|
||||
* The singleton instance of the HttpClient, used for all requests.
|
||||
*/
|
||||
private static OkHttpClient okHttpClient;
|
||||
private final Request.Builder httpRequest;
|
||||
//private static OkHttpClient okHttpClient;
|
||||
private final HTTPRequest httpRequest;
|
||||
private String resourceFormat = null;
|
||||
private Iterable<HTTPHeader> headers = null;
|
||||
private String message = null;
|
||||
@ -51,19 +52,15 @@ public class FhirRequestBuilder {
|
||||
private TimeUnit timeoutUnit = TimeUnit.MILLISECONDS;
|
||||
|
||||
/**
|
||||
* {@link FhirLoggingInterceptor} for log output.
|
||||
* {@link ToolingClientLogger} for log output.
|
||||
*/
|
||||
private FhirLoggingInterceptor logger = null;
|
||||
private ToolingClientLogger logger = null;
|
||||
private String source;
|
||||
|
||||
//TODO this should be the only constructor. There should be no okHttp exposure.
|
||||
public FhirRequestBuilder(HTTPRequest HTTPRequest, String source) {
|
||||
public FhirRequestBuilder(HTTPRequest httpRequest, String source) {
|
||||
this.source = source;
|
||||
|
||||
RequestBody body = RequestBody.create(HTTPRequest.getBody());
|
||||
this.httpRequest = new Request.Builder()
|
||||
.url(HTTPRequest.getUrl())
|
||||
.method(HTTPRequest.getMethod().name(), body);
|
||||
this.httpRequest = httpRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,43 +71,25 @@ public class FhirRequestBuilder {
|
||||
* @param format Expected {@link Resource} format.
|
||||
* @param headers Any additional {@link Headers} to add to the request.
|
||||
*/
|
||||
protected static void formatHeaders(Request.Builder request, String format, Iterable<HTTPHeader> headers) {
|
||||
addDefaultHeaders(request, headers);
|
||||
if (format != null)
|
||||
addResourceFormatHeaders(request, format);
|
||||
if (headers != null)
|
||||
addHeaders(request, headers);
|
||||
protected static HTTPRequest formatHeaders(HTTPRequest request, String format, Iterable<HTTPHeader> headers) {
|
||||
List<HTTPHeader> allHeaders = new ArrayList<>();
|
||||
request.getHeaders().forEach(allHeaders::add);
|
||||
|
||||
if (format != null) getResourceFormatHeaders(request, format).forEach(allHeaders::add);
|
||||
if (headers != null) headers.forEach(allHeaders::add);
|
||||
return request.withHeaders(allHeaders);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds necessary headers for all REST requests.
|
||||
* <li>User-Agent : hapi-fhir-tooling-client</li>
|
||||
*
|
||||
* @param request {@link Request.Builder} to add default headers to.
|
||||
*/
|
||||
protected static void addDefaultHeaders(Request.Builder request, Iterable<HTTPHeader> headers) {
|
||||
boolean hasUserAgent = false;
|
||||
if (headers != null) {
|
||||
for (HTTPHeader header : headers) {
|
||||
if (header.getName().equalsIgnoreCase("User-Agent")) {
|
||||
hasUserAgent = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
protected static Iterable<HTTPHeader> getResourceFormatHeaders(HTTPRequest httpRequest, String format) {
|
||||
List<HTTPHeader> headers = new ArrayList<>();
|
||||
headers.add(new HTTPHeader("Accept", format));
|
||||
if (httpRequest.getMethod() == HTTPRequest.HttpMethod.PUT
|
||||
|| httpRequest.getMethod() == HTTPRequest.HttpMethod.POST
|
||||
|| httpRequest.getMethod() == HTTPRequest.HttpMethod.PATCH
|
||||
) {
|
||||
headers.add( new HTTPHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET));
|
||||
}
|
||||
if (!hasUserAgent) {
|
||||
request.addHeader("User-Agent", "hapi-fhir-tooling-client");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds necessary headers for the given resource format provided.
|
||||
*
|
||||
* @param request {@link Request.Builder} to add default headers to.
|
||||
*/
|
||||
protected static void addResourceFormatHeaders(Request.Builder request, String format) {
|
||||
request.addHeader("Accept", format);
|
||||
request.addHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET);
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -148,15 +127,21 @@ public class FhirRequestBuilder {
|
||||
* @param headers {@link Headers} to evaluate
|
||||
* @return {@link String} header value, or null if no location headers are set.
|
||||
*/
|
||||
protected static String getLocationHeader(Headers headers) {
|
||||
Map<String, List<String>> headerMap = headers.toMultimap();
|
||||
if (headerMap.containsKey(LOCATION_HEADER)) {
|
||||
return headerMap.get(LOCATION_HEADER).get(0);
|
||||
} else if (headerMap.containsKey(CONTENT_LOCATION_HEADER)) {
|
||||
return headerMap.get(CONTENT_LOCATION_HEADER).get(0);
|
||||
} else {
|
||||
return null;
|
||||
/**
|
||||
* Extracts the 'location' header from. If no value for 'location' exists, the
|
||||
* value for 'content-location' is returned. If neither header exists, we return null.
|
||||
*/
|
||||
protected static String getLocationHeader(Iterable<HTTPHeader> headers) {
|
||||
String locationHeader = HTTPHeaderUtil.getSingleHeader(headers, LOCATION_HEADER);
|
||||
|
||||
if (locationHeader != null) {
|
||||
return locationHeader;
|
||||
}
|
||||
return HTTPHeaderUtil.getSingleHeader(headers, CONTENT_LOCATION_HEADER);
|
||||
}
|
||||
|
||||
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
||||
return new ManagedFhirWebAccessBuilder("hapi-fhir-tooling-client", null).withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,6 +161,7 @@ public class FhirRequestBuilder {
|
||||
*
|
||||
* @return {@link OkHttpClient} instance
|
||||
*/
|
||||
/*FIXME remove after refactor
|
||||
protected OkHttpClient getHttpClient() {
|
||||
if (okHttpClient == null) {
|
||||
okHttpClient = new OkHttpClient();
|
||||
@ -191,7 +177,7 @@ public class FhirRequestBuilder {
|
||||
return builder.connectTimeout(timeout, timeoutUnit).writeTimeout(timeout, timeoutUnit)
|
||||
.readTimeout(timeout, timeoutUnit).proxyAuthenticator(proxyAuthenticator).build();
|
||||
}
|
||||
|
||||
*/
|
||||
@Nonnull
|
||||
private static Authenticator getAuthenticator() {
|
||||
return (route, response) -> {
|
||||
@ -225,7 +211,7 @@ public class FhirRequestBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public FhirRequestBuilder withLogger(FhirLoggingInterceptor logger) {
|
||||
public FhirRequestBuilder withLogger(ToolingClientLogger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
@ -236,33 +222,28 @@ public class FhirRequestBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
protected Request buildRequest() {
|
||||
return httpRequest.build();
|
||||
}
|
||||
|
||||
public <T extends Resource> ResourceRequest<T> execute() throws IOException {
|
||||
formatHeaders(httpRequest, resourceFormat, headers);
|
||||
Response response = getHttpClient().newCall(httpRequest.build()).execute();
|
||||
HTTPRequest requestWithHeaders = formatHeaders(httpRequest, resourceFormat, headers);
|
||||
HTTPResult response = getManagedWebAccessBuilder().httpCall(requestWithHeaders);
|
||||
T resource = unmarshalReference(response, resourceFormat, null);
|
||||
return new ResourceRequest<T>(resource, response.code(), getLocationHeader(response.headers()));
|
||||
return new ResourceRequest<T>(resource, response.getCode(), getLocationHeader(response.getHeaders()));
|
||||
}
|
||||
|
||||
public Bundle executeAsBatch() throws IOException {
|
||||
formatHeaders(httpRequest, resourceFormat, null);
|
||||
Response response = getHttpClient().newCall(httpRequest.build()).execute();
|
||||
return unmarshalFeed(response, resourceFormat);
|
||||
HTTPRequest requestWithHeaders = formatHeaders(httpRequest, resourceFormat, null);
|
||||
HTTPResult response = getManagedWebAccessBuilder().httpCall(requestWithHeaders);return unmarshalFeed(response, resourceFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshalls a resource from the response stream.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T extends Resource> T unmarshalReference(Response response, String format, String resourceType) {
|
||||
int code = response.code();
|
||||
protected <T extends Resource> T unmarshalReference(HTTPResult response, String format, String resourceType) {
|
||||
int code = response.getCode();
|
||||
boolean ok = code >= 200 && code < 300;
|
||||
if (response.body() == null) {
|
||||
if (response.getContent() == null) {
|
||||
if (!ok) {
|
||||
throw new EFhirClientException(response.message());
|
||||
throw new EFhirClientException(response.getMessage());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@ -271,9 +252,9 @@ public class FhirRequestBuilder {
|
||||
|
||||
Resource resource = null;
|
||||
try {
|
||||
body = response.body().string();
|
||||
String ct = response.header("Content-Type");
|
||||
if (ct == null) {
|
||||
body = response.getContentAsString();
|
||||
String contentType = HTTPHeaderUtil.getSingleHeader(response.getHeaders(), "Content-Type");
|
||||
if (contentType == null) {
|
||||
if (ok) {
|
||||
resource = getParser(format).parse(body);
|
||||
} else {
|
||||
@ -282,10 +263,10 @@ public class FhirRequestBuilder {
|
||||
resource = OperationOutcomeUtilities.outcomeFromTextError(body);
|
||||
}
|
||||
} else {
|
||||
if (ct.contains(";")) {
|
||||
ct = ct.substring(0, ct.indexOf(";"));
|
||||
if (contentType.contains(";")) {
|
||||
contentType = contentType.substring(0, contentType.indexOf(";"));
|
||||
}
|
||||
switch (ct) {
|
||||
switch (contentType) {
|
||||
case "application/json":
|
||||
case "application/fhir+json":
|
||||
if (!format.contains("json")) {
|
||||
@ -305,10 +286,10 @@ public class FhirRequestBuilder {
|
||||
resource = OperationOutcomeUtilities.outcomeFromTextError(body);
|
||||
break;
|
||||
case "text/html" :
|
||||
resource = OperationOutcomeUtilities.outcomeFromTextError(XhtmlUtils.convertHtmlToText(response.body().string(), source));
|
||||
resource = OperationOutcomeUtilities.outcomeFromTextError(XhtmlUtils.convertHtmlToText(response.getContentAsString(), source));
|
||||
break;
|
||||
default: // not sure what else to do?
|
||||
System.out.println("Got content-type '"+ct+"' from "+source);
|
||||
System.out.println("Got content-type '"+contentType+"' from "+source);
|
||||
System.out.println(body);
|
||||
resource = OperationOutcomeUtilities.outcomeFromTextError(body);
|
||||
}
|
||||
@ -343,7 +324,7 @@ public class FhirRequestBuilder {
|
||||
/**
|
||||
* Unmarshalls Bundle from response stream.
|
||||
*/
|
||||
protected Bundle unmarshalFeed(Response response, String format) {
|
||||
protected Bundle unmarshalFeed(HTTPResult response, String format) {
|
||||
return unmarshalReference(response, format, "Bundle");
|
||||
}
|
||||
|
||||
|
@ -1,67 +0,0 @@
|
||||
package org.hl7.fhir.r4b.utils.client.network;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An {@link Interceptor} for {@link okhttp3.OkHttpClient} that controls the
|
||||
* number of times we retry a to execute a given request, before reporting a
|
||||
* failure. This includes unsuccessful return codes and timeouts.
|
||||
*/
|
||||
public class RetryInterceptor implements Interceptor {
|
||||
|
||||
// Delay between retying failed requests, in millis
|
||||
private final long RETRY_TIME = 2000;
|
||||
|
||||
// Maximum number of times to retry the request before failing
|
||||
private final int maxRetry;
|
||||
|
||||
// Internal counter for tracking the number of times we've tried this request
|
||||
private int retryCounter = 0;
|
||||
|
||||
public RetryInterceptor(int maxRetry) {
|
||||
this.maxRetry = maxRetry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
Response response = null;
|
||||
|
||||
do {
|
||||
try {
|
||||
// If we are retrying a failed request that failed due to a bad response from
|
||||
// the server, we must close it first
|
||||
if (response != null) {
|
||||
// System.out.println("Previous " + chain.request().method() + " attempt returned HTTP<" + (response.code())
|
||||
// + "> from url -> " + chain.request().url() + ".");
|
||||
response.close();
|
||||
}
|
||||
// System.out.println(chain.request().method() + " attempt <" + (retryCounter +
|
||||
// 1) + "> to url -> " + chain.request().url());
|
||||
response = chain.proceed(request);
|
||||
} catch (IOException e) {
|
||||
try {
|
||||
// Include a small break in between requests.
|
||||
Thread.sleep(RETRY_TIME);
|
||||
} catch (InterruptedException e1) {
|
||||
System.out.println(chain.request().method() + " to url -> " + chain.request().url() + " interrupted on try <"
|
||||
+ retryCounter + ">");
|
||||
}
|
||||
} finally {
|
||||
retryCounter++;
|
||||
}
|
||||
} while ((response == null || !response.isSuccessful()) && (retryCounter <= maxRetry + 1));
|
||||
|
||||
/*
|
||||
* if something has gone wrong, and we are unable to complete the request, we
|
||||
* still need to initialize the return response so we don't get a null pointer
|
||||
* exception.
|
||||
*/
|
||||
return response != null ? response : chain.proceed(request);
|
||||
}
|
||||
|
||||
}
|
@ -1,50 +1,52 @@
|
||||
package org.hl7.fhir.r4b.utils.client.network;
|
||||
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import org.hl7.fhir.r4b.model.OperationOutcome;
|
||||
import org.hl7.fhir.utilities.http.HTTPHeader;
|
||||
import org.hl7.fhir.utilities.http.HTTPHeaderUtil;
|
||||
import org.hl7.fhir.utilities.http.HTTPRequest;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
class FhirRequestBuilderTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Test default headers are added correctly.")
|
||||
void addDefaultHeaders() {
|
||||
Request.Builder request = new Request.Builder().url("http://www.google.com");
|
||||
FhirRequestBuilder.addDefaultHeaders(request, null);
|
||||
|
||||
Map<String, List<String>> headersMap = request.build().headers().toMultimap();
|
||||
Assertions.assertNotNull(headersMap.get("User-Agent"), "User-Agent header null.");
|
||||
Assertions.assertEquals("hapi-fhir-tooling-client", headersMap.get("User-Agent").get(0),
|
||||
"User-Agent header not populated with expected value \"hapi-fhir-tooling-client\".");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test resource format headers are added correctly.")
|
||||
void addResourceFormatHeaders() {
|
||||
void addResourceFormatHeadersGET() {
|
||||
String testFormat = "yaml";
|
||||
Request.Builder request = new Request.Builder().url("http://www.google.com");
|
||||
FhirRequestBuilder.addResourceFormatHeaders(request, testFormat);
|
||||
HTTPRequest request = new HTTPRequest().withUrl("http://www.google.com").withMethod(HTTPRequest.HttpMethod.GET);
|
||||
|
||||
Map<String, List<String>> headersMap = request.build().headers().toMultimap();
|
||||
Iterable<HTTPHeader> headers = FhirRequestBuilder.getResourceFormatHeaders(request, testFormat);
|
||||
|
||||
Map<String, List<String>> headersMap = HTTPHeaderUtil.getMultimap(headers);
|
||||
Assertions.assertNotNull(headersMap.get("Accept"), "Accept header null.");
|
||||
Assertions.assertEquals(testFormat, headersMap.get("Accept").get(0),
|
||||
"Accept header not populated with expected value " + testFormat + ".");
|
||||
|
||||
Assertions.assertNull(headersMap.get("Content-Type"), "Content-Type header null.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test resource format headers are added correctly (POST).")
|
||||
void addResourceFormatHeadersPOST() {
|
||||
//FIXME tested here. Should get list of HTTPHeader.
|
||||
String testFormat = "yaml";
|
||||
HTTPRequest request = new HTTPRequest().withUrl("http://www.google.com").withMethod(HTTPRequest.HttpMethod.POST);
|
||||
|
||||
Iterable<HTTPHeader> headers = FhirRequestBuilder.getResourceFormatHeaders(request, testFormat);
|
||||
|
||||
Map<String, List<String>> headersMap = HTTPHeaderUtil.getMultimap(headers);
|
||||
Assertions.assertNotNull(headersMap.get("Accept"), "Accept header null.");
|
||||
Assertions.assertEquals(testFormat, headersMap.get("Accept").get(0),
|
||||
"Accept header not populated with expected value " + testFormat + ".");
|
||||
|
||||
Assertions.assertNotNull(headersMap.get("Content-Type"), "Content-Type header null.");
|
||||
Assertions.assertEquals(testFormat + ";charset=" + FhirRequestBuilder.DEFAULT_CHARSET,
|
||||
headersMap.get("Content-Type").get(0), "Content-Type header not populated with expected value \"" + testFormat
|
||||
+ ";charset=" + FhirRequestBuilder.DEFAULT_CHARSET + "\".");
|
||||
Assertions.assertEquals(testFormat + ";charset=" + FhirRequestBuilder.DEFAULT_CHARSET, headersMap.get("Content-Type").get(0),
|
||||
"Content-Type header not populated with expected value \"" + testFormat + ";charset=" + FhirRequestBuilder.DEFAULT_CHARSET + "\".");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -119,7 +121,7 @@ class FhirRequestBuilderTest {
|
||||
@DisplayName("Test that getLocationHeader returns header for 'location'.")
|
||||
void getLocationHeaderWhenOnlyLocationIsSet() {
|
||||
final String expectedLocationHeader = "location_header_value";
|
||||
Headers headers = new Headers.Builder().add(FhirRequestBuilder.LOCATION_HEADER, expectedLocationHeader).build();
|
||||
Iterable<HTTPHeader> headers = List.of(new HTTPHeader(FhirRequestBuilder.LOCATION_HEADER, expectedLocationHeader));
|
||||
Assertions.assertEquals(expectedLocationHeader, FhirRequestBuilder.getLocationHeader(headers));
|
||||
}
|
||||
|
||||
@ -127,8 +129,7 @@ class FhirRequestBuilderTest {
|
||||
@DisplayName("Test that getLocationHeader returns header for 'content-location'.")
|
||||
void getLocationHeaderWhenOnlyContentLocationIsSet() {
|
||||
final String expectedContentLocationHeader = "content_location_header_value";
|
||||
Headers headers = new Headers.Builder()
|
||||
.add(FhirRequestBuilder.CONTENT_LOCATION_HEADER, expectedContentLocationHeader).build();
|
||||
Iterable<HTTPHeader> headers = List.of(new HTTPHeader(FhirRequestBuilder.CONTENT_LOCATION_HEADER, expectedContentLocationHeader));
|
||||
Assertions.assertEquals(expectedContentLocationHeader, FhirRequestBuilder.getLocationHeader(headers));
|
||||
}
|
||||
|
||||
@ -137,15 +138,17 @@ class FhirRequestBuilderTest {
|
||||
void getLocationHeaderWhenLocationAndContentLocationAreSet() {
|
||||
final String expectedLocationHeader = "location_header_value";
|
||||
final String expectedContentLocationHeader = "content_location_header_value";
|
||||
Headers headers = new Headers.Builder().add(FhirRequestBuilder.LOCATION_HEADER, expectedLocationHeader)
|
||||
.add(FhirRequestBuilder.CONTENT_LOCATION_HEADER, expectedContentLocationHeader).build();
|
||||
Iterable<HTTPHeader> headers = List.of(
|
||||
new HTTPHeader(FhirRequestBuilder.LOCATION_HEADER, expectedLocationHeader),
|
||||
new HTTPHeader(FhirRequestBuilder.CONTENT_LOCATION_HEADER, expectedContentLocationHeader)
|
||||
);
|
||||
Assertions.assertEquals(expectedLocationHeader, FhirRequestBuilder.getLocationHeader(headers));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that getLocationHeader returns null when no location available.")
|
||||
void getLocationHeaderWhenNoLocationSet() {
|
||||
Headers headers = new Headers.Builder().build();
|
||||
Assertions.assertNull(FhirRequestBuilder.getLocationHeader(headers));
|
||||
|
||||
Assertions.assertNull(FhirRequestBuilder.getLocationHeader(Collections.emptyList()));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user