Skip to content

Commit

Permalink
テストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaji255 committed Apr 1, 2024
1 parent d0db7ad commit ab04353
Show file tree
Hide file tree
Showing 8 changed files with 2,154 additions and 3 deletions.
2 changes: 1 addition & 1 deletion KoeBook.Test/DiTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace KoeBook.Test;

public class DiTestBase
{
protected IHost Host { get; } = CreateDefaultBuilder().Build();
protected IHost Host { get; init; } = CreateDefaultBuilder().Build();

protected T GetService<T>() where T : notnull
{
Expand Down
34 changes: 32 additions & 2 deletions KoeBook.Test/Epub/ScrapingNaroServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using KoeBook.Core;
using System.Text.Json;
using AngleSharp;
using KoeBook.Core;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
using KoeBook.Epub.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace KoeBook.Test.Epub;

Expand All @@ -18,6 +20,34 @@ public ScrapingNaroServiceTest()
.Single();
}

[Fact]
public async Task ReadPageAsync()
{
Directory.CreateDirectory("./tmp/img");
var config = Configuration.Default.WithDefaultLoader();
using var context = BrowsingContext.New(config);
using var doc = await context.OpenAsync(req => req.Content(File.ReadAllText("./TestData/Naro/n0000aa/1.html")));

var (chapterTitle, section) = await _scrapingNaroService.ReadPageAsync(doc, true, "./tmp/img", default);

Assert.Null(chapterTitle);
Assert.Equal("タイトル1", section.Title);
var elements = section.Elements;
Assert.Equal(6, elements.Count);
var text = Assert.IsType<Paragraph>(elements[0]);
Assert.Equal("名前は【<ruby><rb>佐久平</rb><rp>《</rp><rt>さくだいら</rt><rp>》</rp></ruby> <ruby><rb>啓介</rb><rp>《</rp><rt>けいすけ</rt><rp>》</rp></ruby>】。", text.Text);
text = Assert.IsType<Paragraph>(elements[1]);
Assert.Equal(" テストテストテストテストテスト", text.Text);
text = Assert.IsType<Paragraph>(elements[2]);
Assert.Equal("「セリフセリフセリフセリフ!」", text.Text);
text = Assert.IsType<Paragraph>(elements[3]);
Assert.Equal("テスト", text.Text);
text = Assert.IsType<Paragraph>(elements[4]);
Assert.Equal("「インラインテスト」", text.Text);
text = Assert.IsType<Paragraph>(elements[5]);
Assert.Equal("テスト。ほげほげほげほげほげ。", text.Text);
}

[Theory]
[InlineData("https://ncode.syosetu.com/n0000a", "n0000a")]
[InlineData("https://ncode.syosetu.com/n0000a/", "n0000a")]
Expand Down
7 changes: 7 additions & 0 deletions KoeBook.Test/KoeBook.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
Expand All @@ -29,4 +30,10 @@
<ItemGroup>
<Compile Include="../KoeBook/Startup.cs" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
52 changes: 52 additions & 0 deletions KoeBook.Test/MockHttpClientHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Net.Http.Headers;
using System.Text;

namespace KoeBook.Test;

internal class MockHttpClientHandler : HttpClientHandler
{
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
return request.RequestUri?.GetLeftPart(UriPartial.Authority) switch
{
"https://ncode.syosetu.com" => ProcessRequest(request, cancellationToken),
_ => base.Send(request, cancellationToken),
};
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return request.RequestUri?.GetLeftPart(UriPartial.Authority) switch
{
"https://ncode.syosetu.com" => Task.FromResult(ProcessRequest(request, cancellationToken)),
_ => base.SendAsync(request, cancellationToken),
};
}

private HttpResponseMessage ProcessRequest(HttpRequestMessage requestMessage, CancellationToken cancellationToken)
{
if (requestMessage.Method != HttpMethod.Get || requestMessage.Headers.UserAgent.Count == 0)
throw new NotSupportedException();
var (filePath, mediaType, isString) = requestMessage.RequestUri?.ToString() switch
{
"https://ncode.syosetu.com/n0000aa" => ("./TestData/Naro/n0000aa.html", "text/html; charset=UTF-8", true),
"https://ncode.syosetu.com/n0000aa/1" => ("./TestData/Naro/n0000aa/1.html", "text/html; charset=UTF-8", true),
"https://ncode.syosetu.com/n0000aa/2" => ("./TestData/Naro/n0000aa/2.html", "text/html; charset=UTF-8", true),
"https://ncode.syosetu.com/n0000aa/3" => ("./TestData/Naro/n0000aa/3.html", "text/html; charset=UTF-8", true),
_ => throw new NotSupportedException()
};

return new HttpResponseMessage
{
Content = isString
? new StringContent(File.ReadAllText(filePath), Encoding.UTF8, MediaTypeHeaderValue.Parse(mediaType))
: new ByteArrayContent(File.ReadAllBytes(filePath))
{
Headers =
{
ContentType = MediaTypeHeaderValue.Parse(mediaType),
}
}
};
}
}
Loading

0 comments on commit ab04353

Please sign in to comment.