当前位置: 首页>>代码示例>>Java>>正文


Java MockHttpServletRequest.setContent方法代码示例

本文整理汇总了Java中org.springframework.mock.web.MockHttpServletRequest.setContent方法的典型用法代码示例。如果您正苦于以下问题:Java MockHttpServletRequest.setContent方法的具体用法?Java MockHttpServletRequest.setContent怎么用?Java MockHttpServletRequest.setContent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.mock.web.MockHttpServletRequest的用法示例。


在下文中一共展示了MockHttpServletRequest.setContent方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testInvokeMultiPartEntity

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testInvokeMultiPartEntity() throws IOException
{
    ResourceWithMetadata entityResource = locator.locateEntityResource(api,"multiparttest", HttpMethod.POST);
    assertNotNull(entityResource);
    MultiPartResourceAction.Create<?> resource = (MultiPartResourceAction.Create<?>) entityResource.getResource();

    File file = TempFileProvider.createTempFile("ParamsExtractorTests-", ".txt");
    PrintWriter writer = new PrintWriter(file);
    writer.println("Multipart Mock test2.");
    writer.close();

    MultiPartRequest reqBody = MultiPartBuilder.create()
                .setFileData(new FileData(file.getName(), file, MimetypeMap.MIMETYPE_TEXT_PLAIN))
                .build();

    MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", "");
    mockRequest.setContent(reqBody.getBody());
    mockRequest.setContentType(reqBody.getContentType());

    String out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api,null, NOT_USED, resource.create(new FormData(mockRequest), NOT_USED, callBack)));
    assertTrue("There must be json output", StringUtils.startsWith(out, "{\"entry\":"));
}
 
开发者ID:Alfresco,项目名称:alfresco-remote-api,代码行数:24,代码来源:SerializeTests.java

