Java Examples for org.springframework.boot.test.TestRestTemplate

The following java examples will help you to understand the usage of org.springframework.boot.test.TestRestTemplate. These source code samples are taken from different open source projects.

Example 1
Project: comsat-master  File: SampleActuatorApplicationTests.java View source code
private void testHomeIsSecure(final String path) throws Exception {
    @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = new TestRestTemplate().getForEntity("http://localhost:" + this.port + "/" + emptyIfNull(path), Map.class);
    assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
    @SuppressWarnings("unchecked") Map<String, Object> body = entity.getBody();
    assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
    assertFalse("Wrong headers: " + entity.getHeaders(), entity.getHeaders().containsKey("Set-Cookie"));
}
Example 2
Project: j360-boot-master  File: J360ApplicationTest.java View source code
/**
     * ·þÎñÆ÷±àÂëÉèÖÃ
     * server.compression.enabled: true
     * server.compression.min-response-size: 1
     * */
@Test
public void testCompression() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Accept-Encoding", "gzip");
    HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    RestTemplate restTemplate = new TestRestTemplate();
    ResponseEntity<byte[]> entity = restTemplate.exchange("http://localhost:" + this.port + "/hello", HttpMethod.GET, requestEntity, byte[].class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()));
    try {
        assertEquals("Hello J360", StreamUtils.copyToString(inflater, Charset.forName("UTF-8")));
    } finally {
        inflater.close();
    }
}
Example 3
Project: sculptor-master  File: FrontResourceControllerTest.java View source code
@Test
public void testHome() throws Exception {
    ResponseEntity<String> entity = new TestRestTemplate().getForEntity("http://localhost:" + this.port, String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body (reefresh URL doesn't match):\n" + entity.getBody(), entity.getBody().contains(";URL=rest/front"));
}
Example 4
Project: spring-boot-hypermedia-master  File: ServerPortHypermediaIntegrationTests.java View source code
@Test
public void links() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/", HttpMethod.GET, new HttpEntity<Void>(null, headers), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body: " + entity.getBody(), entity.getBody().contains("\"_links\":"));
}
Example 5
Project: spring-oauth2-integration-tests-master  File: ImplicitProviderTests.java View source code
private void getToken() {
    Map<String, String> form = new LinkedHashMap<String, String>();
    form.put("client_id", "my-trusted-client");
    form.put("redirect_uri", "http://foo.com");
    form.put("response_type", "token");
    form.put("scope", "read");
    ResponseEntity<Void> response = new TestRestTemplate("user", "password").getForEntity(http.getUrl("/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type={response_type}&scope={scope}"), Void.class, form);
    assertEquals(HttpStatus.FOUND, response.getStatusCode());
    assertTrue(response.getHeaders().getLocation().toString().contains("access_token"));
}
Example 6
Project: xtext-master  File: FrontResourceControllerTest.java View source code
@Test
public void testHome() throws Exception {
    ResponseEntity<String> entity = new TestRestTemplate().getForEntity("http://localhost:" + this.port, String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body (reefresh URL doesn't match):\n" + entity.getBody(), entity.getBody().contains(";URL=rest/front"));
}
Example 7
Project: spring-boot-shiro-orientdb-master  File: UserControllerTest.java View source code
@Test
public void test_authenticate_success() throws JsonProcessingException {
    // authenticate
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    final String json = new ObjectMapper().writeValueAsString(new UsernamePasswordToken(USER_EMAIL, USER_PWD));
    System.out.println(json);
    final ResponseEntity<String> response = new TestRestTemplate(HttpClientOption.ENABLE_COOKIES).exchange(BASE_URL.concat("/auth"), HttpMethod.POST, new HttpEntity<>(json, headers), String.class);
    assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}
Example 8
Project: mars-calendar-master  File: GreetingsControllerTest.java View source code
@Test
public void testHttpGet() throws IOException, URISyntaxException {
    RestTemplate restTemplate = new TestRestTemplate();
    int port = 8080;
    final URI url = new URI("http://localhost:" + port + "/rest/v1/greeting");
    final ResponseEntity<Greeting> entity = restTemplate.getForEntity(url, Greeting.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertEquals("Hello, Anonymous!", entity.getBody().getText());
}
Example 9
Project: spring-boot-https-kit-master  File: TomcatConfigTest.java View source code
@Before
public void init() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    SSLContextBuilder builder = new SSLContextBuilder();
    // trust self signed certificate
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build());
    final HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
    restTemplate = new TestRestTemplate();
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient) {

        @Override
        protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
            HttpClientContext context = HttpClientContext.create();
            RequestConfig.Builder builder = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).setAuthenticationEnabled(false).setRedirectsEnabled(false).setConnectTimeout(1000).setConnectionRequestTimeout(1000).setSocketTimeout(1000);
            context.setRequestConfig(builder.build());
            return context;
        }
    });
}
Example 10
Project: spring-security-oauth-master  File: ImplicitProviderTests.java View source code
private void getToken() {
    Map<String, String> form = new LinkedHashMap<String, String>();
    form.put("client_id", "my-trusted-client");
    form.put("redirect_uri", "http://foo.com");
    form.put("response_type", "token");
    form.put("scope", "read");
    ResponseEntity<Void> response = new TestRestTemplate("user", "password").getForEntity(http.getUrl("/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type={response_type}&scope={scope}"), Void.class, form);
    assertEquals("Wrong status: " + response.getHeaders(), HttpStatus.FOUND, response.getStatusCode());
    assertTrue(response.getHeaders().getLocation().toString().contains("access_token"));
}
Example 11
Project: java-webapp-security-examples-master  File: WebSecurityTests.java View source code
@Test
public void testHome() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port, HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity.getBody().contains("<title>Spring"));
}
Example 12
Project: spring-data-orientdb-master  File: UserControllerTest.java View source code
@Test
public void test_authenticate_success() throws JsonProcessingException {
    // authenticate
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    final String json = new ObjectMapper().writeValueAsString(new UsernamePasswordToken(USER_EMAIL, USER_PWD));
    System.out.println(json);
    final ResponseEntity<String> response = new TestRestTemplate(TestRestTemplate.HttpClientOption.ENABLE_COOKIES).exchange(BASE_URL.concat("/auth"), HttpMethod.POST, new HttpEntity<>(json, headers), String.class);
    assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}
