Example usage for org.springframework.mock.web MockHttpServletResponse setOutputStreamAccessAllowed

List of usage examples for org.springframework.mock.web MockHttpServletResponse setOutputStreamAccessAllowed

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse setOutputStreamAccessAllowed.

Prototype

public void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed) 

Source Link

Document

Set whether #getOutputStream() access is allowed.

Usage

From source file:com.baidu.jprotobuf.rpc.client.ProxyFactoryBeanTestBase.java

protected HttpServer createServer() throws Exception {

    servlet.init();/*from   w w w  . ja  va  2 s. c  o m*/

    HttpServerProvider provider = HttpServerProvider.provider();
    HttpServer httpserver = provider.createHttpServer(new InetSocketAddress(8080), 10);

    httpserver.createContext(getPathInfo(), new HttpHandler() {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {

            MockHttpServletRequest request = new MockHttpServletRequest();
            request.setPathInfo(getPathInfo());

            String queryString = httpExchange.getRequestURI().getRawQuery();

            if (queryString != null) {
                if (queryString.indexOf(ServiceExporter.INPUT_IDL_PARAMETER) != -1) {
                    request.addParameter(ServiceExporter.INPUT_IDL_PARAMETER, "");
                }
                if (queryString.indexOf(ServiceExporter.OUTPUT_IDL_PARAMETER) != -1) {
                    request.addParameter(ServiceExporter.OUTPUT_IDL_PARAMETER, "");
                }
            }

            request.setQueryString(queryString);
            InputStream requestBody = httpExchange.getRequestBody();
            request.setContent(IOUtils.toByteArray(requestBody));

            MockHttpServletResponse response = new MockHttpServletResponse();
            response.setOutputStreamAccessAllowed(true);

            try {
                servlet.service(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            }
            httpExchange.sendResponseHeaders(200, response.getContentLength());
            OutputStream out = httpExchange.getResponseBody(); // ?
            out.write(response.getContentAsByteArray());
            out.flush();
            httpExchange.close();
        }
    });
    httpserver.setExecutor(null);
    httpserver.start();

    return httpserver;
}