Java Code Examples for org.springframework.mock.web.MockHttpServletResponse#getStatus()

The following examples show how to use org.springframework.mock.web.MockHttpServletResponse#getStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestUtil.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
public static String sendRequest(WebApplicationContext webApplicationContext,String requestType, String uriWithParam, String ...requestJsons) throws Exception {
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    MockHttpServletResponse mockResponse =null;
    if(ConstantsForTest.POST.equals(requestType)){
        if(requestJsons.length > 0){
            mockResponse =sendPost(mockMvc,uriWithParam,requestJsons[0]);
        }else{
            mockResponse =sendPost(mockMvc,uriWithParam,"");
        }
    }else if(ConstantsForTest.GET.equals(requestType)){
        HttpHeaders httpHeaders=getHeaders(webApplicationContext);
        Cookie cookies =getCookies(webApplicationContext);
        MockHttpSession session=getSession(webApplicationContext);
        mockResponse =sendGet(mockMvc,httpHeaders,cookies,session,uriWithParam);
    }else if(ConstantsForTest.PUT.equals(requestType)){
        if(requestJsons.length > 0){
            mockResponse =sendPut(mockMvc,uriWithParam,requestJsons[0]);
        }
    }
    int status = mockResponse.getStatus();
    String uri = "";
    if (uriWithParam.indexOf(ConstantsForTest.QuestionMark) > 0) {
        uri = uriWithParam.substring(0, uriWithParam.indexOf(ConstantsForTest.QuestionMark));
    } else {
        uri = uriWithParam;
    }
    if (status != 200) {
        LOGGER.error(MessageUtil.getFailureString(uri, status));
        throw new RuntimeException(MessageUtil.getFailureString(uri, status));
    }
    String res = mockResponse.getContentAsString();
    System.out.println(MessageUtil.getSuccessString(uri, res));
    LOGGER.info(MessageUtil.getSuccessString(uri, res));
    return res;
}
 
Example 2
Source File: RequestUtil.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
public static String sendRequest(WebApplicationContext webApplicationContext,String requestType, String uriWithParam, String ...requestJsons) throws Exception {
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    MockHttpServletResponse mockResponse =null;
    if(ConstantsForTest.POST.equals(requestType)){
        if(requestJsons.length > 0){
            mockResponse =sendPost(mockMvc,uriWithParam,requestJsons[0]);
        }else{
            mockResponse =sendPost(mockMvc,uriWithParam,"");
        }
    }else if(ConstantsForTest.GET.equals(requestType)){
        HttpHeaders httpHeaders=getHeaders(webApplicationContext);
        Cookie cookies =getCookies(webApplicationContext);
        MockHttpSession session=getSession(webApplicationContext);
        mockResponse =sendGet(mockMvc,httpHeaders,cookies,session,uriWithParam);
    }else if(ConstantsForTest.PUT.equals(requestType)){
        if(requestJsons.length > 0){
            mockResponse =sendPut(mockMvc,uriWithParam,requestJsons[0]);
        }
    }
    int status = mockResponse.getStatus();
    String uri = "";
    if (uriWithParam.indexOf(ConstantsForTest.QuestionMark) > 0) {
        uri = uriWithParam.substring(0, uriWithParam.indexOf(ConstantsForTest.QuestionMark));
    } else {
        uri = uriWithParam;
    }
    if (status != 200) {
        LOGGER.error(MessageUtil.getFailureString(uri, status));
        throw new RuntimeException(MessageUtil.getFailureString(uri, status));
    }
    String res = mockResponse.getContentAsString();
    System.out.println(MessageUtil.getSuccessString(uri, res));
    LOGGER.info(MessageUtil.getSuccessString(uri, res));
    return res;
}
 
Example 3
Source File: DapTestCommon.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public HttpResponse execute(HttpRequestBase rq) throws IOException {
  URI uri = rq.getURI();
  DapController controller = getController(uri);
  StandaloneMockMvcBuilder mvcbuilder = MockMvcBuilders.standaloneSetup(controller);
  mvcbuilder.setValidator(new TestServlet.NullValidator());
  MockMvc mockMvc = mvcbuilder.build();
  MockHttpServletRequestBuilder mockrb = MockMvcRequestBuilders.get(uri);
  // We need to use only the path part
  mockrb.servletPath(uri.getPath());
  // Move any headers from rq to mockrb
  Header[] headers = rq.getAllHeaders();
  for (int i = 0; i < headers.length; i++) {
    Header h = headers[i];
    mockrb.header(h.getName(), h.getValue());
  }
  // Since the url has the query parameters,
  // they will automatically be parsed and added
  // to the rb parameters.

  // Finally set the resource dir
  mockrb.requestAttr("RESOURCEDIR", this.resourcepath);

  // Now invoke the servlet
  MvcResult result;
  try {
    result = mockMvc.perform(mockrb).andReturn();
  } catch (Exception e) {
    throw new IOException(e);
  }

  // Collect the output
  MockHttpServletResponse res = result.getResponse();
  byte[] byteresult = res.getContentAsByteArray();

  // Convert to HttpResponse
  HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, res.getStatus(), "");
  if (response == null)
    throw new IOException("HTTPMethod.executeMock: Response was null");
  Collection<String> keys = res.getHeaderNames();
  // Move headers to the response
  for (String key : keys) {
    List<String> values = res.getHeaders(key);
    for (String v : values) {
      response.addHeader(key, v);
    }
  }
  ByteArrayEntity entity = new ByteArrayEntity(byteresult);
  String sct = res.getContentType();
  entity.setContentType(sct);
  response.setEntity(entity);
  return response;
}
 
Example 4
Source File: AbstractRepositoryServletTestCase.java    From archiva with Apache License 2.0 4 votes vote down vote up
protected WebResponse getWebResponse( WebRequest webRequest ) //, boolean followRedirect )
    throws Exception
{

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI( webRequest.getUrl().getPath() );
    request.addHeader( "User-Agent", "Apache Archiva unit test" );

    request.setMethod( webRequest.getHttpMethod().name() );

    if ( webRequest.getHttpMethod() == HttpMethod.PUT )
    {
        PutMethodWebRequest putRequest = PutMethodWebRequest.class.cast( webRequest );
        request.setContentType( putRequest.contentType );
        request.setContent( IOUtils.toByteArray( putRequest.inputStream ) );
    }

    if ( webRequest instanceof MkColMethodWebRequest )
    {
        request.setMethod( "MKCOL" );
    }

    final MockHttpServletResponse response = execute( request );

    if ( response.getStatus() == HttpServletResponse.SC_MOVED_PERMANENTLY
        || response.getStatus() == HttpServletResponse.SC_MOVED_TEMPORARILY )
    {
        String location = response.getHeader( "Location" );
        log.debug( "follow redirect to {}", location );
        return getWebResponse( new GetMethodWebRequest( location ) );
    }

    return new WebResponse( null, null, 1 )
    {
        @Override
        public String getContentAsString()
        {
            try
            {
                return response.getContentAsString();
            }
            catch ( UnsupportedEncodingException e )
            {
                throw new RuntimeException( e.getMessage(), e );
            }
        }

        @Override
        public int getStatusCode()
        {
            return response.getStatus();
        }

        @Override
        public String getResponseHeaderValue( String headerName )
        {
            return response.getHeader( headerName );
        }
    };
}
 
Example 5
Source File: CSVResponse.java    From gocd with Apache License 2.0 4 votes vote down vote up
public CSVResponse(MockHttpServletResponse response) throws UnsupportedEncodingException {
    this(response.getContentAsString(), response.getStatus(), response.getContentType());
}