11package com .mindee .input ;
22
33import static org .junit .jupiter .api .Assertions .*;
4- import static org .mockito .Mockito .*;
54
65import java .io .File ;
76import java .io .IOException ;
1110import java .nio .file .Paths ;
1211import lombok .Setter ;
1312import org .junit .jupiter .api .*;
13+ import org .junit .jupiter .api .Test ;
1414
1515public class URLInputSourceTest {
1616
@@ -31,7 +31,7 @@ public void tearDown() {
3131 void fetchFile_shouldSaveFileLocally () throws IOException {
3232 urlInputSource .fetchFile ();
3333
34- File savedFile = new File (urlInputSource .getLocalFilename ());
34+ var savedFile = new File (urlInputSource .getLocalFilename ());
3535 assertTrue (savedFile .exists (), "The file should be saved locally" );
3636
3737 Files .deleteIfExists (savedFile .toPath ());
@@ -47,7 +47,7 @@ void fetchFile_shouldThrowIOException_onFailedFetch() {
4747
4848 @ Test
4949 void fetchFile_shouldHandleRedirects () throws IOException {
50- urlInputSource .setMockResponseCode (HttpURLConnection .HTTP_MOVED_TEMP );
50+ urlInputSource .setMockResponseCode (HttpURLConnection .HTTP_OK );
5151 urlInputSource .setMockRedirectUrl ("https://example.com/redirectedfile.pdf" );
5252
5353 urlInputSource .setMockResponseCode (HttpURLConnection .HTTP_OK );
@@ -84,7 +84,7 @@ void toLocalInputSource_shouldCreateLocalInputSource() throws IOException {
8484 urlInputSource .cleanup ();
8585 }
8686
87- class TestableURLInputSource extends URLInputSource {
87+ static class TestableURLInputSource extends URLInputSource {
8888
8989 @ Setter
9090 private int mockResponseCode = HttpURLConnection .HTTP_OK ;
@@ -98,23 +98,51 @@ public TestableURLInputSource(String url) {
9898
9999 @ Override
100100 protected HttpURLConnection createConnection (String urlString ) throws IOException {
101- HttpURLConnection mockConnection = mock (HttpURLConnection .class );
102-
103- when (mockConnection .getResponseCode ()).thenReturn (mockResponseCode );
101+ java .net .URL url = new java .net .URL (urlString );
102+ boolean wasRedirected = isRedirected ;
104103
105- Path path = Paths .get ("src/test/resources/file_types/pdf/multipage.pdf" );
106- if (isRedirected ) {
107- when (mockConnection .getHeaderField ("Location" )).thenReturn (null );
108- when (mockConnection .getInputStream ()).thenReturn (Files .newInputStream (path ));
109- } else {
110- when (mockConnection .getHeaderField ("Location" )).thenReturn (mockRedirectUrl );
111- when (mockConnection .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_MOVED_TEMP );
104+ if (!isRedirected && mockRedirectUrl != null ) {
112105 isRedirected = true ;
113- return mockConnection ;
114106 }
115107
116- when (mockConnection .getInputStream ()).thenReturn (Files .newInputStream (path ));
117- return mockConnection ;
108+ return new HttpURLConnection (url ) {
109+ @ Override
110+ public void disconnect () {
111+ }
112+
113+ @ Override
114+ public boolean usingProxy () {
115+ return false ;
116+ }
117+
118+ @ Override
119+ public void connect () {
120+ }
121+
122+ @ Override
123+ public int getResponseCode () {
124+ if (mockRedirectUrl != null && !wasRedirected ) {
125+ return HttpURLConnection .HTTP_MOVED_TEMP ;
126+ }
127+ return mockResponseCode ;
128+ }
129+
130+ @ Override
131+ public String getHeaderField (String name ) {
132+ if ("Location" .equalsIgnoreCase (name )) {
133+ if (mockRedirectUrl != null && !wasRedirected ) {
134+ return mockRedirectUrl ;
135+ }
136+ }
137+ return null ;
138+ }
139+
140+ @ Override
141+ public java .io .InputStream getInputStream () throws IOException {
142+ Path path = Paths .get ("src/test/resources/file_types/pdf/multipage.pdf" );
143+ return Files .newInputStream (path );
144+ }
145+ };
118146 }
119147 }
120148}
0 commit comments