Example 13
Project: apollo-master  File: ItemSetControllerTest.java View source code
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetCreated() {
    String appId = "someAppId";
    AppDTO app = restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);
    ClusterDTO cluster = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default", ClusterDTO.class);
    NamespaceDTO namespace = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);
    Assert.assertEquals("someAppId", app.getAppId());
    Assert.assertEquals("default", cluster.getName());
    Assert.assertEquals("application", namespace.getNamespaceName());
    ItemChangeSets itemSet = new ItemChangeSets();
    itemSet.setDataChangeLastModifiedBy("created");
    RestTemplate createdTemplate = new TestRestTemplate("created", "");
    createdTemplate.setMessageConverters(restTemplate.getMessageConverters());
    int createdSize = 3;
    for (int i = 0; i < createdSize; i++) {
        ItemDTO item = new ItemDTO();
        item.setNamespaceId(namespace.getId());
        item.setKey("key_" + i);
        item.setValue("created_value_" + i);
        itemSet.addCreateItem(item);
    }
    ResponseEntity<Void> response = createdTemplate.postForEntity("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/itemset", itemSet, Void.class);
    Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
    List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
    Assert.assertEquals(createdSize, items.size());
    Item item0 = items.get(0);
    Assert.assertEquals("key_0", item0.getKey());
    Assert.assertEquals("created_value_0", item0.getValue());
    Assert.assertEquals("created", item0.getDataChangeCreatedBy());
    Assert.assertNotNull(item0.getDataChangeCreatedTime());
}
Example 14
Project: Springs-master  File: BookEndpointTest.java View source code
@Before
public void setup() {
    // TestRestTemplate与RestTemplate, �务端返回�200返回�时,�会抛异常.
    restTemplate = new TestRestTemplate();
    resourceUrl = "http://localhost:" + port + "/api/books";
    loginUrl = "http://localhost:" + port + "/api/accounts/login";
    logoutUrl = "http://localhost:" + port + "/api/accounts/logout";
}
Example 15
Project: springside4-master  File: BookEndpointTest.java View source code
@Before
public void setup() {
    // TestRestTemplate与RestTemplate, �务端返回�200返回�时,�会抛异常.
    restTemplate = new TestRestTemplate();
    resourceUrl = "http://localhost:" + port + "/api/books";
    loginUrl = "http://localhost:" + port + "/api/accounts/login";
    logoutUrl = "http://localhost:" + port + "/api/accounts/logout";
}
Example 16
Project: rod-master  File: RodServerIT.java View source code
@Test
public void testHealthIndicator() {
    RodServer.main(new String[] {});
    final RestTemplate template = new TestRestTemplate();
    final String body = template.getForEntity("http://localhost:8080/health", String.class).getBody();
    assertThat(body, equalTo("{\"status\":\"UP\"}"));
}
Example 17
Project: catwatch-master  File: AbstractCatwatchIT.java View source code
@Before
public void setUp() throws Exception {
    this.base = new URL("http://localhost:" + port);
    template = new TestRestTemplate();
}
Example 18
Project: spring-boot-uni-devoxxfr-master  File: HomeControllerIntegrationTest.java View source code
@Test
public void runAndInvokeHome() {
    String url = "http://localhost:" + port + "/";
    String body = new TestRestTemplate("hero", "hero").getForObject(url, String.class);
    assertThat(body, is("Hello devoxx"));
}
Example 19
Project: blog-microservices-master  File: ApplicationTests.java View source code
@Test
public void catalogLoads() {
    @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = new TestRestTemplate("user", "password").getForEntity("http://localhost:" + port + "/eureka/apps", Map.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
}
Example 20
Project: locks-master  File: ApplicationTests.java View source code
@Test
public void locksLoad() {
    @SuppressWarnings("rawtypes") ResponseEntity<List> entity = new TestRestTemplate().getForEntity("http://localhost:" + port, List.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
}
Example 21
Project: microservice-master  File: ApplicationTests.java View source code
@Test
public void catalogLoads() {
    ResponseEntity<Map> entity = new TestRestTemplate().getForEntity("http://localhost:" + port + "/eureka/apps", Map.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
}
Example 22
Project: mule-spring-boot-starter-master  File: HttpTest.java View source code
@Test
public void testHttpRequest() {
    // Given
    // When
    ResponseEntity<String> response = new TestRestTemplate().getForEntity("http://localhost:8081/echo", String.class);
    // Then
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals("/echo", response.getBody());
}
Example 23
Project: spring-guides-master  File: HelloControllerIntegrationTest.java View source code
@Before
public void setUp() throws Exception {
    base = new URL(String.format("http://localhost:%d/", port));
    restTemplate = new TestRestTemplate();
}
Example 24
Project: components-html5-master  File: TestsConfig.java View source code
@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.webAppContextSetup(webContext).build();
    template = new TestRestTemplate();
}
Example 25
Project: sandboxes-master  File: AdvisedControllerApplication_Test.java View source code
private ResponseEntity<String> response() {
    return new TestRestTemplate().exchange(url(), POST, new HttpEntity<>("world"), String.class);
}
Example 26
Project: spring-boot-legacy-master  File: EmbeddedIntegrationTests.java View source code
@Test
public void testVersion() throws IOException {
    String body = new TestRestTemplate().getForObject("http://127.0.0.1:" + port + "/info", String.class);
    log.info("found info = " + body);
    assertTrue("Wrong body: " + body, body.contains("{\"version"));
}
Example 27
Project: spring-cloud-lattice-master  File: LatticeDiscoveryClientConfiguration.java View source code
private ClientHttpRequestFactory getClientRequestFactory() {
    String username = latticeProperties.getReceptor().getUsername();
    String password = latticeProperties.getReceptor().getPassword();
    return new TestRestTemplate(username, password).getRequestFactory();
}
Example 28
Project: sipgate-io-spring-boot-master  File: SipgateIoIntegrationTests.java View source code
@Test
public void testHome() throws Exception {
    ResponseEntity<String> entity = new TestRestTemplate().getForEntity(eac.getInsideRootUrl(), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertEquals("Hello World", entity.getBody());
}
Example 29
Project: spring-boot-jade-master  File: AutoApplicationTests.java View source code
@Test
public void testHomePage() throws Exception {
    String body = new TestRestTemplate().getForObject("http://localhost:" + port, String.class);
    assertTrue(body.contains("<title>Hello App</title>"));
    assertTrue(body.contains("<p>Hello World</p>"));
}
Example 30
Project: enhanced-pet-clinic-master  File: BaseTests.java View source code
protected ResponseEntity<String> getPage(String url) {
    ResponseEntity<String> page = new TestRestTemplate().exchange(url, HttpMethod.GET, new HttpEntity<Void>(httpHeaders), String.class);
    saveCSRFAndCookieValues(page);
    return page;
}
Example 31
Project: spring-oauth2-samples-master  File: HttpTestUtils.java View source code
public RestOperations createRestTemplate() {
    RestTemplate client = new TestRestTemplate();
    return client;
}