### Question:
HttpEventTask implements Runnable { protected static void processPostRequest(InputStream in, CommandRequest request) throws RequestException, IOException { Map<String, String> headerMap = parsePostHeaders(in); if (headerMap == null) { CommandCenterLog.warn("Illegal request read: null headerMap"); throw new RequestException(StatusCode.BAD_REQUEST, ""); } if (headerMap.containsKey("content-type") && !checkContentTypeSupported(headerMap.get("content-type"))) { CommandCenterLog.warn("Request not supported: unsupported Content-Type: " + headerMap.get("content-type")); throw new RequestException(StatusCode.UNSUPPORTED_MEDIA_TYPE, "Only form-encoded post request is supported"); } int bodyLength = 0; try { bodyLength = Integer.parseInt(headerMap.get("content-length")); } catch (Exception e) { } if (bodyLength < 1) { CommandCenterLog.warn("Request not supported: no available Content-Length in headers"); throw new RequestException(StatusCode.LENGTH_REQUIRED, "No legal Content-Length"); } parseParams(readBody(in, bodyLength), request); } HttpEventTask(Socket socket); void close(); @Override void run(); static final String SERVER_ERROR_MESSAGE; static final String INVALID_COMMAND_MESSAGE; }### Answer:
@Test public void processPostRequest() throws IOException { CommandRequest request; request = new CommandRequest(); request.addParam("a", "1"); try { HttpEventTask.processPostRequest(new ByteArrayInputStream("".getBytes()), request); assertFalse(true); } catch (Exception e) { assertTrue(e instanceof RequestException); } assertEquals("1", request.getParam("a")); try { HttpEventTask.processPostRequest(new ByteArrayInputStream(("Host: demo.com\r\n" + "Accept: **\r\n" + "Accept-Language: en-us\r\n" + "Accept-Encoding: gzip, deflate\r\n" + "Content-Type: application/json\r\n" + "Connection: keep-alive\r\n" + "Content-Length: 7\r\n" + "\r\n" + "a=1&b=2").getBytes()), request); assertTrue(false); } catch (RequestException e) { assertTrue(e.getStatusCode() == StatusCode.UNSUPPORTED_MEDIA_TYPE); } char[] buf = new char[1024 * 1024]; Arrays.fill(buf, '&'); String padding = new String(buf); try { request = new CommandRequest(); HttpEventTask.processPostRequest(new ByteArrayInputStream(("Host: demo.com\r\n" + "Accept: **\r\n" + "Accept-Language: en-us\r\n" + "Accept-Encoding: gzip, deflate\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Connection: keep-alive\r\n" + "Content-Length: " + (padding.length() + querystring.getBytes().length) + "\r\n" + "\r\n" + padding + querystring).getBytes()), request); assertEquals(2, request.getParameters().size()); assertEquals(" ", request.getParam("a ")); assertEquals("的的", request.getParam("b")); } catch (Exception e) { assertTrue(false); } }