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.protocol.data;
16  
17  import static org.htmlunit.protocol.data.DataUrlDecoder.decodeDataURL;
18  import static org.junit.Assert.assertArrayEquals;
19  import static org.junit.Assert.assertEquals;
20  
21  import java.io.ByteArrayInputStream;
22  
23  import javax.imageio.ImageIO;
24  
25  import org.junit.Test;
26  
27  /**
28   * Tests for {@link DataUrlDecoder}.
29   *
30   * @author Marc Guillemot
31   * @author Ahmed Ashour
32   * @author Ronald Brill
33   * @author Carsten Steul
34   */
35  public class DataURLDecoder2Test {
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      public void testDecodeDataURL() throws Exception {
42          DataUrlDecoder decoder = decodeDataURL("data:text/javascript,d1%20%3D%20'one'%3B");
43          assertEquals("d1 = 'one';", decoder.getDataAsString());
44          assertEquals("text/javascript", decoder.getMediaType());
45          assertEquals("US-ASCII", decoder.getCharset());
46  
47          decoder = decodeDataURL("data:text/javascript;base64,ZDIgPSAndHdvJzs%3D");
48          assertEquals("d2 = 'two';", decoder.getDataAsString());
49  
50          decoder = decodeDataURL(
51              "data:text/javascript;base64,%5a%44%4d%67%50%53%41%6e%64%47%68%79%5a%57%55%6e%4f%77%3D%3D");
52          assertEquals("d3 = 'three';", decoder.getDataAsString());
53  
54          decoder = decodeDataURL("data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207%20");
55          assertEquals("d4 = 'four';", decoder.getDataAsString());
56  
57          decoder = decodeDataURL("data:text/javascript,d5%20%3D%20'five%5Cu0027s'%3B");
58          assertEquals("d5 = 'five\\u0027s';", decoder.getDataAsString());
59  
60          decoder = decodeDataURL("data:application/octet-stream;base64,a+b/cQ==");
61          assertArrayEquals(new byte[]{107, -26, -1, 113}, decoder.getBytes());
62  
63          decoder = decodeDataURL("data:text/html;charset=utf-8,%3C%21DOCTYPE%20html%3E%0D%0A%3Cht"
64                      + "ml%20lang%3D%22en%22%3E%0D%0A%3Chead%3E%3Ctitle%3EEmbedded%20Window%3C%2F"
65                      + "title%3E%3C%2Fhead%3E%0D%0A%3Cbody%3E%3Ch1%3E42%3C%2Fh1%3E%3C%2Fbody%3E%0"
66                      + "A%3C%2Fhtml%3E%0A%0D%0A");
67          assertEquals("<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head><title>Embedded Window</title></head>\r\n"
68                      + "<body><h1>42</h1></body>\n</html>\n\r\n", decoder.getDataAsString());
69  
70          decoder = decodeDataURL("data:text/plain;charset=iso-8859-7,%b8%f7%fe");
71          assertEquals("\u0388\u03c7\u03ce", decoder.getDataAsString());
72  
73          decoder = decodeDataURL("data:,A brief note");
74          assertEquals("A brief note", decoder.getDataAsString());
75  
76          decoder = decodeDataURL("data:;,A brief note");
77          assertEquals("A brief note", decoder.getDataAsString());
78      }
79  
80      /**
81       * @throws Exception if the test fails
82       */
83      @Test
84      public void urlWithPlus() throws Exception {
85          // real browsers are able to show this image
86          final DataUrlDecoder decoder = decodeDataURL(
87                  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAA"
88                  + "BlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeN"
89                  + "Ge4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC");
90          try (ByteArrayInputStream in = new ByteArrayInputStream(decoder.getBytes());
91                  ) {
92              ImageIO.read(in);
93          }
94      }
95  }