Skip to content

Commit

Permalink
fix(repo): ensure options only accepts one of html or url but not both
Browse files Browse the repository at this point in the history
refactor(repo): move urlboxoptions tests to own file
  • Loading branch information
AJCJ1 authored and cjroebuck committed Jan 21, 2025
1 parent 82f2f43 commit fe67dc0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 63 deletions.
1 change: 0 additions & 1 deletion Urlbox.MsTest/Urlbox.MsTest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- <TargetFramework>netcoreapp2.0</TargetFramework> -->
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
Expand Down
70 changes: 70 additions & 0 deletions Urlbox.MsTest/UrlboxOptionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Diagnostics;
using System.Dynamic;
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Screenshots;

[TestClass]
public class UrlboxOptionsTest
{
[TestMethod]
public void UrlboxOptions_MissingHTMLandURL()
{
Assert.ThrowsException<ArgumentException>(() => new UrlboxOptions());
}

[TestMethod]
public void UrlboxOptions_BothHTMLandURL()
{
var exception = Assert.ThrowsException<ArgumentException>(() => new UrlboxOptions(url: "urlbox.com", html: "<h1>test</h1>"));
Assert.AreEqual(exception.Message, "Either but not both options 'url' or 'html' must be provided.");
}

[TestMethod]
public void UrlboxOptions_CreatesSuccess_URL()
{
string url = "https://urlbox.com";
var urlboxOptions = new UrlboxOptions(url: url);

Assert.IsNotNull(urlboxOptions);
Assert.IsInstanceOfType(urlboxOptions, typeof(UrlboxOptions));
Assert.AreEqual(url, urlboxOptions.Url);
Assert.IsNull(urlboxOptions.Html);
}

[TestMethod]
public void UrlboxOptions_CreatesSuccess_HTML()
{
string html = "<h1>test</h1>";
var urlboxOptions = new UrlboxOptions(html: html);

Assert.IsNotNull(urlboxOptions);
Assert.IsInstanceOfType(urlboxOptions, typeof(UrlboxOptions));
Assert.AreEqual(html, urlboxOptions.Html);
Assert.IsNull(urlboxOptions.Url);
}

[TestMethod]
public void UrlboxOptions_PassingWrongTypeCookie()
{
string html = "<h1>test</h1>";
var urlboxOptions = new UrlboxOptions(html: html);

var exception = Assert.ThrowsException<ArgumentException>(() => urlboxOptions.Cookie = 1);
Assert.IsTrue(exception.Message.Contains("Cookie must be either a string or a string array."));
}

[TestMethod]
public void UrlboxOptions_PassingWrongTypeHeader()
{
string html = "<h1>test</h1>";
var urlboxOptions = new UrlboxOptions(html: html);

var exception = Assert.ThrowsException<ArgumentException>(() => urlboxOptions.Header = 1);
Assert.IsTrue(exception.Message.Contains("Header must be either a string or a string array."));
}
}
59 changes: 1 addition & 58 deletions Urlbox.MsTest/UrlboxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class UrlTests
{
UrlboxOptions urlboxAllOptions = new UrlboxOptions(url: "https://urlbox.com")
{
Html = "test",
Width = 123,
Height = 123,
FullPage = true,
Expand Down Expand Up @@ -177,7 +176,7 @@ public void GenerateUrlboxUrl_WithAllOptions()
var output = dummyUrlbox.GenerateUrlboxUrl(urlboxAllOptions);

Assert.AreEqual(
"https://api.urlbox.com/v1/MY_API_KEY/c40d001f724ed2d798ef8ce9e43d02ead2f05c25/png?url=https%3A%2F%2Furlbox.com&html=test&width=123&height=123&full_page=true&selector=test&clip=test&gpu=true&response_type=test&block_ads=true&hide_cookie_banners=true&click_accept=true&block_urls=test%2Ctest2&block_images=true&block_fonts=true&block_medias=true&block_styles=true&block_scripts=true&block_frames=true&block_fetch=true&block_xhr=true&block_sockets=true&hide_selector=test&js=test&css=test&dark_mode=true&reduced_motion=true&retina=true&thumb_width=123&thumb_height=123&img_fit=test&img_position=test&img_bg=test&img_pad=12%2C10%2C10%2C10&quality=123&transparent=true&max_height=123&download=test&pdf_page_size=test&pdf_page_range=test&pdf_page_width=123&pdf_page_height=123&pdf_margin=test&pdf_margin_top=123&pdf_margin_right=123&pdf_margin_bottom=123&pdf_margin_left=123&pdf_auto_crop=true&pdf_scale=0.12&pdf_orientation=test&pdf_background=true&disable_ligatures=true&media=test&pdf_show_header=true&pdf_header=test&pdf_show_footer=true&pdf_footer=test&readable=true&force=true&unique=test&ttl=123&proxy=test&header=test&cookie=test&user_agent=test&platform=test&accept_lang=test&authorization=test&tz=test&engine_version=test&delay=123&timeout=123&wait_until=test&wait_for=test&wait_to_leave=test&wait_timeout=123&fail_if_selector_missing=true&fail_if_selector_present=true&fail_on4xx=true&fail_on5xx=true&scroll_to=test&click=test&click_all=test&hover=test&bg_color=test&disable_js=true&full_page_mode=test&full_width=true&allow_infinite=true&skip_scroll=true&detect_full_height=true&max_section_height=123&scroll_increment=400&scroll_delay=123&highlight=test&highlight_fg=test&highlight_bg=test&latitude=0.12&longitude=0.12&accuracy=123&use_s3=true&s3_path=test&s3_bucket=test&s3_endpoint=test&s3_region=test&cdn_host=test&s3_storage_class=test",
"https://api.urlbox.com/v1/MY_API_KEY/6d4c63313408cb3127cb12811b4d7b1ab99579d3/png?url=https%3A%2F%2Furlbox.com&width=123&height=123&full_page=true&selector=test&clip=test&gpu=true&response_type=test&block_ads=true&hide_cookie_banners=true&click_accept=true&block_urls=test%2Ctest2&block_images=true&block_fonts=true&block_medias=true&block_styles=true&block_scripts=true&block_frames=true&block_fetch=true&block_xhr=true&block_sockets=true&hide_selector=test&js=test&css=test&dark_mode=true&reduced_motion=true&retina=true&thumb_width=123&thumb_height=123&img_fit=test&img_position=test&img_bg=test&img_pad=12%2C10%2C10%2C10&quality=123&transparent=true&max_height=123&download=test&pdf_page_size=test&pdf_page_range=test&pdf_page_width=123&pdf_page_height=123&pdf_margin=test&pdf_margin_top=123&pdf_margin_right=123&pdf_margin_bottom=123&pdf_margin_left=123&pdf_auto_crop=true&pdf_scale=0.12&pdf_orientation=test&pdf_background=true&disable_ligatures=true&media=test&pdf_show_header=true&pdf_header=test&pdf_show_footer=true&pdf_footer=test&readable=true&force=true&unique=test&ttl=123&proxy=test&header=test&cookie=test&user_agent=test&platform=test&accept_lang=test&authorization=test&tz=test&engine_version=test&delay=123&timeout=123&wait_until=test&wait_for=test&wait_to_leave=test&wait_timeout=123&fail_if_selector_missing=true&fail_if_selector_present=true&fail_on4xx=true&fail_on5xx=true&scroll_to=test&click=test&click_all=test&hover=test&bg_color=test&disable_js=true&full_page_mode=test&full_width=true&allow_infinite=true&skip_scroll=true&detect_full_height=true&max_section_height=123&scroll_increment=400&scroll_delay=123&highlight=test&highlight_fg=test&highlight_bg=test&latitude=0.12&longitude=0.12&accuracy=123&use_s3=true&s3_path=test&s3_bucket=test&s3_endpoint=test&s3_region=test&cdn_host=test&s3_storage_class=test",
output
);
}
Expand Down Expand Up @@ -367,7 +366,6 @@ public async Task Render_ThrowsException()
Assert.IsTrue(exception.Message.Contains("Could not make post request to https://api.urlbox.com/v1/render/sync"));
}


