1+ package io .github .jycr .javadataurlhandler ;
2+
3+ import java .io .IOException ;
4+ import java .io .InputStream ;
5+ import java .net .URI ;
6+ import java .nio .charset .Charset ;
7+ import java .nio .charset .StandardCharsets ;
8+
9+ import org .assertj .core .api .AbstractObjectAssert ;
10+ import org .assertj .core .api .SoftAssertions ;
11+ import org .junit .jupiter .api .Test ;
12+
13+ import static org .assertj .core .api .Assertions .assertThat ;
14+
15+ class DataUriConnectionTest {
16+ @ Test
17+ void testMinimal () throws IOException {
18+ assertDataUriAsString (
19+ "data:,A%20brief%20note" ,
20+ "text/plain;charset=US-ASCII" ,
21+ StandardCharsets .US_ASCII ,
22+ 12
23+ )
24+ .isEqualTo ("A brief note" );
25+ }
26+
27+ @ Test
28+ void testImage () throws IOException {
29+ DataUriConnection connection = assertDataUri (
30+ "data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7" ,
31+ "image/gif" ,
32+ StandardCharsets .US_ASCII ,
33+ 273
34+ );
35+ assertThat (connection .getInputStream ().readAllBytes ())
36+ .isEqualTo (new byte []{
37+ 71 , 73 , 70 , 56 , 55 , 97 , 48 , 0 , 48 , 0 , -16 , 0 , 0 , 0 , 0 , 0 , -1 , -1 , -1 , 44 , 0 , 0 , 0 , 0 , 48 , 0 , 48 , 0 , 0 , 2 , -16 , -116 , -113 , -87 , -53 ,
38+ -19 , -33 , 0 , -100 , 14 , 72 , -117 , 115 , -80 , -76 , -85 , 12 , -122 , 30 , 20 , -106 , -90 , 52 , 46 , -25 , 42 , -90 , 9 , -117 , 26 , -89 , 43 , -81 , 81 ,
39+ 73 , 111 , 56 , -39 , -114 , 102 , -41 , -13 , -128 , 92 , -63 , -55 , 48 , 117 , -47 , 8 , 49 , 57 , 29 , 19 , -88 , 20 , 30 , -114 , 20 , 77 , -52 , -25 , -28 ,
40+ 84 , -97 , -71 , 98 , 37 , 42 , -83 , 113 , 121 , 99 , -103 , 74 , -121 , -16 , -34 , -72 , -41 , 15 , -25 , 34 , -42 , 26 , -63 , 27 , -76 , -72 , -114 , 74 ,
41+ -106 , -65 , 76 , -8 , 59 , 38 , -110 , 71 , -57 , 39 , -89 , 119 , 53 , -109 , 5 , -11 , -12 , 115 , 19 , -89 , 40 , -8 , -32 , 7 , 56 , -72 , 118 , 40 , -89 ,
42+ 88 , 103 , 100 , 23 , -55 , 35 , 117 , -7 , -14 , 113 , 6 , 87 , 101 , -26 , 6 , 122 , 57 , -119 , -107 , -105 , -122 , -105 , -40 , -74 , -119 , -59 , 106 ,
43+ 104 , -43 , 90 , -118 , 84 , -6 , 23 , -104 , -119 , -9 , 73 , -70 , -102 , -5 , -109 , 75 , -125 , -118 , -45 , -109 , 52 , -68 , 57 , 52 , 67 , -123 , -44 ,
44+ -73 , 11 , -54 , 59 , 58 , -36 , 119 , 120 , -86 , -10 , -24 , -84 , 23 , -51 , 76 , -44 , -30 , 28 , 71 , -71 , 68 , 123 , 12 , 28 , 46 , -98 , -19 , -72 , -45 ,
45+ 100 , 51 , 123 , -115 , -99 , -83 , -59 , -40 , -108 , 5 , -93 , -34 , -43 , -82 , 44 , -65 , 94 , 94 , 63 , 127 , 87 , 31 , -27 , 85 , -77 , 95 , 44 , -90 , -20 ,
46+ -15 , -31 , -121 , 30 , 29 , 110 , 6 , 10 , 0 , 0 , 59
47+ });
48+ }
49+
50+ @ Test
51+ void testGreekCharaters () throws IOException {
52+ assertDataUriAsString (
53+ "data:text/plain;charset=iso-8859-7,%D6%EF%E9%ED%E9%EA%DE%E9%E1+%E3%F1%DC%EC%EC%E1%F4%E1" ,
54+ "text/plain;charset=ISO-8859-7" ,
55+ Charset .forName ("ISO-8859-7" ),
56+ 18
57+ )
58+ .isEqualTo ("Φοινικήια γράμματα" );
59+ }
60+
61+ @ Test
62+ void testApplicationData () throws IOException {
63+ assertDataUriAsString (
64+ "data:application/vnd-xxx-query,select_vcount,fcol_from_fieldtable/local" ,
65+ "application/vnd-xxx-query" ,
66+ StandardCharsets .US_ASCII ,
67+ 40
68+ )
69+ .isEqualTo ("select_vcount,fcol_from_fieldtable/local" );
70+ }
71+
72+ @ Test
73+ void testPlainText () throws IOException {
74+ assertDataUriAsString (
75+ "data:text/plain,Hello%2C%20World!" ,
76+ "text/plain;charset=US-ASCII" ,
77+ StandardCharsets .US_ASCII ,
78+ 13
79+ )
80+ .isEqualTo ("Hello, World!" );
81+ }
82+
83+ private AbstractObjectAssert <?, String > assertDataUriAsString (
84+ String dataUri ,
85+ String expectedContentType ,
86+ Charset expectedCharset ,
87+ long expectedContentLength
88+ ) throws IOException {
89+ DataUriConnection connection = assertDataUri (dataUri , expectedContentType , expectedCharset , expectedContentLength );
90+
91+ return assertThat (connection .getContent ())
92+ .describedAs ("Content" )
93+ .isInstanceOf (InputStream .class )
94+ .extracting (content -> {
95+ try {
96+ return ((InputStream ) content ).readAllBytes ();
97+ } catch (IOException e ) {
98+ throw new RuntimeException (e );
99+ }
100+ })
101+ .extracting (content -> new String (content , expectedCharset ))
102+ ;
103+ }
104+
105+
106+ private DataUriConnection assertDataUri (
107+ String dataUri ,
108+ String expectedContentType ,
109+ Charset expectedCharset ,
110+ long expectedContentLength
111+ ) throws IOException {
112+ DataUriConnection connection = new DataUriConnection (URI .create (dataUri ).toURL ());
113+ connection .connect ();
114+ SoftAssertions assertions = new SoftAssertions ();
115+ assertions .assertThat (connection .getContentType ())
116+ .describedAs ("Content type" )
117+ .isEqualTo (expectedContentType );
118+ assertions .assertThat (connection .getCharset ())
119+ .describedAs ("Charset" )
120+ .isEqualTo (expectedCharset );
121+ assertions .assertThat (connection .getContentLengthLong ())
122+ .describedAs ("Content length" )
123+ .isEqualTo (expectedContentLength );
124+ assertions .assertThat (connection .getContentEncoding ())
125+ .describedAs ("Content encoding" )
126+ .isNull ();
127+ assertions .assertAll ();
128+
129+ return connection ;
130+ }
131+ }
0 commit comments