View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for text content.
27   *
28   * @author Ronald Brill
29   */
30  public class TextPageTest extends WebServerTestCase {
31  
32      @TempDir
33      static Path TEMP_DIR_;
34  
35      /**
36       * @throws Exception if the test fails
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  }