mirror of
https://github.com/TeamNewPipe/NewPipeExtractor
synced 2024-12-03 07:20:12 +01:00
Merge pull request #644 from TiA4f8R/utf-8-encoding-for-mocks-windows
Specify UTF-8 file encoding in RecordingDownloader and MockDownloader
This commit is contained in:
commit
db81384ae0
@ -7,8 +7,10 @@ import org.schabi.newpipe.extractor.downloader.Request;
|
||||
import org.schabi.newpipe.extractor.downloader.Response;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -24,14 +26,15 @@ class MockDownloader extends Downloader {
|
||||
private final String path;
|
||||
private final Map<Request, Response> mocks;
|
||||
|
||||
public MockDownloader(@Nonnull String path) throws IOException {
|
||||
public MockDownloader(@Nonnull final String path) throws IOException {
|
||||
this.path = path;
|
||||
this.mocks = new HashMap<>();
|
||||
final File[] files = new File(path).listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
for (final File file : files) {
|
||||
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
|
||||
final FileReader reader = new FileReader(file);
|
||||
final InputStreamReader reader = new InputStreamReader(new FileInputStream(
|
||||
file), StandardCharsets.UTF_8);
|
||||
final TestRequestResponse response = new GsonBuilder()
|
||||
.create()
|
||||
.fromJson(reader, TestRequestResponse.class);
|
||||
@ -43,12 +46,12 @@ class MockDownloader extends Downloader {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(@Nonnull Request request) {
|
||||
Response result = mocks.get(request);
|
||||
public Response execute(@Nonnull final Request request) {
|
||||
final Response result = mocks.get(request);
|
||||
if (result == null) {
|
||||
throw new NullPointerException("No mock response for request with url '" + request.url()
|
||||
+ "' exists in path '" + path + "'.\nPlease make sure to run the tests with " +
|
||||
"the RecordingDownloader first after changes.");
|
||||
throw new NullPointerException("No mock response for request with url '" + request
|
||||
.url() + "' exists in path '" + path + "'.\nPlease make sure to run the tests "
|
||||
+ "with the RecordingDownloader first after changes.");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -8,8 +8,10 @@ import org.schabi.newpipe.extractor.downloader.Response;
|
||||
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@ -18,7 +20,8 @@ import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Relays requests to {@link DownloaderTestImpl} and saves the request/response pair into a json file.
|
||||
* Relays requests to {@link DownloaderTestImpl} and saves the request/response pair into a json
|
||||
* file.
|
||||
* </p>
|
||||
* <p>
|
||||
* Those files are used by {@link MockDownloader}.
|
||||
@ -44,12 +47,12 @@ class RecordingDownloader extends Downloader {
|
||||
* Deletes existing files starting with {@link RecordingDownloader#FILE_NAME_PREFIX}.
|
||||
* @param stringPath Path to the folder where the json files will be saved to.
|
||||
*/
|
||||
public RecordingDownloader(String stringPath) throws IOException {
|
||||
public RecordingDownloader(final String stringPath) throws IOException {
|
||||
this.path = stringPath;
|
||||
Path path = Paths.get(stringPath);
|
||||
File folder = path.toFile();
|
||||
final Path path = Paths.get(stringPath);
|
||||
final File folder = path.toFile();
|
||||
if (folder.exists()) {
|
||||
for (File file : folder.listFiles()) {
|
||||
for (final File file : folder.listFiles()) {
|
||||
if (file.getName().startsWith(RecordingDownloader.FILE_NAME_PREFIX)) {
|
||||
file.delete();
|
||||
}
|
||||
@ -60,14 +63,17 @@ class RecordingDownloader extends Downloader {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(@Nonnull Request request) throws IOException, ReCaptchaException {
|
||||
Downloader downloader = DownloaderTestImpl.getInstance();
|
||||
Response response = downloader.execute(request);
|
||||
public Response execute(@Nonnull final Request request) throws IOException,
|
||||
ReCaptchaException {
|
||||
final Downloader downloader = DownloaderTestImpl.getInstance();
|
||||
final Response response = downloader.execute(request);
|
||||
|
||||
File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index + ".json");
|
||||
final File outputFile = new File(path + File.separator + FILE_NAME_PREFIX + index
|
||||
+ ".json");
|
||||
index++;
|
||||
outputFile.createNewFile();
|
||||
FileWriter writer = new FileWriter(outputFile);
|
||||
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile),
|
||||
StandardCharsets.UTF_8);
|
||||
new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.create()
|
||||
|
Loading…
Reference in New Issue
Block a user