List of usage examples for org.springframework.mock.web DelegatingServletInputStream DelegatingServletInputStream
public DelegatingServletInputStream(InputStream sourceStream)
From source file:org.appverse.web.framework.backend.frontfacade.rest.authentication.servlet.JWSHttpServletRequestWrapperTest.java
@Test public void testWrapperObtainContent() throws Exception { //environment ServletInputStream sis = new DelegatingServletInputStream( new ByteArrayInputStream(TEST_CONTENT.getBytes())); when(request.getInputStream()).thenReturn(sis); //test//from ww w .jav a2 s . c o m JWSHttpServletRequestWrapper jwsHttpServletRequestWrapper = new JWSHttpServletRequestWrapper(request, null); InputStream is = jwsHttpServletRequestWrapper.getInputStream(); //validation String obtainedContent = obtainContent(is); Assert.assertNotNull("content should be not be null", obtainedContent); Assert.assertEquals("content should be the same", TEST_CONTENT, obtainedContent); }
From source file:org.appverse.web.framework.backend.frontfacade.rest.authentication.filter.JWSAuthenticationProcessFilterTest.java
@Test public void testJWSAuthenticationFilterFailInvalidHeader() throws Exception { //environement String content = ""; String requestURL = "http://localhost:8080"; ServletInputStream emptyContent = new DelegatingServletInputStream( new ByteArrayInputStream(content.getBytes())); ArgumentCaptor<Integer> errorCode = ArgumentCaptor.forClass(Integer.class); when(request.getHeader(JWSAuthenticationProcessingFilter.JWS_AUTH_HEADER)) .thenReturn(JWSAuthenticationProcessingFilter.JWS_AUTH_HEADER_TOKEN_MARK + "invalidHash"); when(request.getInputStream()).thenReturn(emptyContent); when(request.getRequestURL()).thenReturn(new StringBuffer(requestURL)); //test/*from w w w .ja v a2 s . c o m*/ myJWSFilter.doFilter(request, response, chain); //verify verify(chain, times(0)).doFilter(any(ServletRequest.class), any(ServletResponse.class)); verify(response, times(1)).sendError(errorCode.capture());//check sendError is not set int errorCodeValue = errorCode.getValue().intValue(); logger.info("Response error:{}", errorCodeValue); Assert.assertEquals("sendError should be:", HttpServletResponse.SC_UNAUTHORIZED, errorCodeValue); }
From source file:org.appverse.web.framework.backend.frontfacade.rest.authentication.filter.JWSAuthenticationProcessFilterTest.java
@Test public void testJWSAuthenticationFilterFailInvalidSignature() throws Exception { String a = "eyJhbGciOiJSUzI1NiJ9.aHR0cDovL2xvY2FsaG9zdDo4MDgw.g1naD_1vfSoXXC-KlOLbzQSmfCyO4JySqAyAC4RSGvEHO2v2V0coWjtzIEkCJ-d-JA_xyxc1me7L3q5PC8zx3IGayIgphqx2KO8CddY0RKTkbP6I3WaKZ3LhzTUZiO9MY5ATmTCYT05HWp9zgW-QAhdqTexzLPS5t1rszkmir0U"; String content = ""; String requestURL = "http://someserver:8080"; ServletInputStream emptyContent = new DelegatingServletInputStream( new ByteArrayInputStream(content.getBytes())); ArgumentCaptor<Integer> errorCode = ArgumentCaptor.forClass(Integer.class); when(request.getHeader(JWSAuthenticationProcessingFilter.JWS_AUTH_HEADER)) .thenReturn(JWSAuthenticationProcessingFilter.JWS_AUTH_HEADER_TOKEN_MARK + a); when(request.getInputStream()).thenReturn(emptyContent); when(request.getRequestURL()).thenReturn(new StringBuffer(requestURL)); //test//from w ww . java 2 s. c o m myJWSFilter.doFilter(request, response, chain); verify(chain, times(0)).doFilter(any(ServletRequest.class), any(ServletResponse.class)); verify(response, times(1)).sendError(errorCode.capture());//check sendError is not set int errorCodeValue = errorCode.getValue().intValue(); logger.info("Response error:{}", errorCodeValue); Assert.assertEquals("sendError should be:", HttpServletResponse.SC_UNAUTHORIZED, errorCodeValue); }
From source file:org.appverse.web.framework.backend.frontfacade.rest.authentication.filter.JWSAuthenticationProcessFilterTest.java
@Test public void testJWSAuthenticationFilterHeaderWithNoContent() throws Exception { //empty content and specific url String content = ""; String requestURL = "http://localhost:8080"; ServletInputStream emptyContent = new DelegatingServletInputStream( new ByteArrayInputStream(content.getBytes())); //prepare client KeyStore keyStore = getKeyStoreClient(); Key key = keyStore.getKey(clientCertAlias, clientCertPassword.toCharArray()); JWSJerseyFilter jwsJerseyFilter = new JWSJerseyFilter(); //environment ArgumentCaptor<String> argumentHeader = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> argumentHeaderValue = ArgumentCaptor.forClass(String.class); when(context.getConfiguration()).thenReturn(config); when(config.getRuntimeType()).thenReturn(RuntimeType.CLIENT); when(context.getProperty(JWSJerseyFilter.JWS_FILTER_KEY)).thenReturn(key); when(context.getUri()).thenReturn(new URI(requestURL)); when(context.getHeaders()).thenReturn(headers); when(context.getEntity()).thenReturn(null); //test client jwsJerseyFilter.filter(context);/* w w w . ja va 2 s.c o m*/ //validation client verify(headers, times(1)).add(argumentHeader.capture(), argumentHeaderValue.capture()); String headerKey = argumentHeader.getValue(); String headerValue = argumentHeaderValue.getValue(); Assert.assertTrue("Response from client should contain token", headerValue.contains(JWSJerseyFilter.JWS_AUTHORIZATION_START_TOKEN)); logger.info("Client Header Content: {}={}", headerKey, headerValue); //prepare server when(request.getHeader(headerKey)).thenReturn(headerValue); when(request.getInputStream()).thenReturn(emptyContent); when(request.getRequestURL()).thenReturn(new StringBuffer(requestURL)); //test server myJWSFilter.doFilter(request, response, chain); //validation verify(chain, times(1)).doFilter(any(ServletRequest.class), any(ServletResponse.class)); verify(response, times(0)).sendError(anyInt());//check sendError is not set }
From source file:org.appverse.web.framework.backend.frontfacade.rest.authentication.filter.JWSAuthenticationProcessFilterTest.java
@Test public void testJWSAuthenticationFilterHeaderWithContent() throws Exception { //some content and specific url final String content = "{\n" + "\t\"id\": \"0001\",\n" + "\t\"type\": \"donut\",\n" + "\t\"name\": \"Cake\",\n" + "\t\"image\":\n" + "\t\t{\n" + "\t\t\t\"url\": \"images/0001.jpg\",\n" + "\t\t\t\"width\": 200,\n" + "\t\t\t\"height\": 200\n" + "\t\t},\n" + "\t\"thumbnail\":\n" + "\t\t{\n" + "\t\t\t\"url\": \"images/thumbnails/0001.jpg\",\n" + "\t\t\t\"width\": 32,\n" + "\t\t\t\"height\": 32\n" + "\t\t}\n" + "}"; String requestURL = "http://localhost:8080"; //prepare client ServletInputStream someContent = new DelegatingServletInputStream( new ByteArrayInputStream(content.getBytes())); KeyStore keyStore = getKeyStoreClient(); Key key = keyStore.getKey(clientCertAlias, clientCertPassword.toCharArray()); JWSJerseyFilter jwsJerseyFilter = new JWSJerseyFilter(); //environment ArgumentCaptor<String> argumentHeader = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> argumentHeaderValue = ArgumentCaptor.forClass(String.class); when(context.getConfiguration()).thenReturn(config); when(config.getRuntimeType()).thenReturn(RuntimeType.CLIENT); when(context.getProperty(JWSJerseyFilter.JWS_FILTER_KEY)).thenReturn(key); when(context.getUri()).thenReturn(new URI(requestURL)); when(context.getHeaders()).thenReturn(headers); when(context.getEntity()).thenReturn(content); doAnswer(new Answer<Void>() { @Override/*from w w w .j a va2s . c o m*/ public Void answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); ((OutputStream) arguments[6]).write(content.getBytes()); ((OutputStream) arguments[6]).flush(); return null; } }).when(messageBodyWriter).writeTo(any(Object.class), any(Class.class), any(Type.class), any(Annotation[].class), any(MediaType.class), any(MultivaluedMap.class), any(OutputStream.class)); /* TODO: Next sentence is not working because of jax.ws.rs version change, it needs to be reviewed when(messageBodyWorkers.getMessageBodyWriter(any(Class.class), any(Type.class), any(Annotation[].class), any(MediaType.class))).thenReturn(messageBodyWriter); */ jwsJerseyFilter.setWorkers(messageBodyWorkers); Type entity = content.getClass(); when(context.getEntityClass()).thenReturn((Class) entity); //test client jwsJerseyFilter.filter(context); //validation client verify(headers, times(1)).add(argumentHeader.capture(), argumentHeaderValue.capture()); String headerKey = argumentHeader.getValue(); String headerValue = argumentHeaderValue.getValue(); Assert.assertTrue("Response from client should contain token", headerValue.contains(JWSJerseyFilter.JWS_AUTHORIZATION_START_TOKEN)); logger.info("Client Header Content: {}={}", headerKey, headerValue); //prepare server when(request.getHeader(headerKey)).thenReturn(headerValue); when(request.getInputStream()).thenReturn(someContent); when(request.getRequestURL()).thenReturn(new StringBuffer(requestURL)); //test server myJWSFilter.doFilter(request, response, chain); //validation verify(chain, times(1)).doFilter(any(ServletRequest.class), any(ServletResponse.class)); verify(response, times(0)).sendError(anyInt());//check sendError is not set }
From source file:org.geoserver.monitor.MonitorServletRequestTest.java
@Test public void testInputStreamMaxSizeZero() throws Exception { byte[] data = data(); DelegatingServletInputStream mock = new DelegatingServletInputStream(new ByteArrayInputStream(data)); MonitorInputStream in = new MonitorInputStream(mock, 0); byte[] read = read(in); assertEquals(data.length, read.length); byte[] buffer = in.getData(); assertEquals(0, buffer.length);//from w ww . ja va 2 s . c om // ? why does this report 1 off ? assertEquals(data.length - 1, in.getBytesRead()); }
From source file:org.geoserver.monitor.MonitorServletRequestTest.java
@Test public void testInputStream() throws Exception { byte[] data = data(); DelegatingServletInputStream mock = new DelegatingServletInputStream(new ByteArrayInputStream(data)); MonitorInputStream in = new MonitorInputStream(mock, 1024); byte[] read = read(in); assertEquals(data.length, read.length); byte[] buffer = in.getData(); assertEquals(1024, buffer.length);// w ww. j av a 2s. c o m for (int i = 0; i < buffer.length; i++) { assertEquals(data[i], buffer[i]); } // ? why does this report 1 off ? assertEquals(data.length - 1, in.getBytesRead()); }