Skip to content

Commit 7c40fb8

Browse files
committed
Add additional downloader implementations
RecordingDownloader relies on the real downloader and saves the request/response pair into a json file. MockDownloader uses json files from above and mocks responses for specific requests.
1 parent 1bcb9c7 commit 7c40fb8

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

extractor/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ dependencies {
99

1010
testImplementation 'junit:junit:4.13.1'
1111
testImplementation "com.squareup.okhttp3:okhttp:3.12.11"
12+
testImplementation 'com.google.code.gson:gson:2.8.6'
13+
testImplementation 'commons-io:commons-io:2.8.0'
1214
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.schabi.newpipe.downloader;
2+
3+
import com.google.gson.GsonBuilder;
4+
5+
import org.schabi.newpipe.extractor.downloader.Downloader;
6+
import org.schabi.newpipe.extractor.downloader.Request;
7+
import org.schabi.newpipe.extractor.downloader.Response;
8+
9+
import java.io.File;
10+
import java.io.FileReader;
11+
import java.io.IOException;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import javax.annotation.Nonnull;
16+
17+
class MockDownloader extends Downloader {
18+
19+
private final String path;
20+
private final Map<Request, Response> mocks;
21+
22+
public MockDownloader(@Nonnull String path) throws IOException {
23+
this.path = path;
24+
this.mocks = new HashMap<>();
25+
File folder = new File(path);
26+
for (File file : folder.listFiles()) {
27+
final FileReader reader = new FileReader(file);
28+
final TestRequestResponse response = new GsonBuilder()
29+
.create()
30+
.fromJson(reader, TestRequestResponse.class);
31+
reader.close();
32+
mocks.put(response.getRequest(), response.getResponse());
33+
}
34+
35+
//shared find proper solution
36+
File clientVersion = new File("src/test/resources/org/schabi/newpipe/extractor/services/youtube/client_version.json");
37+
final FileReader reader = new FileReader(clientVersion);
38+
final TestRequestResponse response = new GsonBuilder()
39+
.create()
40+
.fromJson(reader, TestRequestResponse.class);
41+
reader.close();
42+
mocks.put(response.getRequest(), response.getResponse());
43+
}
44+
45+
@Override
46+
public Response execute(@Nonnull Request request) {
47+
Response result = mocks.get(request);
48+
if (result == null) {
49+
throw new NullPointerException("No mock response for request with url '" + request.url()
50+
+ "' exists in path '" + path + "'.\nPlease make sure to run the tests with " +
51+
"the RecordingDownloader first after changes.");
52+
}
53+
return result;
54+
}
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.schabi.newpipe.downloader;
2+
3+
import com.google.gson.GsonBuilder;
4+
5+
import org.apache.commons.io.FileUtils;
6+
import org.schabi.newpipe.extractor.downloader.Downloader;
7+
import org.schabi.newpipe.extractor.downloader.Request;
8+
import org.schabi.newpipe.extractor.downloader.Response;
9+
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
10+
11+
import java.io.File;
12+
import java.io.FileWriter;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
17+
18+
import javax.annotation.Nonnull;
19+
20+
class RecordingDownloader extends Downloader {
21+
22+
private int index = 0;
23+
private final String path;
24+
25+
public RecordingDownloader(String stringPath) throws IOException {
26+
this.path = stringPath;
27+
Path path = Paths.get(stringPath);
28+
File directory = path.toFile();
29+
if (directory.exists()) {
30+
FileUtils.cleanDirectory(directory);
31+
}
32+
Files.createDirectories(path);
33+
}
34+
35+
@Override
36+
public Response execute(@Nonnull Request request) throws IOException, ReCaptchaException {
37+
Downloader downloader = DownloaderTestImpl.getInstance();
38+
Response response = downloader.execute(request);
39+
40+
File outputFile = new File(path + File.separator + index + ".json");
41+
index++;
42+
outputFile.createNewFile();
43+
FileWriter writer = new FileWriter(outputFile);
44+
new GsonBuilder()
45+
.setPrettyPrinting()
46+
.create()
47+
.toJson(new TestRequestResponse(request, response), writer);
48+
writer.flush();
49+
writer.close();
50+
51+
return response;
52+
}
53+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.schabi.newpipe.downloader;
2+
3+
import org.schabi.newpipe.extractor.downloader.Request;
4+
import org.schabi.newpipe.extractor.downloader.Response;
5+
6+
final class TestRequestResponse {
7+
private final String comment;
8+
private final Request request;
9+
private final Response response;
10+
11+
public TestRequestResponse(Request request, Response response) {
12+
this.comment = "Auto-generated for tests. See RecordingDownloader";
13+
this.request = request;
14+
this.response = response;
15+
}
16+
17+
public String getComment() {
18+
return comment;
19+
}
20+
21+
public Request getRequest() {
22+
return request;
23+
}
24+
25+
public Response getResponse() {
26+
return response;
27+
}
28+
}

0 commit comments

Comments
 (0)