private static Map<String, Object> getOpenidConfiguration(String issuer) { ParameterizedTypeReference<Map<String, Object>> typeReference = new ParameterizedTypeReference<Map<String, Object>>() {}; RestTemplate rest = new RestTemplate(); try { URI uri = UriComponentsBuilder.fromUriString(issuer + "/.well-known/openid-configuration") .build() .toUri(); RequestEntity<Void> request = RequestEntity.get(uri).build(); return rest.exchange(request, typeReference).getBody(); } catch(RuntimeException e) { throw new IllegalArgumentException("Unable to resolve the OpenID Configuration with the provided Issuer of " + "\"" + issuer + "\"", e); } }
@Test public void get() { RequestEntity<Void> requestEntity = RequestEntity.get(URI.create("http://example.com")).accept( MediaType.IMAGE_GIF, MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG).build(); assertNotNull(requestEntity); assertEquals(HttpMethod.GET, requestEntity.getMethod()); assertTrue(requestEntity.getHeaders().containsKey("Accept")); assertEquals("image/gif, image/jpeg, image/png", requestEntity.getHeaders().getFirst("Accept")); assertNull(requestEntity.getBody()); }
@Test public void uriVariablesExpansion() throws URISyntaxException { URI uri = new UriTemplate("http://example.com/{foo}").expand("bar"); RequestEntity.get(uri).accept(MediaType.TEXT_PLAIN).build(); String url = "http://www.{host}.com/{path}"; String host = "example"; String path = "foo/bar"; URI expected = new URI("http://www.example.com/foo/bar"); uri = new UriTemplate(url).expand(host, path); RequestEntity<?> entity = RequestEntity.get(uri).build(); assertEquals(expected, entity.getUrl()); Map<String, String> uriVariables = new HashMap<>(2); uriVariables.put("host", host); uriVariables.put("path", path); uri = new UriTemplate(url).expand(uriVariables); entity = RequestEntity.get(uri).build(); assertEquals(expected, entity.getUrl()); }
@Test public void methods() throws URISyntaxException { URI url = new URI("http://example.com"); RequestEntity<?> entity = RequestEntity.get(url).build(); assertEquals(HttpMethod.GET, entity.getMethod()); entity = RequestEntity.post(url).build(); assertEquals(HttpMethod.POST, entity.getMethod()); entity = RequestEntity.head(url).build(); assertEquals(HttpMethod.HEAD, entity.getMethod()); entity = RequestEntity.options(url).build(); assertEquals(HttpMethod.OPTIONS, entity.getMethod()); entity = RequestEntity.put(url).build(); assertEquals(HttpMethod.PUT, entity.getMethod()); entity = RequestEntity.patch(url).build(); assertEquals(HttpMethod.PATCH, entity.getMethod()); entity = RequestEntity.delete(url).build(); assertEquals(HttpMethod.DELETE, entity.getMethod()); }
@Test public void testRequestToFooHandler() throws Exception { URI url = new URI("http://localhost:" + this.port + "/foo"); RequestEntity<Void> request = RequestEntity.get(url).build(); ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertArrayEquals("foo".getBytes("UTF-8"), response.getBody()); }
@Test public void testRequestToBarHandler() throws Exception { URI url = new URI("http://localhost:" + this.port + "/bar"); RequestEntity<Void> request = RequestEntity.get(url).build(); ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertArrayEquals("bar".getBytes("UTF-8"), response.getBody()); }
private RequestEntity<Void> prepareGet(String url, HttpHeaders headers) throws Exception { URI uri = new URI("http://localhost:" + this.port + url); RequestEntity.HeadersBuilder<?> builder = get(uri); addHeaders(builder, headers); return builder.build(); }
@Test @Ignore // TODO: fragile due to socket failures public void basicTest() throws Exception { URI url = new URI("http://localhost:" + port); ResponseEntity<String> response = new RestTemplate().exchange( RequestEntity.get(url).build(), String.class); assertThat(response.getBody(), Matchers.equalTo("hello")); }
@Test public void testHandlerNotFound() throws Exception { URI url = new URI("http://localhost:" + this.port + "/oops"); RequestEntity<Void> request = RequestEntity.get(url).build(); try { new RestTemplate().exchange(request, byte[].class); } catch (HttpClientErrorException ex) { assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode()); } }
@Test public void forwardedHeaders() { // One integration test to verify triggering of Forwarded header support. // More fine-grained tests in ForwardedHeaderTransformerTests. RequestEntity<Void> request = RequestEntity .get(URI.create("http://localhost:" + this.port + "/uri")) .header("Forwarded", "host=84.198.58.199;proto=https") .build(); ResponseEntity<String> entity = getRestTemplate().exchange(request, String.class); assertEquals("https://84.198.58.199/uri", entity.getBody()); }
@Test public void testRequestToHeaderSettingHandler() throws Exception { URI url = new URI("http://localhost:" + this.port + "/header"); RequestEntity<Void> request = RequestEntity.get(url).build(); ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals("bar", response.getHeaders().getFirst("foo")); }
@Test public void etagCheckWithNotModifiedResponse() throws Exception { URI uri = new URI("http://localhost:" + this.port + "/html"); RequestEntity<Void> request = RequestEntity.get(uri).ifNoneMatch("\"deadb33f8badf00d\"").build(); ResponseEntity<String> response = getRestTemplate().exchange(request, String.class); assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode()); assertNull(response.getBody()); }
@Test // SPR-15291 public void redirect() throws Exception { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory() { @Override protected void prepareConnection(HttpURLConnection conn, String method) throws IOException { super.prepareConnection(conn, method); conn.setInstanceFollowRedirects(false); } }; URI uri = new URI("http://localhost:" + this.port + "/redirect"); RequestEntity<Void> request = RequestEntity.get(uri).accept(MediaType.ALL).build(); ResponseEntity<Void> response = new RestTemplate(factory).exchange(request, Void.class); assertEquals(HttpStatus.SEE_OTHER, response.getStatusCode()); assertEquals("/", response.getHeaders().getLocation().toString()); }
@SuppressWarnings("unchecked") @Test public void basicTest() throws Exception { URI url = new URI("http://localhost:" + port); String header = "SID=31d4d96e407aad42; lang=en-US"; ResponseEntity<Void> response = new RestTemplate().exchange( RequestEntity.get(url).header("Cookie", header).build(), Void.class); Map<String, List<HttpCookie>> requestCookies = this.cookieHandler.requestCookies; assertEquals(2, requestCookies.size()); List<HttpCookie> list = requestCookies.get("SID"); assertEquals(1, list.size()); assertEquals("31d4d96e407aad42", list.iterator().next().getValue()); list = requestCookies.get("lang"); assertEquals(1, list.size()); assertEquals("en-US", list.iterator().next().getValue()); List<String> headerValues = response.getHeaders().get("Set-Cookie"); assertEquals(2, headerValues.size()); assertThat(splitCookie(headerValues.get(0)), containsInAnyOrder(equalTo("SID=31d4d96e407aad42"), equalToIgnoringCase("Path=/"), equalToIgnoringCase("Secure"), equalToIgnoringCase("HttpOnly"))); assertThat(splitCookie(headerValues.get(1)), containsInAnyOrder(equalTo("lang=en-US"), equalToIgnoringCase("Path=/"), equalToIgnoringCase("Domain=example.com"))); }
@Test public void invalidate() throws Exception { // First request: no session yet, new session created RequestEntity<Void> request = RequestEntity.get(createUri()).build(); ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); String id = extractSessionId(response.getHeaders()); assertNotNull(id); // Second request: invalidates session URI uri = new URI("http://localhost:" + this.port + "/?invalidate"); request = RequestEntity.get(uri).header("Cookie", "SESSION=" + id).build(); response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); String value = response.getHeaders().getFirst("Set-Cookie"); assertNotNull(value); assertTrue("Actual value: " + value, value.contains("Max-Age=0")); }
@Test public void zeroCopy() throws Exception { // Zero-copy only does not support servlet assumeTrue(server instanceof ReactorHttpServer || server instanceof UndertowHttpServer); URI url = new URI("http://localhost:" + port); RequestEntity<?> request = RequestEntity.get(url).build(); ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class); Resource logo = new ClassPathResource("spring.png", ZeroCopyIntegrationTests.class); assertTrue(response.hasBody()); assertEquals(logo.contentLength(), response.getHeaders().getContentLength()); assertEquals(logo.contentLength(), response.getBody().length); assertEquals(MediaType.IMAGE_PNG, response.getHeaders().getContentType()); }
@Test public void expiredSessionEnds() throws Exception { // First request: no session yet, new session created RequestEntity<Void> request = RequestEntity.get(createUri()).build(); ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); String id = extractSessionId(response.getHeaders()); assertNotNull(id); // Now fast-forward by 31 minutes InMemoryWebSessionStore store = (InMemoryWebSessionStore) this.sessionManager.getSessionStore(); store.setClock(Clock.offset(store.getClock(), Duration.ofMinutes(31))); // Second request: session expires URI uri = new URI("http://localhost:" + this.port + "/?expire"); request = RequestEntity.get(uri).header("Cookie", "SESSION=" + id).build(); response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); String value = response.getHeaders().getFirst("Set-Cookie"); assertNotNull(value); assertTrue("Actual value: " + value, value.contains("Max-Age=0")); }
@Test public void createSession() throws Exception { RequestEntity<Void> request = RequestEntity.get(createUri()).build(); ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); String id = extractSessionId(response.getHeaders()); assertNotNull(id); assertEquals(1, this.handler.getSessionRequestCount()); request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build(); response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertNull(response.getHeaders().get("Set-Cookie")); assertEquals(2, this.handler.getSessionRequestCount()); }
@Test public void changeSessionId() throws Exception { // First request: no session yet, new session created RequestEntity<Void> request = RequestEntity.get(createUri()).build(); ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); String oldId = extractSessionId(response.getHeaders()); assertNotNull(oldId); assertEquals(1, this.handler.getSessionRequestCount()); // Second request: session id changes URI uri = new URI("http://localhost:" + this.port + "/?changeId"); request = RequestEntity.get(uri).header("Cookie", "SESSION=" + oldId).build(); response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); String newId = extractSessionId(response.getHeaders()); assertNotNull("Expected new session id", newId); assertNotEquals(oldId, newId); assertEquals(2, this.handler.getSessionRequestCount()); }
@Test public void expiredSessionIsRecreated() throws Exception { // First request: no session yet, new session created RequestEntity<Void> request = RequestEntity.get(createUri()).build(); ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); String id = extractSessionId(response.getHeaders()); assertNotNull(id); assertEquals(1, this.handler.getSessionRequestCount()); // Second request: same session request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build(); response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertNull(response.getHeaders().get("Set-Cookie")); assertEquals(2, this.handler.getSessionRequestCount()); // Now fast-forward by 31 minutes InMemoryWebSessionStore store = (InMemoryWebSessionStore) this.sessionManager.getSessionStore(); WebSession session = store.retrieveSession(id).block(); assertNotNull(session); store.setClock(Clock.offset(store.getClock(), Duration.ofMinutes(31))); // Third request: expired session, new session created request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build(); response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); id = extractSessionId(response.getHeaders()); assertNotNull("Expected new session id", id); assertEquals(1, this.handler.getSessionRequestCount()); }