HttpUtilsTest.java 文件源码

java
阅读 32 收藏 0 点赞 0 评论 0

项目:openimaj 作者:
/**
 * Test that the redirection handler is working
 *
 * @throws MalformedURLException
 * @throws IOException
 */
@Test
public void testRedirect() throws MalformedURLException, IOException {
    final String[][] links = new String[][] {
            // new String[] { "http://t.co/kp7qdeL6",
            // "http://www.openrightsgroup.org/press/releases/bruce-willis-right-to-challenge-apple#copyright"
            // },
            new String[] { "http://t.co/VJn1ISBl", "http://ow.ly/1mgxj1",
                    "http://www.electronicsweekly.com/Articles/2012/09/03/54467/raspberry-pi-goes-to-cambridge-to-get-free-os.htm" }
    };
    for (final String[] link : links) {
        final String[] expecting = Arrays.copyOfRange(link, 1, link.length);
        final String firstLink = link[0];
        HttpUtils.readURLAsByteArrayInputStream(new URL(firstLink), new HttpUtils.MetaRefreshRedirectStrategy() {
            int redirected = 0;

            @Override
            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                    throws ProtocolException
            {
                final boolean isRedirect = super.isRedirected(request, response, context);
                if (redirected < expecting.length) {
                    assertTrue(isRedirect);
                    final HttpUriRequest redirect = this.getRedirect(request, response, context);
                    final RedirectLocations redirectLocations = (RedirectLocations) context
                            .getAttribute(REDIRECT_LOCATIONS);
                    if (redirectLocations != null)
                        redirectLocations.remove(redirect.getURI());
                    final String uriString = redirect.getURI().toString();
                    assertEquals(expecting[redirected++], uriString);
                }
                return isRedirect;
            }
        });
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号