Skip to content

Commit 560d4fc

Browse files
committed
re-adding format tests
1 parent caa8579 commit 560d4fc

File tree

4 files changed

+198
-8
lines changed

4 files changed

+198
-8
lines changed

core/src/main/java/org/everit/json/schema/internal/HostnameFormatValidator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ public class HostnameFormatValidator implements FormatValidator {
1414
@Override
1515
public Optional<String> validate(final String subject) {
1616
try {
17-
InternetDomainName.from(subject);
18-
return Optional.empty();
19-
} catch (IllegalArgumentException | NullPointerException e) {
17+
if (InternetDomainName.isValid(subject) && !subject.contains("_")) {
18+
return Optional.empty();
19+
} else {
20+
return Optional.of(String.format("[%s] is not a valid hostname", subject));
21+
}
22+
} catch (NullPointerException e) {
2023
return Optional.of(String.format("[%s] is not a valid hostname", subject));
2124
}
2225
}

core/src/main/java/org/everit/json/schema/internal/URIFormatValidator.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ public class URIFormatValidator implements FormatValidator {
1414
@Override
1515
public Optional<String> validate(final String subject) {
1616
try {
17-
new URI(subject);
18-
return Optional.empty();
17+
URI uri = new URI(subject);
18+
if (hasProtocol(uri) || isProtocolRelativeURI(subject)) {
19+
return Optional.empty();
20+
} else {
21+
throw new URISyntaxException(subject, "no protocol and not protocol-relative");
22+
}
1923
} catch (URISyntaxException | NullPointerException e) {
2024
return Optional.of(String.format("[%s] is not a valid URI", subject));
2125
}
2226
}
2327

28+
private boolean isProtocolRelativeURI(String subject) {
29+
return subject.startsWith("//");
30+
}
31+
32+
private boolean hasProtocol(URI uri) {
33+
return uri.getScheme() != null;
34+
}
35+
2436
@Override public String formatName() {
2537
return "uri";
2638
}

core/src/test/java/org/everit/json/schema/internal/DefaultFormatValidatorTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
*/
1616
package org.everit.json.schema.internal;
1717

18+
import java.util.Optional;
19+
1820
import org.everit.json.schema.FormatValidator;
1921
import org.junit.Assert;
2022
import org.junit.Test;
2123

22-
import java.util.Optional;
23-
2424
public class DefaultFormatValidatorTest {
2525

2626
private static final String THERE_IS_NO_PLACE_LIKE = "127.0.0.1";
2727

2828
private static final String IPV6_ADDR = "2001:db8:85a3:0:0:8a2e:370:7334";
2929

3030
private void assertFailure(final String subject, final FormatValidator format,
31-
final String expectedFailure) {
31+
final String expectedFailure) {
3232
Optional<String> opt = format.validate(subject);
3333
Assert.assertNotNull("the optional is not null", opt);
3434
Assert.assertTrue("failure exists", opt.isPresent());
@@ -145,6 +145,11 @@ public void hostnameSuccess() {
145145
assertSuccess("localhost", new HostnameFormatValidator());
146146
}
147147

148+
@Test
149+
public void hostnameWithUnderscoresFailure() {
150+
assertFailure("not_a_valid_host_name", new HostnameFormatValidator(), "[not_a_valid_host_name] is not a valid hostname");
151+
}
152+
148153
@Test
149154
public void ipv4Failure() {
150155
assertFailure("asd", new IPV4Validator(), "[asd] is not a valid ipv4 address");
@@ -192,6 +197,16 @@ public void uriFailure() {
192197
assertFailure("12 34", new URIFormatValidator(), "[12 34] is not a valid URI");
193198
}
194199

200+
@Test
201+
public void relativeURIRefFails() {
202+
assertFailure("abc", new URIFormatValidator(), "[abc] is not a valid URI");
203+
}
204+
205+
@Test
206+
public void protocolRelativeUriSuccess() {
207+
assertSuccess("//foo.bar/?baz=qux#quux", new URIFormatValidator());
208+
}
209+
195210
@Test
196211
public void uriNullFailure() {
197212
assertFailure(null, new URIFormatValidator(), "[null] is not a valid URI");
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
[
2+
{
3+
"description": "validation of date-time strings",
4+
"schema": {
5+
"format": "date-time"
6+
},
7+
"tests": [
8+
{
9+
"description": "a valid date-time string",
10+
"data": "1963-06-19T08:30:06.283185Z",
11+
"valid": true
12+
},
13+
{
14+
"description": "an invalid date-time string",
15+
"data": "06/19/1963 08:30:06 PST",
16+
"valid": false
17+
},
18+
{
19+
"description": "only RFC3339 not all of ISO 8601 are valid",
20+
"data": "2013-350T01:01:01",
21+
"valid": false
22+
}
23+
]
24+
},
25+
{
26+
"description": "validation of URIs",
27+
"schema": {
28+
"format": "uri"
29+
},
30+
"tests": [
31+
{
32+
"description": "a valid URI",
33+
"data": "http://foo.bar/?baz=qux#quux",
34+
"valid": true
35+
},
36+
{
37+
"description": "a valid protocol-relative URI",
38+
"data": "//foo.bar/?baz=qux#quux",
39+
"valid": true
40+
},
41+
{
42+
"description": "an invalid URI",
43+
"data": "\\\\WINDOWS\\fileshare",
44+
"valid": false
45+
},
46+
{
47+
"data": "abc",
48+
"description": "an invalid URI though valid URI reference",
49+
"valid": false
50+
}
51+
]
52+
},
53+
{
54+
"description": "validation of e-mail addresses",
55+
"schema": {
56+
"format": "email"
57+
},
58+
"tests": [
59+
{
60+
"description": "a valid e-mail address",
61+
"data": "[email protected]",
62+
"valid": true
63+
},
64+
{
65+
"description": "an invalid e-mail address",
66+
"data": "2962",
67+
"valid": false
68+
}
69+
]
70+
},
71+
{
72+
"description": "validation of IP addresses",
73+
"schema": {
74+
"format": "ipv4"
75+
},
76+
"tests": [
77+
{
78+
"description": "a valid IP address",
79+
"data": "192.168.0.1",
80+
"valid": true
81+
},
82+
{
83+
"description": "an IP address with too many components",
84+
"data": "127.0.0.0.1",
85+
"valid": false
86+
},
87+
{
88+
"description": "an IP address with out-of-range values",
89+
"data": "256.256.256.256",
90+
"valid": false
91+
},
92+
{
93+
"description": "an IP address without 4 components",
94+
"data": "127.0",
95+
"valid": false
96+
},
97+
{
98+
"description": "an IP address as an integer",
99+
"data": "0x7f000001",
100+
"valid": false
101+
}
102+
]
103+
},
104+
{
105+
"description": "validation of IPv6 addresses",
106+
"schema": {
107+
"format": "ipv6"
108+
},
109+
"tests": [
110+
{
111+
"description": "a valid IPv6 address",
112+
"data": "::1",
113+
"valid": true
114+
},
115+
{
116+
"description": "an IPv6 address with out-of-range values",
117+
"data": "12345::",
118+
"valid": false
119+
},
120+
{
121+
"description": "an IPv6 address with too many components",
122+
"data": "1:1:1:1:1:1:1:1:1:1:1:1:1:1:1:1",
123+
"valid": false
124+
},
125+
{
126+
"description": "an IPv6 address containing illegal characters",
127+
"data": "::laptop",
128+
"valid": false
129+
}
130+
]
131+
},
132+
{
133+
"description": "validation of host names",
134+
"schema": {
135+
"format": "hostname"
136+
},
137+
"tests": [
138+
{
139+
"description": "a valid host name",
140+
"data": "www.example.com",
141+
"valid": true
142+
},
143+
{
144+
"description": "a host name starting with an illegal character",
145+
"data": "-a-host-name-that-starts-with--",
146+
"valid": false
147+
},
148+
{
149+
"description": "a host name containing illegal characters",
150+
"data": "not_a_valid_host_name",
151+
"valid": false
152+
},
153+
{
154+
"description": "a host name with a component too long",
155+
"data": "a-vvvvvvvvvvvvvvvveeeeeeeeeeeeeeeerrrrrrrrrrrrrrrryyyyyyyyyyyyyyyy-long-host-name-component",
156+
"valid": false
157+
}
158+
]
159+
}
160+
]

0 commit comments

Comments
 (0)