示例2: verifyRequestBody

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyRequestBody() {
    final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
        + "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
        + "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
        + "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContent(body.getBytes());

    final SamlService impl = new SamlServiceFactory().createService(request);
    assertEquals("artifact", impl.getArtifactId());
    assertEquals("_192.168.16.51.1024506224022", impl.getRequestID());
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:14,代码来源:SamlServiceTests.java

示例3: verifyRequestBody

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyRequestBody() {
    final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
        + "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
        + "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
        + "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContent(body.getBytes());

    final SamlService impl = SamlService.createServiceFrom(request);
    assertEquals("artifact", impl.getArtifactId());
    assertEquals("_192.168.16.51.1024506224022", impl.getRequestID());
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:14,代码来源:SamlServiceTests.java

示例4: verifyRequestBody

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifyRequestBody() {
    final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
            + "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
            + "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContent(body.getBytes());

    final SamlService impl = new SamlServiceFactory().createService(request);
    assertEquals("artifact", impl.getArtifactId());
    assertEquals("_192.168.16.51.1024506224022", impl.getRequestID());
}
 
开发者ID:mrluo735,项目名称:cas-5.1.0,代码行数:14,代码来源:SamlServiceTests.java

示例5: verifySerializeASamlServiceToJson

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void verifySerializeASamlServiceToJson() throws IOException {
    final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
            + "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
            + "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContent(body.getBytes());

    final SamlService serviceWritten = new SamlServiceFactory().createService(request);
    MAPPER.writeValue(JSON_FILE, serviceWritten);
    final SamlService serviceRead = MAPPER.readValue(JSON_FILE, SamlService.class);
    assertEquals(serviceWritten, serviceRead);
}
 
开发者ID:mrluo735,项目名称:cas-5.1.0,代码行数:15,代码来源:SamlServiceTests.java

示例6: testRequestBody

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testRequestBody() {
    final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
        + "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
        + "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
        + "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContent(body.getBytes());

    final SamlService impl = SamlService.createServiceFrom(request);
    assertEquals("artifact", impl.getArtifactId());
    assertEquals("_192.168.16.51.1024506224022", impl.getRequestID());
}
 
开发者ID:luotuo,项目名称:cas4.0.x-server-wechat,代码行数:14,代码来源:SamlServiceTests.java

示例7: buildRequestLog_WithCorrectBody_ReturnStringRequestWithUriAndBody

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void buildRequestLog_WithCorrectBody_ReturnStringRequestWithUriAndBody() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod(HttpMethod.GET.name());
	request.setRequestURI("/dummy");
	request.setContent("test".getBytes());

	String log = FILTER.buildRequestLog(new ContentCachingRequestWrapper(request));

	assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n > Content-Length: 4\ntest"));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:12,代码来源:BaseLoggingFilterTest.java

示例8: buildRequestLog_WithBadCacheRequest_ReturnStringRequestWithUriAndUnknownBody

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void buildRequestLog_WithBadCacheRequest_ReturnStringRequestWithUriAndUnknownBody() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod(HttpMethod.GET.name());
	request.setRequestURI("/dummy");
	request.setContent("test".getBytes());

	String log = FILTER.buildRequestLog(request);

	assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n > Content-Length: 4\n[unknown]"));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:12,代码来源:BaseLoggingFilterTest.java

示例9: buildRequestLog_WithUnreadableBody_ReturnStringRequestWithUriAndUnknownBody

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void buildRequestLog_WithUnreadableBody_ReturnStringRequestWithUriAndUnknownBody() throws IOException {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod(HttpMethod.GET.name());
	request.setRequestURI("/dummy");
	request.setContent(nextBytes(4));

	ContentCachingRequestWrapper requestWrapper = spy(new ContentCachingRequestWrapper(request));
	when(requestWrapper.getReader()).thenThrow(new UnsupportedEncodingException());

	String log = FILTER.buildRequestLog(requestWrapper);

	assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n > Content-Length: 4\n[unknown]"));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:15,代码来源:BaseLoggingFilterTest.java

示例10: executeMethod

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
/**
 * Executes WebDAV method for testing
 * <p>
 * Sets content to request from a test file
 * 
 * @param methodName Method name to prepare, should be initialized (PUT, LOCK, UNLOCK are supported)
 * @param fileName the name of the file set to the context, can be used with path, i.e. "path/to/file/fileName.txt"
 * @param content If <b>not null</b> adds test content to the request
 * @param headers to set to request, can be null
 * @throws Exception
 */
private void executeMethod(String methodName, String fileName, byte[] content, Map<String, String> headers) throws Exception
{
    if (methodName == WebDAV.METHOD_PUT)
        method = new PutMethod();
    else if (methodName == WebDAV.METHOD_LOCK)
        method = new LockMethod();
    else if (methodName == WebDAV.METHOD_UNLOCK)
        method = new UnlockMethod();
    if (method != null)
    {
        request = new MockHttpServletRequest(methodName, "/alfresco/webdav/" + fileName);
        response = new MockHttpServletResponse();
        request.setServerPort(8080);
        request.setServletPath("/webdav");
        if (content != null)
        {
            request.setContent(content);
        }

        if (headers != null && !headers.isEmpty())
        {
            for (String key : headers.keySet())
            {
                request.addHeader(key, headers.get(key));
            }
        }

        method.setDetails(request, response, webDAVHelper, companyHomeNodeRef);

        method.execute();
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-remote-api,代码行数:44,代码来源:WebDAVonContentUpdateTest.java

示例11: executeMethod

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
/**
 * Executes WebDAV method for testing
 * <p>
 * Sets content to request from a test file
 * 
 * @param methodName Method to prepare, should be initialized (PUT, LOCK, UNLOCK are supported)
 * @param fileName the name of the file set to the context, can be used with path, i.e. "path/to/file/fileName.txt"
 * @param content If <b>not null</b> adds test content to the request
 * @param headers to set to request, can be null
 * @throws Exception
 */
private void executeMethod(String methodName, String fileName, byte[] content, Map<String, String> headers) throws Exception
{
    if (methodName == WebDAV.METHOD_PUT)
        method = new PutMethod();
    else if (methodName == WebDAV.METHOD_LOCK)
        method = new LockMethod();
    else if (methodName == WebDAV.METHOD_UNLOCK)
        method = new UnlockMethod();
    if (method != null)
    {
        request = new MockHttpServletRequest(methodName, "/alfresco/webdav/" + fileName);
        response = new MockHttpServletResponse();
        request.setServerPort(8080);
        request.setServletPath("/webdav");
        if (content != null)
        {
            request.setContent(content);
        }

        if (headers != null && !headers.isEmpty())
        {
            for (String key : headers.keySet())
            {
                request.addHeader(key, headers.get(key));
            }
        }

        method.setDetails(request, response, webDAVHelper, companyHomeNodeRef);

        method.execute();
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-remote-api,代码行数:44,代码来源:PutMethodTest.java

示例12: makeHttpRequest

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Override
protected HttpResponse makeHttpRequest(HttpBody entity, long startTime) {
    logger.info("making mock http client request: {} - {}", request.getMethod(), getRequestUri());
    MockHttpServletRequest req = requestBuilder.buildRequest(getServletContext());
    byte[] bytes;
    if (entity != null) {
        bytes = entity.getBytes();
        req.setContentType(entity.getContentType());
        if (entity.isMultiPart()) {
            for (MultiPartItem item : entity.getParts()) {
                MockMultiPart part = new MockMultiPart(item);
                req.addPart(part);
                if (!part.isFile()) {
                    req.addParameter(part.getName(), part.getValue());
                }
            }
        } else if (entity.isUrlEncoded()) {
            req.addParameters(entity.getParameters());
        } else {
            req.setContent(bytes);
        }
    } else {
        bytes = null;
    }
    MockHttpServletResponse res = new MockHttpServletResponse();
    logRequest(req, bytes);
    try {
        getServlet(request).service(req, res);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    long responseTime = getResponseTime(startTime);
    bytes = res.getContentAsByteArray();
    logResponse(res, bytes);
    HttpResponse response = new HttpResponse(responseTime);
    response.setUri(getRequestUri());
    response.setBody(bytes);
    response.setStatus(res.getStatus());
    for (Cookie c : res.getCookies()) {
        com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
        cookie.put(DOMAIN, c.getDomain());
        cookie.put(PATH, c.getPath());
        cookie.put(SECURE, c.getSecure() + "");
        cookie.put(MAX_AGE, c.getMaxAge() + "");
        cookie.put(VERSION, c.getVersion() + "");
        response.addCookie(cookie);
    }
    for (String headerName : res.getHeaderNames()) {
        response.putHeader(headerName, res.getHeaders(headerName));
    }
    return response;
}
 
开发者ID:intuit,项目名称:karate,代码行数:53,代码来源:MockHttpClient.java

示例13: checkLockedNodeTestWork

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
private void checkLockedNodeTestWork() throws WebDAVServerException
{
    req = new MockHttpServletRequest();
    resp = new MockHttpServletResponse();

    String rootPath = "/app:company_home";
    String storeName = "workspace://SpacesStore";
    StoreRef storeRef = new StoreRef(storeName);
    NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
    List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, rootPath, null, namespaceService, false);
    NodeRef defaultRootNode = nodeRefs.get(0);

    lockMethod = new LockMethod();
    NodeRef rootNodeRef = tenantService.getRootNode(nodeService, searchService, namespaceService, rootPath, defaultRootNode);
    String strPath = "/" + "testLockedNode" + GUID.generate();

    lockMethod.createExclusive = true;
    lockMethod.setDetails(req, resp, webDAVHelper, rootNodeRef);
    lockMethod.m_strPath = strPath;

    // Lock the node (will create a new one).
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Object>()
    {
        @Override
        public Object execute() throws Throwable {
            lockMethod.executeImpl();
            return null;
        }
    });

    // Prepare for PUT
    req.addHeader(WebDAV.HEADER_IF, "(<" + lockMethod.lockToken + ">)");
    putMethod = new PutMethod();
    putMethod.setDetails(req, resp, webDAVHelper, rootNodeRef);
    putMethod.parseRequestHeaders();
    putMethod.m_strPath = strPath;
    String content = "test content stream";
    req.setContent(content.getBytes());

    // Issue a put request
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Object>()
    {
        @Override
        public Object execute() throws Throwable
        {
            putMethod.executeImpl();
            return null;
        }
    });
}
 
开发者ID:Alfresco,项目名称:alfresco-remote-api,代码行数:51,代码来源:WebDAVMethodTest.java

示例14: testMNT_10873

import org.springframework.mock.web.MockHttpServletRequest; //导入方法依赖的package包/类
@Test
public void testMNT_10873() throws Exception
{
    String fileName = TEST_FILE_NAME + GUID.generate();
    final MockHttpServletRequest lockRequest = new MockHttpServletRequest();
    MockHttpServletResponse lockResponse = new MockHttpServletResponse();
    lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 5);
    // set request uri to point to non-existent file
    lockRequest.setRequestURI("/" + fileName);
    
    String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:lockinfo xmlns:D=\"DAV:\"><D:lockscope xmlns:D=\"DAV:\">" +
            "<D:exclusive xmlns:D=\"DAV:\"/></D:lockscope><D:locktype xmlns:D=\"DAV:\"><D:write xmlns:D=\"DAV:\"/></D:locktype>" +
            "<D:owner xmlns:D=\"DAV:\">" + userName + "</D:owner></D:lockinfo>";

    lockRequest.setContent(content.getBytes("UTF-8"));

    lockMethod.setDetails(lockRequest, lockResponse, davHelper, folderNodeRef);
    lockMethod.parseRequestHeaders();
    lockMethod.parseRequestBody();
    
    RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            lockMethod.executeImpl();
            return null;
        }
    };
    
    // lock node for 5 seconds
    this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
    
    assertEquals("Unexpected response status code.", HttpServletResponse.SC_CREATED, lockResponse.getStatus());
    
    RetryingTransactionCallback<NodeRef> getNodeRefCallback = new RetryingTransactionCallback<NodeRef>()
    {
        @Override
        public NodeRef execute() throws Throwable
        {
            return lockMethod.getNodeForPath(folderNodeRef, lockRequest.getRequestURI()).getNodeRef();
        }
    };
    
    NodeRef nodeRef = this.transactionService.getRetryingTransactionHelper().doInTransaction(getNodeRefCallback);
    
    assertTrue("NodeRef should exists.", nodeService.exists(nodeRef));        
    assertTrue("sys:webdavNoContent aspect should be applied on node.", nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WEBDAV_NO_CONTENT));
    
    // sleep for 6 seconds to ensure that timer was triggered
    Thread.sleep(6000);
    
    assertFalse("File should note exist in repo any more.", nodeService.exists(nodeRef));
    assertFalse("File should note exist in trashcan.", nodeService.exists(new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, nodeRef.getId())));
}
 
开发者ID:Alfresco,项目名称:alfresco-remote-api,代码行数:56,代码来源:LockMethodTest.java


注:本文中的org.springframework.mock.web.MockHttpServletRequest.setContent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。