Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions sdk/src/main/java/io/dapr/client/DaprHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import reactor.util.context.Context;

import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

Expand All @@ -50,6 +53,12 @@ public class DaprHttp implements AutoCloseable {
*/
private static final String DEFAULT_HTTP_SCHEME = "http";

/**
* Context entries allowed to be in HTTP Headers.
*/
private static final Set<String> ALLOWED_CONTEXT_IN_HEADERS =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("grpc-trace-bin", "traceparent", "tracestate")));

/**
* HTTP Methods supported.
*/
Expand Down Expand Up @@ -267,8 +276,9 @@ private CompletableFuture<Response> doInvokeApi(String method,
.url(urlBuilder.build())
.addHeader(HEADER_DAPR_REQUEST_ID, requestId);
if (context != null) {
context.stream().forEach(
entry -> requestBuilder.addHeader(entry.getKey().toString(), entry.getValue().toString()));
context.stream()
.filter(entry -> ALLOWED_CONTEXT_IN_HEADERS.contains(entry.getKey().toString().toLowerCase()))
.forEach(entry -> requestBuilder.addHeader(entry.getKey().toString(), entry.getValue().toString()));
}
if (HttpMethods.GET.name().equals(method)) {
requestBuilder.get();
Expand Down
24 changes: 24 additions & 0 deletions sdk/src/test/java/io/dapr/client/DaprClientHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.dapr.client.domain.GetStateRequest;
import io.dapr.client.domain.GetStateRequestBuilder;
import io.dapr.client.domain.HttpExtension;
import io.dapr.client.domain.InvokeMethodRequest;
import io.dapr.client.domain.PublishEventRequest;
import io.dapr.client.domain.PublishEventRequestBuilder;
import io.dapr.client.domain.State;
Expand All @@ -36,6 +37,7 @@
import org.junit.Test;
import org.mockito.Mockito;
import reactor.core.publisher.Mono;
import reactor.util.context.Context;

import java.io.IOException;
import java.net.ServerSocket;
Expand Down Expand Up @@ -388,6 +390,28 @@ public void invokeServiceNoHotMono() {
// No exception should be thrown because did not call block() on mono above.
}

@Test
public void invokeServiceWithContext() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test actually check that the header is filtered out? I'm not sure if the mockInterceptor rule will only be matched if it has just those headers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It only matches if the headers match too. I tested by removing the fix and the test fails.

String traceparent = "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01";
String tracestate = "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7";
Context context = Context.empty()
.put("traceparent", traceparent)
.put("tracestate", tracestate)
.put("not_added", "xyz");
mockInterceptor.addRule()
.post("http://127.0.0.1:3000/v1.0/invoke/41/method/neworder")
.header("traceparent", traceparent)
.header("tracestate", tracestate)
.respond(new byte[0]);

InvokeMethodRequest req = new InvokeMethodRequest("41", "neworder")
.setBody("request")
.setHttpExtension(HttpExtension.POST);
Mono<Void> result = daprClientHttp.invokeMethod(req, TypeRef.get(Void.class))
.subscriberContext(it -> it.putAll(context));
result.block();
}

@Test
public void invokeBinding() {
Map<String, String> map = new HashMap<>();
Expand Down