1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import java.io.File;
18 import java.nio.charset.StandardCharsets;
19 import java.nio.file.Path;
20
21 import org.apache.commons.io.FileUtils;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.io.TempDir;
24
25
26
27
28
29
30 public class TextPageTest extends WebServerTestCase {
31
32 @TempDir
33 static Path TEMP_DIR_;
34
35
36
37
38 @Test
39 public void save() throws Exception {
40 final String response = "HTTP/1.1 200 OK\r\n"
41 + "Content-Type: text/plain\r\n"
42 + "\r\n"
43 + "HtmlUnit Text Response";
44
45 try (PrimitiveWebServer primitiveWebServer = new PrimitiveWebServer(null, response, null)) {
46 final WebClient client = getWebClient();
47
48 final TextPage page = client.getPage("http://localhost:" + primitiveWebServer.getPort() + "/" + "text");
49
50 final File tmpFolder = new File(TEMP_DIR_.toFile(), "hu");
51 tmpFolder.mkdir();
52 final File file = new File(tmpFolder, "hu_txt.plain");
53 FileUtils.deleteQuietly(file);
54
55 page.save(file);
56 assertTrue(file.exists());
57 assertTrue(file.isFile());
58
59 assertEquals("HtmlUnit Text Response", FileUtils.readFileToString(file, StandardCharsets.UTF_8));
60 }
61 }
62 }