单元测试调试通过
This commit is contained in:
parent
4b35d74853
commit
85d5689d0f
|
@ -14,7 +14,6 @@ import org.springframework.web.server.ServerWebExchange;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@SpringBootConfiguration
|
||||
|
@ -32,8 +31,11 @@ public class TokenFilter implements GlobalFilter {
|
|||
|
||||
if (token != null) {
|
||||
//
|
||||
|
||||
return chain.filter(exchange);
|
||||
if (VerifyToken(token)) {
|
||||
log.info("request token is Verify Successed: {}", token);
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
log.info("request token is Verify Fail: {} != {}", token, TEST_TOKEN);
|
||||
} else {
|
||||
log.warn("request token is empty: {}", request.getURI());
|
||||
}
|
||||
|
@ -41,11 +43,19 @@ public class TokenFilter implements GlobalFilter {
|
|||
ServerHttpResponse response = exchange.getResponse();
|
||||
if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) {
|
||||
log.error("if the status code has not been set because the HTTP response is already committed");
|
||||
}
|
||||
}
|
||||
return response.setComplete();
|
||||
}
|
||||
|
||||
|
||||
public static final String TEST_TOKEN = "yame";
|
||||
public boolean VerifyToken(String token) {
|
||||
if (token.equals( TEST_TOKEN) ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package ocean.gateway.filters;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -7,12 +9,16 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import ocean.gateway.service.ServiceApplication;
|
||||
import ocean.gateway.service.filters.TokenFilter;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
|
@ -27,9 +33,25 @@ public class TokenFilterTests {
|
|||
private TestRestTemplate restTemplate ;
|
||||
|
||||
@Test
|
||||
public void GetHello() {
|
||||
public void GetHelloWithoutToken() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity("http://127.0.0.1:" + randomServerPort +"/hello", String.class);
|
||||
Assert.notNull(response, "The class must not be null");
|
||||
Assert.state(response.getStatusCode() == HttpStatus.UNAUTHORIZED, "response.getStatusCode() must be HttpStatus.UNAUTHORIZED");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void GetHelloWithToken() {
|
||||
String uri = "http://127.0.0.1:" + randomServerPort +"/hello";
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
String key = TokenFilter.TEST_TOKEN;
|
||||
|
||||
headers.add("token",key);
|
||||
HttpEntity<String> entity = new HttpEntity<>("", headers);
|
||||
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
|
||||
Assert.notNull(response, "The class must not be null");
|
||||
Assert.state(response.getStatusCode() != HttpStatus.UNAUTHORIZED, "response.getStatusCode() must not be HttpStatus.UNAUTHORIZED");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user