/**
  * For the routing to work correctly we need to have a context set and the pathinfo being part of
  * the request uri. Without this the wrong matching got done in {@link
  * org.springframework.web.util.UrlPathHelper.getPathWithinApplication( HttpServletRequest)}
  *
  * @param method The request method (GET/POST/etc)
  * @param pathInfo The path info
  * @return A request to a context of /context with the path info appended.
  */
 public static MockHttpServletRequest newRequest(String method, String pathInfo) {
   MockHttpServletRequest request = new MockHttpServletRequest();
   request.setMethod(method);
   request.setContextPath("/context");
   request.setRequestURI("/context" + pathInfo);
   request.setPathInfo(pathInfo);
   return request;
 }
  protected MockHttpServletRequest createHttpRequest(String pathInfo) {
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setMethod(HttpMethods.GET);
    mockHttpServletRequest.setPathInfo(pathInfo);

    return mockHttpServletRequest;
  }
 @Test
 public void getComponentIdWithJspPathInfo() {
   MockHttpServletRequest request =
       new MockHttpServletRequest(
           "GET", "http://localhost:8000/silverpeas/jsp/javaScript/forums.js");
   request.setPathInfo("/jsp/javaScript/forums.js");
   SilverpeasWebUtil util = new SilverpeasWebUtil();
   String[] result = util.getComponentId(request);
   String[] expResult = new String[] {null, null, "javaScript/forums.js"};
   assertArrayEquals(expResult, result);
 }
 /** Test of getComponentId method, of class SilverpeasWebUtil. */
 @Test
 public void getComponentIdForURLWithFunction() {
   SilverpeasWebUtil util = new SilverpeasWebUtil();
   MockHttpServletRequest request =
       new MockHttpServletRequest(
           "GET", "http://localhost:8000/silverpeas/Rtoolbox/toolbox8/ViewAttachments");
   request.setPathInfo("/toolbox8/ViewAttachments");
   String[] expResult = new String[] {null, "toolbox8", "ViewAttachments"};
   String[] result = util.getComponentId(request);
   assertArrayEquals(expResult, result);
 }
  protected void testGetI18nData(Locale locale, I18nServlet.I18nData expectedI18nData)
      throws Exception {

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setPathInfo(StringPool.SLASH);
    mockHttpServletRequest.setServletPath(StringPool.SLASH + LocaleUtil.toLanguageId(locale));

    I18nServlet.I18nData actualI18nData = _i18nServlet.getI18nData(mockHttpServletRequest);

    Assert.assertEquals(expectedI18nData, actualI18nData);
  }
 @Test
 public void getComponentIdForMainURL() {
   MockHttpServletRequest request =
       new MockHttpServletRequest(
           "GET", "http://localhost:8000/silverpeas/Rtoolbox/toolbox8/Main");
   request.setPathInfo("/toolbox8/Main");
   OrganizationController controller = getOrganisationController();
   ComponentInstLight component = mock(ComponentInstLight.class);
   String spaceId = "12";
   when(component.getDomainFatherId()).thenReturn(spaceId);
   when(controller.getComponentInstLight("toolbox8")).thenReturn(component);
   SilverpeasWebUtil util = new SilverpeasWebUtil();
   String[] result = util.getComponentId(request);
   String[] expResult = new String[] {"12", "toolbox8", "Main"};
   assertArrayEquals(expResult, result);
 }
示例#7
0
  public Tuple service(String method, String path, Map<String, String> headers, byte[] data) {

    WebDAVStorage webDAVStorage = new DLWebDAVStorageImpl();

    webDAVStorage.setToken("document_library");

    WebDAVUtil.addStorage(webDAVStorage);

    WebDAVServlet webDAVServlet = new WebDAVServlet();

    String requestURI = _CONTEXT_PATH + _SERVLET_PATH + _PATH_INFO_PREFACE + path;

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, requestURI);

    mockHttpServletRequest.setContextPath(_CONTEXT_PATH);
    mockHttpServletRequest.setServletPath(_SERVLET_PATH);
    mockHttpServletRequest.setPathInfo(_PATH_INFO_PREFACE + path);

    try {
      mockHttpServletRequest.setRemoteUser(String.valueOf(TestPropsValues.getUserId()));
    } catch (Exception e) {
      Assert.fail("User ID cannot be initialized");
    }

    if (headers == null) {
      headers = new HashMap<String, String>();
    }

    headers.put(HttpHeaders.USER_AGENT, getUserAgent());

    try {
      throw new Exception();
    } catch (Exception e) {
      StackTraceElement[] stackTraceElements = e.getStackTrace();

      for (StackTraceElement stackTraceElement : stackTraceElements) {
        String methodName = stackTraceElement.getMethodName();

        if (methodName.equals("setUp")
            || methodName.equals("tearDown")
            || methodName.startsWith("test")) {

          String testName =
              StringUtil.extractLast(stackTraceElement.getClassName(), CharPool.PERIOD);

          testName =
              StringUtil.replace(testName, new String[] {"WebDAV", "Test"}, new String[] {"", ""});

          headers.put(
              "X-Litmus",
              testName
                  + ": ("
                  + stackTraceElement.getMethodName()
                  + ":"
                  + stackTraceElement.getLineNumber()
                  + ")");

          break;
        }
      }
    }

    if (data != null) {
      mockHttpServletRequest.setContent(data);

      String contentType = headers.remove(HttpHeaders.CONTENT_TYPE);

      if (contentType != null) {
        mockHttpServletRequest.setContentType(contentType);
      } else {
        mockHttpServletRequest.setContentType(ContentTypes.TEXT_PLAIN);
      }
    }

    for (Map.Entry<String, String> entry : headers.entrySet()) {
      String key = entry.getKey();
      String value = entry.getValue();

      mockHttpServletRequest.addHeader(key, value);
    }

    try {
      MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

      webDAVServlet.service(mockHttpServletRequest, mockHttpServletResponse);

      int statusCode = mockHttpServletResponse.getStatus();
      byte[] responseBody = mockHttpServletResponse.getContentAsByteArray();

      Map<String, String> responseHeaders = new HashMap<String, String>();

      for (String name : mockHttpServletResponse.getHeaderNames()) {
        responseHeaders.put(name, (String) mockHttpServletResponse.getHeader(name));
      }

      return new Tuple(statusCode, responseBody, responseHeaders);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }
 private void setPathInfoAndRequestURI(
     MockHttpServletRequest httpServletRequest, String imageRequestPath) {
   httpServletRequest.setRequestURI("/admin/" + imageRequestPath);
   httpServletRequest.setPathInfo("/" + imageRequestPath);
 }
示例#9
0
 private void setPathInfoAndRequestURI(
     MockHttpServletRequest httpServletRequest, String imageRequestPath) {
   httpServletRequest.setRequestURI("/site/" + site1.getKey() + "/" + imageRequestPath);
   httpServletRequest.setPathInfo("/" + site1.getKey() + "/" + imageRequestPath);
 }