@Test public void testHome() throws Exception { SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); TestRestTemplate testRestTemplate = new TestRestTemplate(); ((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()) .setHttpClient(httpClient); ResponseEntity<String> entity = testRestTemplate .getForEntity("https://localhost:" + this.port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue( "Result is not Matched with Server Response",entity.getBody().contains("welcome to the application")); }
@Test public void envPostAvailable() { MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = new TestRestTemplate().postForEntity( "http://localhost:" + port + "/admin/env", form, Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); }
@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, HttpMethod.GET, requestEntity, byte[].class); assertEquals(HttpStatus.OK, entity.getStatusCode()); GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody())); try { assertEquals("welcome to the application "+System.getProperty("user.name"), StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))); } finally { inflater.close(); } }
@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, HttpMethod.GET, requestEntity, byte[].class); assertEquals(HttpStatus.OK, entity.getStatusCode()); GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody())); try { assertEquals("welcome to the application Adarsh", StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))); } finally { inflater.close(); } }
@Test public void testWelcome() throws Exception { SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); TestRestTemplate testRestTemplate = new TestRestTemplate(); ((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()) .setHttpClient(httpClient); ResponseEntity<String> entity = testRestTemplate .getForEntity("https://localhost:" + this.port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("welcome to the application "+System.getProperty("user.name"), entity.getBody()); }
@Test public void testHome() throws Exception { SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); TestRestTemplate testRestTemplate = new TestRestTemplate(); ((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()) .setHttpClient(httpClient); ResponseEntity<String> entity = testRestTemplate .getForEntity("https://localhost:" + this.port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello, world", entity.getBody()); }
@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, HttpMethod.GET, requestEntity, byte[].class); assertEquals(HttpStatus.OK, entity.getStatusCode()); GZIPInputStream inflater = new GZIPInputStream( new ByteArrayInputStream(entity.getBody())); try { assertEquals("Hello World", StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))); } finally { inflater.close(); } }
@Test public void testDenied() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.set("username", "user"); form.set("password", "user"); getCsrf(form, headers); ResponseEntity<String> entity = new TestRestTemplate().exchange( "http://localhost:" + this.port + "/login", HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class); assertEquals(HttpStatus.FOUND, entity.getStatusCode()); String cookie = entity.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); ResponseEntity<String> page = new TestRestTemplate().exchange( entity.getHeaders().getLocation(), HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertEquals(HttpStatus.FORBIDDEN, page.getStatusCode()); assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(), page.getBody().contains("Access denied")); }
@Test public void testHome() throws Exception { SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); TestRestTemplate testRestTemplate = new TestRestTemplate(); ((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory()) .setHttpClient(httpClient); ResponseEntity<String> entity = testRestTemplate .getForEntity("https://localhost:" + this.port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("Hello World", entity.getBody()); }
@Test public void testMetricsIsSecure() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = new TestRestTemplate() .getForEntity("http://localhost:" + this.port + "/metrics", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); entity = new TestRestTemplate() .getForEntity("http://localhost:" + this.port + "/metrics/", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.port + "/metrics/foo", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.port + "/metrics.json", Map.class); assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode()); }
@Test public void testDenied() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); MultiValueMap<String, String> form = new LinkedMultiValueMap<>(); form.set("username", "admin"); form.set("password", "admin"); getCsrf(form, headers); ResponseEntity<String> entity = new TestRestTemplate().exchange( "http://localhost:" + this.port + "/login", HttpMethod.POST, new HttpEntity<>(form, headers), String.class); assertEquals(HttpStatus.FOUND, entity.getStatusCode()); String cookie = entity.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); ResponseEntity<String> page = new TestRestTemplate().exchange(entity.getHeaders() .getLocation(), HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertEquals(HttpStatus.OK, page.getStatusCode()); cookie = entity.getHeaders().getFirst("Set-Cookie"); assertTrue(cookie.contains("remember-me")); assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(), page .getBody().contains("Invalid username and password")); }
@Test public void testTrace() throws Exception { new TestRestTemplate().getForEntity("http://localhost:" + this.port + "/health", String.class); @SuppressWarnings("rawtypes") ResponseEntity<List> entity = new TestRestTemplate("user", getPassword()) .getForEntity("http://localhost:" + this.port + "/trace", List.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); @SuppressWarnings("unchecked") List<Map<String, Object>> list = entity.getBody(); Map<String, Object> trace = list.get(list.size() - 1); @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) trace .get("info")).get("headers")).get("response"); assertEquals("200", map.get("status")); }
@Test public void configurationAvailable() { @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( "http://localhost:" + port + "/app/cloud", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); }
@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)); }
@Test public void test_authenticate_failure() 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, "wrong password")); 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)); }
@Test public void loginSucceeds() { RestTemplate template = new TestRestTemplate("user", "foo"); ResponseEntity<String> response = template.getForEntity("http://localhost:" + port + "/user", String.class); assertEquals(HttpStatus.OK, response.getStatusCode()); }
@Test public void testHome() throws Exception { ResponseEntity<String> entity = new TestRestTemplate() .getForEntity("http://localhost:" + this.port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("welcome to the application "+System.getProperty("user.name"), entity.getBody()); }
@Test public void testHome() throws Exception { ResponseEntity<String> entity = new TestRestTemplate() .getForEntity("http://localhost:" + this.port, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("welcome to the application Adarsh", entity.getBody()); }
@Before public void setup() { try { this.base = new URL("http://" + this.hostname + ":" + this.port + "/"); this.restTemplate = new TestRestTemplate(); } catch (MalformedURLException exception) { log.error("Could not open a test server socket based on the hostname and port provided"); log.trace("Test server socket couldn't be formed", exception); Assert.fail(); } }
@Test public void testTestEndpointWithQueryParams() throws Exception { TestRestTemplate testRestTemplate = new TestRestTemplate(); ResponseEntity<String> response = testRestTemplate .getForEntity("http://localhost:" + this.port + "/api/tasks?filter[tasks][name]=John", String.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertThatJson(response.getBody()).node("data[0].attributes.name").isStringEqualTo("John"); assertThatJson(response.getBody()).node("data[0].links.self").isStringEqualTo("http://localhost:8080/api/tasks/1"); assertThatJson(response.getBody()).node("meta.name").isStringEqualTo("meta information"); }
@Test public void testTestCustomEndpoint() throws Exception { TestRestTemplate testRestTemplate = new TestRestTemplate(); ResponseEntity<String> response = testRestTemplate .getForEntity("http://localhost:" + this.port + "/api/custom", String.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals(response.getBody(), "hello"); }
@Test public void slashFooReturnsFoo() { ResponseEntity<String> entity = new TestRestTemplate() .getForEntity("http://localhost:" + this.port + "/amigos", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals("si", entity.getBody()); }
@Before public void init() throws TopicRpcException { restTemplate = new TestRestTemplate(); request = TopicRequest.build(operatorId, "sfa", "hengda", TopicOwnerType.GROUP, TopicType.values()); // 自定义数据 Map<String, List<String>> headers = Maps.newHashMap(); headers.put("appId", Lists.newArrayList("sfa")); headers.put("tenantId", Lists.newArrayList("hengda")); headers.put("operatorId", Lists.newArrayList(operatorId.toString())); CommHeaderInterceptor headerInterceptor = new CommHeaderInterceptor(headers); restTemplate.getInterceptors().add(headerInterceptor); this.testDeleteTopics(); }
@Before public void init() { restTemplate = new TestRestTemplate(); // 自定义数据 Map<String, List<String>> headers = Maps.newHashMap(); headers.put("appId", Lists.newArrayList("sfa")); headers.put("tenantId", Lists.newArrayList("hengda")); headers.put("operatorId", Lists.newArrayList("1")); CommHeaderInterceptor headerInterceptor = new CommHeaderInterceptor(headers); restTemplate.getInterceptors().add(headerInterceptor); }
@Test public void persist_order_successfully() throws IOException { Order order = JAXB.unmarshal(new ClassPathResource("order.xml").getFile(), Order.class); RestTemplate restTemplate = new TestRestTemplate(); ResponseEntity<OrderEntity> response = restTemplate.postForEntity( format("http://localhost:%d/orders", port), order, OrderEntity.class); assertThat("Placing order did not return 200 OK", response.getStatusCode(), equalTo(OK)); assertNotNull("Persisted order Id cannot be null", response.getBody().getId()); }
@Test public void testInfo() throws Exception { @SuppressWarnings("rawtypes") ResponseEntity<Map> entity = new TestRestTemplate().getForEntity( "http://localhost:" + this.mgt + "/info", Map.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); }
@PostConstruct public void init() { DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setBaseUrl(url + ":" + port); handler.setParsePath(true); client = new TestRestTemplate(); client.setUriTemplateHandler(handler); }
@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>")); }