File size: 2,843 Bytes
fab29d7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System.IO.Compression;
namespace VersOne.Epub.Test.Integration.CustomSerialization.Context
{
internal class TestEpubFile : IDisposable
{
private const string FILE_PATH_IN_EPUB_PREFIX = "epub://";
private const string HTTP_USER_AGENT = "EpubReader Integration Test Runner (https://github.com/vers-one/EpubReader)";
private static readonly HttpClient httpClient = new();
private readonly ZipArchive testEpubArchive;
public TestEpubFile(string testEpubFilePath)
{
testEpubArchive = ZipFile.OpenRead(testEpubFilePath);
TestCaseDirectoryPath = Path.GetDirectoryName(testEpubFilePath) ?? throw new ArgumentException($"Cannot get directory name for {testEpubFilePath}.");
}
public string TestCaseDirectoryPath { get; }
public void Dispose()
{
testEpubArchive.Dispose();
}
public static string GetFilePathInEpub(string filePath)
{
return Path.Combine(FILE_PATH_IN_EPUB_PREFIX, filePath);
}
public byte[] ReadFileAsBytes(string filePathInEpub)
{
string filePath = filePathInEpub[FILE_PATH_IN_EPUB_PREFIX.Length..];
ZipArchiveEntry? fileInEpub = testEpubArchive.GetEntry(filePath);
Assert.NotNull(fileInEpub);
byte[] result = new byte[(int)fileInEpub.Length];
using Stream fileStream = fileInEpub.Open();
using MemoryStream memoryStream = new(result);
fileStream.CopyTo(memoryStream);
return result;
}
public string ReadFileAsText(string filePathInEpub)
{
string filePath = filePathInEpub[FILE_PATH_IN_EPUB_PREFIX.Length..];
ZipArchiveEntry? fileInEpub = testEpubArchive.GetEntry(filePath);
Assert.NotNull(fileInEpub);
using Stream fileStream = fileInEpub.Open();
using StreamReader streamReader = new(fileStream);
return streamReader.ReadToEnd();
}
public static byte[] DownloadFileAsBytes(string url)
{
return RequestHttpContent(url).ReadAsByteArrayAsync().Result;
}
public static string DownloadFileAsText(string url)
{
return RequestHttpContent(url).ReadAsStringAsync().Result;
}
private static HttpContent RequestHttpContent(string url)
{
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Get, url);
httpRequestMessage.Headers.Add("User-Agent", HTTP_USER_AGENT);
HttpResponseMessage httpResponseMessage = httpClient.Send(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
httpResponseMessage.EnsureSuccessStatusCode();
return httpResponseMessage.Content;
}
}
}
|