1919import org .apache .http .impl .client .cache .BasicHttpCacheStorage ;
2020import org .apache .http .impl .client .cache .CacheConfig ;
2121import org .apache .http .impl .client .cache .CachingHttpClientBuilder ;
22- import org .junit .Ignore ;
2322import org .junit .Test ;
2423
2524import com .github .jsonldjava .utils .JarCacheStorage ;
2625import com .github .jsonldjava .utils .JsonUtils ;
2726
2827public class MinimalSchemaOrgRegressionTest {
2928
30- private static final String ACCEPT_HEADER = "application/ld+json, application/json;q=0.9, application/javascript;q=0.5, text/javascript;q=0.5, text/plain;q=0.2, */*;q=0.1" ;
31-
32- @ Ignore ("Java API does not have any way of redirecting automatically from HTTP to HTTPS, which breaks schema.org usage with it" )
29+ /**
30+ * Tests getting JSON from schema.org with the HTTP Accept header set to
31+ * {@value com.github.jsonldjava.utils.JsonUtils#ACCEPT_HEADER}? .
32+ */
3333 @ Test
34- public void testHttpURLConnection () throws Exception {
34+ public void testApacheHttpClient () throws Exception {
3535 final URL url = new URL ("http://schema.org/" );
36- final boolean followRedirectsSetting = HttpURLConnection .getFollowRedirects ();
37- try {
38- HttpURLConnection .setFollowRedirects (true );
39- final HttpURLConnection urlConn = (HttpURLConnection ) url .openConnection ();
40- urlConn .setInstanceFollowRedirects (true );
41- urlConn .addRequestProperty ("Accept" , ACCEPT_HEADER );
42-
43- final InputStream directStream = urlConn .getInputStream ();
44- verifyInputStream (directStream );
45- } finally {
46- HttpURLConnection .setFollowRedirects (followRedirectsSetting );
47- }
48- }
49-
50- private void verifyInputStream (InputStream directStream ) throws IOException {
51- assertNotNull ("InputStream was null" , directStream );
52- final StringWriter output = new StringWriter ();
53- try {
54- IOUtils .copy (directStream , output , StandardCharsets .UTF_8 );
55- } finally {
56- directStream .close ();
57- output .flush ();
58- }
59- final String outputString = output .toString ();
60- checkBasicConditions (outputString );
36+ // Common CacheConfig for both the JarCacheStorage and the underlying
37+ // BasicHttpCacheStorage
38+ final CacheConfig cacheConfig = CacheConfig .custom ().setMaxCacheEntries (1000 )
39+ .setMaxObjectSize (1024 * 128 ).build ();
40+
41+ final CloseableHttpClient httpClient = CachingHttpClientBuilder .create ()
42+ // allow caching
43+ .setCacheConfig (cacheConfig )
44+ // Wrap the local JarCacheStorage around a BasicHttpCacheStorage
45+ .setHttpCacheStorage (new JarCacheStorage (null , cacheConfig ,
46+ new BasicHttpCacheStorage (cacheConfig )))
47+ // Support compressed data
48+ // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d5e1238
49+ .addInterceptorFirst (new RequestAcceptEncoding ())
50+ .addInterceptorFirst (new ResponseContentEncoding ())
51+ .setRedirectStrategy (DefaultRedirectStrategy .INSTANCE )
52+ // use system defaults for proxy etc.
53+ .useSystemProperties ().build ();
54+
55+ Object content = JsonUtils .fromURL (url , httpClient );
56+ checkBasicConditions (content .toString ());
6157 }
6258
6359 private void checkBasicConditions (final String outputString ) {
@@ -68,31 +64,5 @@ private void checkBasicConditions(final String outputString) {
6864 outputString .isEmpty ());
6965 assertTrue ("Unexpected length: " + outputString .length (), outputString .length () > 100000 );
7066 }
71-
72- @ Test
73- public void testApacheHttpClient () throws Exception {
74- final URL url = new URL ("http://schema.org/" );
75- // Common CacheConfig for both the JarCacheStorage and the underlying
76- // BasicHttpCacheStorage
77- final CacheConfig cacheConfig = CacheConfig .custom ().setMaxCacheEntries (1000 )
78- .setMaxObjectSize (1024 * 128 ).build ();
79-
80- final CloseableHttpClient httpClient = CachingHttpClientBuilder .create ()
81- // allow caching
82- .setCacheConfig (cacheConfig )
83- // Wrap the local JarCacheStorage around a BasicHttpCacheStorage
84- .setHttpCacheStorage (new JarCacheStorage (null , cacheConfig ,
85- new BasicHttpCacheStorage (cacheConfig )))
86- // Support compressed data
87- // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d5e1238
88- .addInterceptorFirst (new RequestAcceptEncoding ())
89- .addInterceptorFirst (new ResponseContentEncoding ())
90- .setRedirectStrategy (DefaultRedirectStrategy .INSTANCE )
91- // use system defaults for proxy etc.
92- .useSystemProperties ().build ();
93-
94- Object content = JsonUtils .fromURL (url , httpClient );
95- checkBasicConditions (content .toString ());
96- }
97-
67+
9868}
0 commit comments