Skip to content

Commit c0f5834

Browse files
committed
Test new WithDownload
1 parent e16ba09 commit c0f5834

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace AngleSharp.Io.Tests.Integration
2+
{
3+
using AngleSharp.Dom;
4+
using AngleSharp.Html.Dom;
5+
using NUnit.Framework;
6+
using System.Threading.Tasks;
7+
8+
[TestFixture]
9+
public class DownloadTest
10+
{
11+
[Test]
12+
public async Task DownloadPngIfWanted()
13+
{
14+
var downloadSeen = false;
15+
var config = Configuration.Default.WithDownload((type, response) =>
16+
{
17+
if (type == new MimeType(MimeTypeNames.Png))
18+
{
19+
downloadSeen = true;
20+
return true;
21+
}
22+
23+
return false;
24+
});
25+
var context = BrowsingContext.New(config);
26+
var document = await context.OpenAsync(req => req.Content("<a href=foo.png>Go to some image</a>"));
27+
var linkedDocument = await document.QuerySelector<IHtmlAnchorElement>("a").NavigateAsync();
28+
29+
Assert.IsTrue(downloadSeen);
30+
Assert.IsNull(linkedDocument);
31+
Assert.AreEqual(document, context.Active);
32+
}
33+
34+
[Test]
35+
public async Task DownloadBinaryNotReceived()
36+
{
37+
var downloadSeen = false;
38+
var config = Configuration.Default.WithDownload((type, response) =>
39+
{
40+
if (type == new MimeType(MimeTypeNames.Binary))
41+
{
42+
downloadSeen = true;
43+
return true;
44+
}
45+
46+
return false;
47+
});
48+
var context = BrowsingContext.New(config);
49+
var document = await context.OpenAsync(req => req.Content("<a href=foo.png>Go to some image</a>"));
50+
var linkedDocument = await document.QuerySelector<IHtmlAnchorElement>("a").NavigateAsync();
51+
52+
Assert.IsFalse(downloadSeen);
53+
Assert.IsNotNull(linkedDocument);
54+
Assert.AreNotEqual(document, context.Active);
55+
}
56+
}
57+
}

src/AngleSharp.Io/Network/DownloadFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected override Task<IDocument> CreateDefaultAsync(IBrowsingContext context,
2020
{
2121
if (_download(options.ContentType, options.Response))
2222
{
23-
return context.OpenNewAsync(options.Response.Address.Href, cancellationToken);
23+
return Task.FromResult<IDocument>(null);
2424
}
2525

2626
return _documentFactory.CreateAsync(context, options, cancellationToken);

0 commit comments

Comments
 (0)