public ParserHandler(Configuration config) { this.config = config; namespaces = new ParserNamespaceSupport(); validating = false; validator = new ValidatorHandler(); uriHandlers.add(new HTTPURIHandler()); }
@Test public void testCanHandleHttp() throws Exception { URI uri = URI.createURI("http://example.com"); assertThat(handler.canHandle(uri), is(true)); handler.createInputStream(uri, Collections.EMPTY_MAP); }
/** * Creates an input stream for the URI, assuming it's a URL, and returns it. * * @return an open input stream. * @exception IOException if there is a problem obtaining an open input stream. */ @Override public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException { try { final HttpURLConnection httpConnection = getConnection(uri); httpConnection.setConnectTimeout(connectionTimeout); httpConnection.setReadTimeout(readTimeout); InputStream result = httpConnection.getInputStream(); Map<Object, Object> response = getResponse(options); if (response != null) { response.put( URIConverter.RESPONSE_TIME_STAMP_PROPERTY, httpConnection.getLastModified()); } return result; } catch (RuntimeException exception) { throw new Resource.IOWrappedException(exception); } }
@Test public void testCustomConnectTimeout() throws Exception { final int testValue = 42; reset(conn); { conn.setConnectTimeout(testValue); expectLastCall(); conn.setReadTimeout(HTTPURIHandler.DEFAULT_READ_TIMEOUT); expectLastCall(); expect(conn.getInputStream()).andReturn(is); } replay(conn); handler.setConnectionTimeout(testValue); URI uri = URI.createURI("http://example.com"); handler.createInputStream(uri, Collections.EMPTY_MAP); }
@Test public void testCustomReadTimeout() throws Exception { final int testValue = 42; reset(conn); { conn.setConnectTimeout(HTTPURIHandler.DEFAULT_CONNECTION_TIMEOUT); expectLastCall(); conn.setReadTimeout(testValue); expectLastCall(); expect(conn.getInputStream()).andStubReturn(is); } replay(conn); handler.setReadTimeout(testValue); URI uri = URI.createURI("http://example.com"); handler.createInputStream(uri, Collections.EMPTY_MAP); }
@Test public void testDefaultTimeouts() throws Exception { reset(conn); { conn.setConnectTimeout(HTTPURIHandler.DEFAULT_CONNECTION_TIMEOUT); expectLastCall(); conn.setReadTimeout(HTTPURIHandler.DEFAULT_READ_TIMEOUT); expectLastCall(); expect(conn.getInputStream()).andStubReturn(is); } replay(conn); URI uri = URI.createURI("http://example.com"); handler.createInputStream(uri, Collections.EMPTY_MAP); }
@Test public void testCantHandleFtp() throws Exception { URI uri = URI.createURI("ftp://example.com"); assertThat(handler.canHandle(uri), is(false)); }
@Test public void testTimeout() throws Exception { reset(conn); { conn.setConnectTimeout(anyInt()); expectLastCall(); conn.setReadTimeout(anyInt()); expectLastCall(); expect(conn.getInputStream()).andThrow(new IOException()); } replay(conn); URI uri = URI.createURI("http://example.com"); exception.expect(IOException.class); handler.createInputStream(uri, Collections.EMPTY_MAP); } }
@Test public void testCanHandleHttps() throws Exception { URI uri = URI.createURI("https://example.com"); assertThat(handler.canHandle(uri), is(true)); handler.createInputStream(uri, Collections.EMPTY_MAP); }