Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BATIK-1366: add read and connection timeout on ParsedURLData #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions batik-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>4.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ParsedURLData {
protected static final String HTTP_ACCEPT_HEADER = "Accept";
protected static final String HTTP_ACCEPT_LANGUAGE_HEADER = "Accept-Language";
protected static final String HTTP_ACCEPT_ENCODING_HEADER = "Accept-Encoding";
protected static final int CONNECT_TIMEOUT = 5000;
protected static final int READ_TIMEOUT = 20000;

protected static List acceptedEncodings = new LinkedList();
static {
Expand Down Expand Up @@ -543,6 +545,8 @@ protected InputStream openStreamInternal(String userAgent,
}
urlC.setRequestProperty(HTTP_ACCEPT_ENCODING_HEADER,
encodingHeader);
urlC.setConnectTimeout(CONNECT_TIMEOUT);
urlC.setReadTimeout(READ_TIMEOUT);
}

contentType = urlC.getContentType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.apache.batik.util;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.SocketPolicy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertThrows;

public class ParsedURLDataTimeoutUnitTest {

private MockWebServer _mockWebServer;

@Before
public void setUp() throws IOException {
_mockWebServer = new MockWebServer();
_mockWebServer.start();
}

@After
public void tearDown() throws IOException {
_mockWebServer.shutdown();
}

/**
* Test that a SocketTimeoutException is thrown when the server does not respond.
* This test fails in case URLConnection in ParsedURLData has no timeout set.
*
* @throws Exception
*/
@Test
public void testThatTimeoutIsUsedForURLConnection() throws Exception {
_mockWebServer.enqueue(new MockResponse()
.setSocketPolicy(SocketPolicy.NO_RESPONSE));
ParsedURLData data = new ParsedURLData(_mockWebServer.url("/test").url());
List<String> mimeTypes = new ArrayList<>();
mimeTypes.add("text/html");
assertThrows(SocketTimeoutException.class, () -> {
InputStream userAgent = data.openStream("userAgent", mimeTypes.iterator());
});
}
}