/** * Parses the HTTP request contained in the given file. * * @param file containing a HTTP request * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid * @throws IOException if a problem occurs reading the file */ public final RawHttpRequest parseRequest(File file) throws IOException { try (InputStream stream = Files.newInputStream(file.toPath())) { return parseRequest(stream).eagerly(); } }
/** * Parses the HTTP request contained in the given file. * * @param file containing a HTTP request * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid * @throws IOException if a problem occurs reading the file */ public final RawHttpRequest parseRequest(File file) throws IOException { try (InputStream stream = Files.newInputStream(file.toPath())) { return parseRequest(stream).eagerly(); } }
@Test public void replacingBodyWithChunkedEncodedMessage() throws IOException { InputStream stream = new ByteArrayInputStream("Hello RawHTTTP".getBytes()); int chunkSize = 4; RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); RawHttpRequest requestWithBody = request.withBody( new ChunkedBody(stream, "text/plain", chunkSize)); System.out.println(requestWithBody.eagerly()); }
@Test(expected = FileNotFoundException.class) public void replacingBodyWithFile() throws Throwable { RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); try { RawHttpRequest requestWithBody = request.withBody( new FileBody(new File("hello.request"), "text/plain")); System.out.println(requestWithBody.eagerly()); } catch (RuntimeException e) { throw e.getCause(); } }
request = request.eagerly(); request.writeTo(System.out); System.out.println();
@Test public void replacingBodyWithBytes() throws IOException { byte[] bytes = "Hello RawHTTP".getBytes(); RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); RawHttpRequest requestWithBody = request.withBody( new BytesBody(bytes, "text/plain")); System.out.println(requestWithBody.eagerly()); }
@Test public void replacingBodyWithString() throws IOException { RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); RawHttpRequest requestWithBody = request.withBody(new StringBody("Hello RawHTTP", "text/plain")); System.out.println(requestWithBody.eagerly()); }