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