[TestMethod]
public async Task RenderAsync_ThrowsException()
{
Expand All @@ -380,7 +378,6 @@ public async Task RenderAsync_ThrowsException()
[TestClass]
public class DownloadTests
{

private Urlbox urlbox;

[TestInitialize]
Expand Down Expand Up @@ -417,60 +414,6 @@ public async Task TestDownloadFail()
}
}

[TestClass]
public class UrlboxOptionsTest
{
[TestMethod]
public void UrlboxOptions_MissingHTMLandURL()
{
Assert.ThrowsException<ArgumentException>(() => new UrlboxOptions());
}

[TestMethod]
public void UrlboxOptions_CreatesSuccess_URL()
{
string url = "https://urlbox.com";
var urlboxOptions = new UrlboxOptions(url: url);

Assert.IsNotNull(urlboxOptions);
Assert.IsInstanceOfType(urlboxOptions, typeof(UrlboxOptions));
Assert.AreEqual(url, urlboxOptions.Url);
Assert.IsNull(urlboxOptions.Html);
}

[TestMethod]
public void UrlboxOptions_CreatesSuccess_HTML()
{
string html = "<h1>test</h1>";
var urlboxOptions = new UrlboxOptions(html: html);

Assert.IsNotNull(urlboxOptions);
Assert.IsInstanceOfType(urlboxOptions, typeof(UrlboxOptions));
Assert.AreEqual(html, urlboxOptions.Html);
Assert.IsNull(urlboxOptions.Url);
}

[TestMethod]
public void UrlboxOptions_PassingWrongTypeCookie()
{
string html = "<h1>test</h1>";
var urlboxOptions = new UrlboxOptions(html: html);

var exception = Assert.ThrowsException<ArgumentException>(() => urlboxOptions.Cookie = 1);
Assert.IsTrue(exception.Message.Contains("Cookie must be either a string or a string array."));
}

[TestMethod]
public void UrlboxOptions_PassingWrongTypeHeader()
{
string html = "<h1>test</h1>";
var urlboxOptions = new UrlboxOptions(html: html);

var exception = Assert.ThrowsException<ArgumentException>(() => urlboxOptions.Header = 1);
Assert.IsTrue(exception.Message.Contains("Header must be either a string or a string array."));
}
}

[TestClass]
public class UrlboxTests
{
Expand Down
11 changes: 7 additions & 4 deletions Urlbox/Urlbox/Resource/UrlboxOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ public class UrlboxOptions

public UrlboxOptions(string url = null, string html = null)
{
if (string.IsNullOrEmpty(url) && string.IsNullOrEmpty(html))
if (
(String.IsNullOrEmpty(url) && String.IsNullOrEmpty(html)) ||
(!String.IsNullOrEmpty(url) && !String.IsNullOrEmpty(html))
)
{
throw new ArgumentException("Either of options 'url' or 'html' must be provided.");
throw new ArgumentException("Either but not both options 'url' or 'html' must be provided.");
}
Url = url;
Html = html;
}

public string Url { get; set; }
public string Html { get; set; }
public string Url { get; }
public string Html { get; }
public string Format { get; set; } // png jpeg webp avif svg pdf html mp4 webm md
public int Width { get; set; }
public int Height { get; set; }
Expand Down

0 comments on commit fe67dc0

Please sign in to comment.