當前位置: 首頁>>代碼示例>>Java>>正文


Java MockHttpServletRequestBuilder.content方法代碼示例

本文整理匯總了Java中org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.content方法的典型用法代碼示例。如果您正苦於以下問題:Java MockHttpServletRequestBuilder.content方法的具體用法?Java MockHttpServletRequestBuilder.content怎麽用?Java MockHttpServletRequestBuilder.content使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder的用法示例。


在下文中一共展示了MockHttpServletRequestBuilder.content方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: toMockMvcRequest

import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; //導入方法依賴的package包/類
protected RequestBuilder toMockMvcRequest(Pact.InteractionRequest request) throws Exception {
    MockHttpServletRequestBuilder requestBuilder = createRequestBuilderWithMethodAndUri(request)
            .headers(customiseHeaders(request.getHeaders()))
            .contentType(MediaType.APPLICATION_JSON);
    Object requestBody = request.getBody();
    return requestBody == null ? requestBuilder : requestBuilder.content(requestBody.toString());
}
 
開發者ID:tyro,項目名稱:pact-spring-mvc,代碼行數:8,代碼來源:PactTestBase.java

示例2: performVoid

import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; //導入方法依賴的package包/類
private void performVoid(MockHttpServletRequestBuilder requestBuilder, String bodyStr, final boolean noSession)
        throws Exception {
    if (bodyStr == null) {
        bodyStr = "";
    }
    requestBuilder = requestBuilder.content(bodyStr);


    if (noSession) {
        requestBuilder = requestBuilder.cookie(new Cookie(LoomClient.SESSION_COOKIE, ""));
    } else {
        if (sessionId != null) {
            requestBuilder = requestBuilder.cookie(new Cookie(LoomClient.SESSION_COOKIE, sessionId));
        }
    }

    StopWatch watch = new StopWatch();
    watch.start();
    MvcResult result =
            mockMvc.perform(requestBuilder).andExpect(status().isOk()).andDo(PrintHandler.print()).andReturn();
    watch.stop();

    Cookie[] c = result.getResponse().getCookies();
    Cookie loomCookie = null;
    for (Cookie element : c) {
        if (element.getName().equals(LoomClient.SESSION_COOKIE)) {
            loomCookie = element;
        }
    }

    if (loomCookie != null) {
        if (loomCookie.getMaxAge() != 0) {
            sessionId = loomCookie.getValue();
        } else {
            sessionId = null;
        }
    } // TODO: LOOM-622 work around for cookie issue
}
 
開發者ID:HewlettPackard,項目名稱:loom,代碼行數:39,代碼來源:LoomMvcClient.java

示例3: perform

import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; //導入方法依賴的package包/類
private <T> T perform(MockHttpServletRequestBuilder requestBuilder, String bodyStr, final java.lang.Class<T> tClass,
        final boolean noSession) throws Exception {
    if (bodyStr == null) {
        bodyStr = "";
    }
    requestBuilder = requestBuilder.content(bodyStr);

    if (noSession) {
        requestBuilder = requestBuilder.cookie(new Cookie(LoomClient.SESSION_COOKIE, ""));
    } else {
        if (sessionId != null) {
            requestBuilder = requestBuilder.cookie(new Cookie(LoomClient.SESSION_COOKIE, sessionId));
        }
    }


    StopWatch watch = new StopWatch();
    watch.start();
    MvcResult result = tClass == null
            ? mockMvc.perform(requestBuilder).andExpect(status().isOk()).andDo(PrintHandler.print()).andReturn()
            : mockMvc.perform(requestBuilder)
                    // .andExpect(status().isOk())
                    // .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                    .andDo(PrintHandler.print()).andReturn();

    if (result.getResponse().getStatus() == 200) {
        T resource = tClass == null ? null : new ObjectMapper().readValue(checkResult(result), tClass);

        watch.stop();
        if (LOG.isTraceEnabled()) {
            String uri = result.getRequest().getRequestURI();
            String method = result.getRequest().getMethod();
            if (watch.getTime() > timeWarning) {
                bodyStr += " WARNING:";
            }
            LOG.trace(method + " " + uri + " " + bodyStr + " Operation took " + watch);
        }

        Cookie[] c = result.getResponse().getCookies();
        Cookie loomCookie = null;
        for (Cookie element : c) {
            if (element.getName().equals(LoomClient.SESSION_COOKIE)) {
                loomCookie = element;
            }
        }

        if (loomCookie != null) {
            if (loomCookie.getMaxAge() != 0) {
                sessionId = loomCookie.getValue();
            } else {
                sessionId = null;
            }
        } // TODO: LOOM-622 work around for cookie issue
        return resource;
    } else {
        System.out.println(result);
        System.out.println(result.getResponse().getStatus());
        System.out.println(result.getResponse().getContentAsString());
        throw new LoomClientException("", result.getResponse().getStatus());
    }
}
 
開發者ID:HewlettPackard,項目名稱:loom,代碼行數:62,代碼來源:LoomMvcClient.java


注:本文中的org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.content方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。