12
12
*/
13
13
package org .openhab .binding .http ;
14
14
15
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
16
+
15
17
import java .net .MalformedURLException ;
16
18
import java .net .URISyntaxException ;
19
+ import java .time .Instant ;
20
+ import java .util .Date ;
17
21
18
22
import org .eclipse .jdt .annotation .NonNullByDefault ;
19
- import org .junit .jupiter .api .Assertions ;
20
23
import org .junit .jupiter .api .Test ;
21
24
import org .openhab .binding .http .internal .Util ;
22
25
@@ -31,30 +34,61 @@ public class UtilTest {
31
34
@ Test
32
35
public void uriUTF8InHostnameEncodeTest () throws MalformedURLException , URISyntaxException {
33
36
String s = "https://foöo.bar/zhu.html?str=zin&tzz=678" ;
34
- Assertions . assertEquals ("https://xn--foo-tna.bar/zhu.html?str=zin&tzz=678" , Util .uriFromString (s ).toString ());
37
+ assertEquals ("https://xn--foo-tna.bar/zhu.html?str=zin&tzz=678" , Util .uriFromString (s ).toString ());
35
38
}
36
39
37
40
@ Test
38
41
public void uriUTF8InPathEncodeTest () throws MalformedURLException , URISyntaxException {
39
42
String s = "https://foo.bar/zül.html?str=zin" ;
40
- Assertions . assertEquals ("https://foo.bar/z%C3%BCl.html?str=zin" , Util .uriFromString (s ).toString ());
43
+ assertEquals ("https://foo.bar/z%C3%BCl.html?str=zin" , Util .uriFromString (s ).toString ());
41
44
}
42
45
43
46
@ Test
44
47
public void uriUTF8InQueryEncodeTest () throws MalformedURLException , URISyntaxException {
45
48
String s = "https://foo.bar/zil.html?str=zän" ;
46
- Assertions . assertEquals ("https://foo.bar/zil.html?str=z%C3%A4n" , Util .uriFromString (s ).toString ());
49
+ assertEquals ("https://foo.bar/zil.html?str=z%C3%A4n" , Util .uriFromString (s ).toString ());
47
50
}
48
51
49
52
@ Test
50
53
public void uriSpaceInPathEncodeTest () throws MalformedURLException , URISyntaxException {
51
54
String s = "https://foo.bar/z l.html?str=zun" ;
52
- Assertions . assertEquals ("https://foo.bar/z%20l.html?str=zun" , Util .uriFromString (s ).toString ());
55
+ assertEquals ("https://foo.bar/z%20l.html?str=zun" , Util .uriFromString (s ).toString ());
53
56
}
54
57
55
58
@ Test
56
59
public void uriSpaceInQueryEncodeTest () throws MalformedURLException , URISyntaxException {
57
60
String s = "https://foo.bar/zzl.html?str=z n" ;
58
- Assertions .assertEquals ("https://foo.bar/zzl.html?str=z%20n" , Util .uriFromString (s ).toString ());
61
+ assertEquals ("https://foo.bar/zzl.html?str=z%20n" , Util .uriFromString (s ).toString ());
62
+ }
63
+
64
+ @ Test
65
+ public void uriPlusInQueryEncodeTest () throws MalformedURLException , URISyntaxException {
66
+ String s = "https://foo.bar/zzl.html?str=z+n" ;
67
+ assertEquals ("https://foo.bar/zzl.html?str=z%2Bn" , Util .uriFromString (s ).toString ());
68
+ }
69
+
70
+ @ Test
71
+ public void uriAlreadyPartlyEscapedTest () throws MalformedURLException , URISyntaxException {
72
+ String s = "https://foo.bar/zzl.html?p=field%2Bvalue&foostatus=This is a test String&date=2024- 07-01" ;
73
+ assertEquals (
74
+ "https://foo.bar/zzl.html?p=field%252Bvalue&foostatus=This%20is%20a%20test%20String&date=2024-%20%2007-01" ,
75
+ Util .uriFromString (s ).toString ());
76
+ }
77
+
78
+ @ Test
79
+ public void wrappedStringFormatDateTest () {
80
+ String formatString = "https://foo.bar/zzl.html?p=field%2Bvalue&date=%1$tY-%1$4tm-%1$td" ;
81
+ Date testDate = Date .from (Instant .parse ("2024-07-01T10:00:00.000Z" ));
82
+ assertEquals ("https://foo.bar/zzl.html?p=field%2Bvalue&date=2024- 07-01" ,
83
+ Util .wrappedStringFormat (formatString , testDate ));
84
+ }
85
+
86
+ @ Test
87
+ public void wrappedStringFormatDateAndCommandTest () {
88
+ String formatString = "https://foo.bar/zzl.html?p=field%2Bvalue&foostatus=%2$s&date=%1$tY-%1$4tm-%1$td" ;
89
+ Date testDate = Date .from (Instant .parse ("2024-07-01T10:00:00.000Z" ));
90
+ String testCommand = "This is a test String" ;
91
+ assertEquals ("https://foo.bar/zzl.html?p=field%2Bvalue&foostatus=This is a test String&date=2024- 07-01" ,
92
+ Util .wrappedStringFormat (formatString , testDate , testCommand ));
59
93
}
60
94
}
0 commit comments