/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
spring-boot-modules/spring-boot-security/src/test/java/com/baeldung/integrationtesting/SecuredControllerRestTemplateIntegrationTest.java
32 строки
1 KB
Harry9656
JAVA-31190: Preparation for migrating spring-boot-modules to version 3. (#15834)
27 фев 2024, 04:49
Не верифицирован
27 фев 2024, 04:49
aefd833
Код
Авторство
О чём код?
package com.baeldung.integrationtesting; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class SecuredControllerRestTemplateIntegrationTest { @Autowired private TestRestTemplate template; @Test public void givenRequestOnPrivateService_shouldFailWith401() throws Exception { ResponseEntity<String> result = template.getForEntity("/private/hello", String.class); assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode()); } @Test public void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception { ResponseEntity<String> result = template.withBasicAuth("spring", "secret") .getForEntity("/private/hello", String.class); assertEquals(HttpStatus.OK, result.getStatusCode()); } }