Add tests for dapr api token

This commit is contained in:
pruthvidhodda 2020-08-27 14:32:01 -07:00
parent c6055c0b96
commit 39f729221c
2 changed files with 43 additions and 0 deletions

View File

@ -84,6 +84,12 @@
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>

View File

@ -13,6 +13,8 @@ import okhttp3.ResponseBody;
import okhttp3.mock.Behavior;
import okhttp3.mock.MockInterceptor;
import org.junit.Before;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.Rule;
import org.junit.Test;
import reactor.core.publisher.Mono;
@ -27,6 +29,9 @@ import static org.junit.Assert.fail;
public class DaprHttpTest {
@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
private static final String EXPECTED_RESULT =
"{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}";
@ -42,6 +47,38 @@ public class DaprHttpTest {
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
}
@Test
public void invokeApi_daprApiToken_present() throws IOException {
mockInterceptor.addRule()
.post("http://127.0.0.1:3500/v1.0/state")
.hasHeader(Constants.DAPR_API_TOKEN_HEADER)
.respond(serializer.serialize(EXPECTED_RESULT));
environmentVariables.set(Constants.DAPR_API_TOKEN, "xyz");
assertEquals("xyz", System.getenv(Constants.DAPR_API_TOKEN));
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono =
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, null, Context.current());
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT, body);
}
@Test
public void invokeApi_daprApiToken_absent() throws IOException {
mockInterceptor.addRule()
.post("http://127.0.0.1:3500/v1.0/state")
.not()
.hasHeader(Constants.DAPR_API_TOKEN_HEADER)
.respond(serializer.serialize(EXPECTED_RESULT));
assertNull(System.getenv(Constants.DAPR_API_TOKEN));
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono =
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, null, Context.current());
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT, body);
}
@Test
public void invokeMethod() throws IOException {
Map<String, String> headers = new HashMap<>();