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.IOException;
18  import java.io.OutputStream;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.servlet.Servlet;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests for binary content.
31   *
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   */
35  public class BinaryPageTest extends WebServerTestCase {
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      public void binary() throws Exception {
42          final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
43          servlets.put("/big", BinaryServlet.class);
44          startWebServer("./", null, servlets);
45  
46          final WebClient client = getWebClient();
47  
48          final Page page = client.getPage(URL_FIRST + "big");
49          assertTrue(page instanceof UnexpectedPage);
50      }
51  
52      /**
53       * Servlet for {@link #binary()}.
54       */
55      public static class BinaryServlet extends HttpServlet {
56  
57          /**
58           * {@inheritDoc}
59           */
60          @Override
61          protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
62              final int length = 1000;
63              response.setContentLength(length);
64              final byte[] buffer = new byte[1024];
65              try (OutputStream out = response.getOutputStream()) {
66                  for (int i = length / buffer.length; i >= 0; i--) {
67                      out.write(buffer);
68                  }
69              }
70          }
71      }
72  
73      /**
74       * @throws Exception if the test fails
75       */
76      @Test
77      public void chunkedBigContent() throws Exception {
78          final Map<String, Class<? extends Servlet>> servlets = new HashMap<>();
79          servlets.put("/bigChunked", ChunkedBigContentServlet.class);
80          startWebServer("./", null, servlets);
81  
82          final WebClient client = getWebClient();
83  
84          final Page page = client.getPage(URL_FIRST + "bigChunked");
85          assertTrue(page instanceof UnexpectedPage);
86      }
87  
88      /**
89       * Servlet for {@link #chunkedBigContent()}.
90       */
91      public static class ChunkedBigContentServlet extends HttpServlet {
92  
93          /**
94           * {@inheritDoc}
95           */
96          @Override
97          protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
98              response.setHeader("Transfer-Encoding", "chunked");
99              final int length = 60 * 1024 * 1024;
100             final byte[] buffer = new byte[1024];
101             try (OutputStream out = response.getOutputStream()) {
102                 for (int i = length / buffer.length; i >= 0; i--) {
103                     out.write(buffer);
104                 }
105             }
106         }
107     }
108 
109     /**
110      * @throws Exception if the test fails
111      */
112     @Test
113     public void chunked() throws Exception {
114         final String response = "HTTP/1.1 200 OK\r\n"
115             + "Transfer-Encoding: chunked\r\n\r\n"
116             + "5\r\n"
117             + "ABCDE\r\n"
118             + "5\r\n"
119             + "FGHIJ\r\n"
120             + "5\r\n"
121             + "KLMNO\r\n"
122             + "5\r\n"
123             + "PQRST\r\n"
124             + "5\r\n"
125             + "UVWXY\r\n"
126             + "1\r\n"
127             + "Z\r\n"
128             + "0\r\n\r\n";
129 
130         try (PrimitiveWebServer primitiveWebServer = new PrimitiveWebServer(null, response, null)) {
131             final WebClient client = getWebClient();
132 
133             final TextPage page = client.getPage("http://localhost:" + primitiveWebServer.getPort() + "/" + "chunked");
134             assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", page.getContent());
135         }
136     }
137 }