mirror of https://github.com/dapr/java-sdk.git
Add tests for dapr api token
This commit is contained in:
parent
c6055c0b96
commit
39f729221c
|
|
@ -84,6 +84,12 @@
|
||||||
<version>1.3.2</version>
|
<version>1.3.2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.stefanbirkner</groupId>
|
||||||
|
<artifactId>system-rules</artifactId>
|
||||||
|
<version>1.19.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ import okhttp3.ResponseBody;
|
||||||
import okhttp3.mock.Behavior;
|
import okhttp3.mock.Behavior;
|
||||||
import okhttp3.mock.MockInterceptor;
|
import okhttp3.mock.MockInterceptor;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.contrib.java.lang.system.EnvironmentVariables;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
|
@ -27,6 +29,9 @@ import static org.junit.Assert.fail;
|
||||||
|
|
||||||
public class DaprHttpTest {
|
public class DaprHttpTest {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
|
||||||
|
|
||||||
private static final String EXPECTED_RESULT =
|
private static final String EXPECTED_RESULT =
|
||||||
"{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}";
|
"{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}";
|
||||||
|
|
||||||
|
|
@ -42,6 +47,38 @@ public class DaprHttpTest {
|
||||||
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
|
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
|
@Test
|
||||||
public void invokeMethod() throws IOException {
|
public void invokeMethod() throws IOException {
|
||||||
Map<String, String> headers = new HashMap<>();
|
Map<String, String> headers = new HashMap<>();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue