public void assertContent(String url, int port, Object expected) throws Exception { SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); ClientHttpRequest request = clientHttpRequestFactory.createRequest( new URI("http://localhost:" + port + url), HttpMethod.GET); try { ClientHttpResponse response = request.execute(); try { String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")); if (expected instanceof Matcher) { assertThat(actual).is(Matched.by((Matcher<?>) expected)); } else { assertThat(actual).isEqualTo(expected); } } finally { response.close(); } } catch (Exception ex) { if (expected == null) { if (SocketException.class.isInstance(ex) || FileNotFoundException.class.isInstance(ex)) { return; } } throw ex; } }
public boolean hasHeader(String url, int port, String header) throws Exception { SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); ClientHttpRequest request = clientHttpRequestFactory.createRequest( new URI("http://localhost:" + port + url), HttpMethod.GET); ClientHttpResponse response = request.execute(); return response.getHeaders().containsKey(header); }
@Test public void interceptShouldAddHeader() throws Exception { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET); ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); byte[] body = new byte[] {}; new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body, execution); verify(execution).execute(request, body); assertEquals("Basic c3ByaW5nOmJvb3Q=", request.getHeaders().getFirst("Authorization")); }
private void doTest(AnnotationConfigEmbeddedWebApplicationContext context, String resourcePath) throws Exception { SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); ClientHttpRequest request = clientHttpRequestFactory.createRequest( new URI( "http://localhost:" + context.getEmbeddedServletContainer().getPort() + resourcePath), HttpMethod.GET); ClientHttpResponse response = request.execute(); try { String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")); assertThat(actual).isEqualTo("Hello World"); } finally { response.close(); } }