RestMockDispatcher extends AbstractMockDispatcher { @Override public MockResult dispatchRequest(HttpServletRequest request, HttpServletResponse response) { RestMockRequest restMockRequest = null; Object result = null; try { restMockRequest = new RestMockRequest(request, response, mockContext); result = mockService.runOnRequestScript(mockContext, restMockRequest); if (!(result instanceof MockResult)) { result = getMockResult(restMockRequest); } mockService.runAfterRequestScript(mockContext, (MockResult) result); return (MockResult) result; } catch (Exception e) { SoapUI.logError(e, "got an exception while dispatching - returning a default 500 response"); return createServerErrorMockResult(restMockRequest); } finally { mockService.fireOnMockResult(result); } } RestMockDispatcher(RestMockService mockService, WsdlMockRunContext mockContext); @Override MockResult dispatchRequest(HttpServletRequest request, HttpServletResponse response);  }
@Test public void afterRequestScriptIsCalled() throws Exception { when(restMockService.getPath()).thenReturn("/"); when(request.getPathInfo()).thenReturn("/"); RestMockResult mockResult = (RestMockResult) restMockDispatcher.dispatchRequest(request, response); verify(restMockService).runAfterRequestScript(context, mockResult); }
@Test public void onRequestScriptIsCalled() throws Exception { restMockDispatcher.dispatchRequest(request, response); verify(restMockService).runOnRequestScript(any(WsdlMockRunContext.class), any(MockRequest.class)); }
@Test public void onRequestScriptOverridesRegularDispatching() throws Exception { RestMockResult restMockResult = mock(RestMockResult.class); when(restMockService.runOnRequestScript(any(WsdlMockRunContext.class), any(MockRequest.class))).thenReturn(restMockResult); restMockDispatcher.dispatchRequest(request, response); verify(restMockService, never()).findBestMatchedOperation(anyString(), any(HttpMethod.class)); }
@Test public void shouldReturnNoResponseFoundWhenThereIsNoMatchingAction() throws Exception { when(restMockService.findBestMatchedOperation(anyString(), any(HttpMethod.class))).thenReturn(null); when(restMockService.getPath()).thenReturn("/"); when(request.getPathInfo()).thenReturn("/"); restMockDispatcher.dispatchRequest(request, response); verify(response).setStatus(HttpStatus.SC_NOT_FOUND); }
@Test public void shouldResponseWhenServicePathMatches() throws Exception { RestMockAction action = mock(RestMockAction.class); when(restMockService.findBestMatchedOperation("/api", HttpMethod.DELETE)).thenReturn(action); when(restMockService.getPath()).thenReturn("/sweden"); when(request.getPathInfo()).thenReturn("/sweden/api"); restMockDispatcher.dispatchRequest(request, response); verify(action).dispatchRequest(any(RestMockRequest.class)); }
@Test public void shouldResponseWhenPathMatches() throws Exception { RestMockAction action = mock(RestMockAction.class); when(restMockService.findBestMatchedOperation("/api", HttpMethod.DELETE)).thenReturn(action); when(restMockService.getPath()).thenReturn("/"); when(request.getPathInfo()).thenReturn("/api"); restMockDispatcher.dispatchRequest(request, response); verify(action).dispatchRequest(any(RestMockRequest.class)); }
@Test public void returnsErrorOnrequestScriptException() throws Exception { Exception runTimeException = new IllegalStateException("wrong state"); when(restMockService.runOnRequestScript(any(WsdlMockRunContext.class), any(MockRequest.class))).thenThrow(runTimeException); restMockDispatcher.dispatchRequest(request, response); verify(response).setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); }