Skip to content

Commit

Permalink
feat(repo): add tests for render and render async endpoints for succe…
Browse files Browse the repository at this point in the history
…ss and failure cases

fix(repo): fix blockUrls to be arr
  • Loading branch information
AJCJ1 authored and cjroebuck committed Jan 21, 2025
1 parent 8dbc95c commit a33a6db
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
51 changes: 49 additions & 2 deletions Urlbox.MsTest/UrlboxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class UrlTests
BlockAds = true,
HideCookieBanners = true,
ClickAccept = true,
BlockUrls = true,
BlockUrls = new string[] { "test", "test2" },
BlockImages = true,
BlockFonts = true,
BlockMedias = true,
Expand Down Expand Up @@ -161,7 +161,7 @@ public void GenerateUrlboxUrl_WithAllOptions()
Console.WriteLine(output);

Assert.AreEqual(
"https://api.urlbox.com/v1/MY_API_KEY/1f11feec77221ee8e21911c342d6b3ba8d2b5153/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=true&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=123&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=test&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/755a40f67ca365e979086eddd0d2cab7e7612174/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=%5Btest%2Ctest2%5D&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=123&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=test&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 @@ -255,6 +255,53 @@ public void GenerateUrlboxUrl_ShouldRemoveFormatFromQueryString()

Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/bba10010e9ece486d34a82344170ae5b4dd5f347/png?url=https%3A%2F%2Furlbox.com&full_page=true", output);
}

[TestMethod]
public async Task Render_Succeeds()
{
UrlboxOptions options = new UrlboxOptions(url: "https://urlbox.com");
// options.BlockUrls = true;
// options.FullPage = true;
options.ClickAccept = true;
var result = await urlbox.Render(options);
Assert.IsNotNull(result.RenderUrl);
Assert.IsNotNull(result.Size);
}

[TestMethod]
public async Task Render_ThrowsException()
{
UrlboxOptions options = new UrlboxOptions(url: "https://doesnotexistZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.com");
var exception = Assert.ThrowsExceptionAsync<ArgumentException>(async () => await urlbox.Render(options));
Assert.IsTrue(exception.Result.Message.Contains("Could not make post request to https://api.urlbox.com/v1/render/sync"));
}


[TestMethod]
public async Task RenderAsync_Succeeds()
{
UrlboxOptions options = new UrlboxOptions(url: "https://urlbox.com");
options.BlockUrls = new string[] { "test" };
// options.FullPage = true;
options.ClickAccept = true;
var result = await urlbox.RenderAsync(options);

Assert.IsInstanceOfType(result, typeof(AsyncUrlboxResponse));
Assert.IsNotNull(result.Status);
Assert.IsNotNull(result.RenderId);
Assert.IsNotNull(result.StatusUrl);
}

[TestMethod]
public async Task RenderAsync_ThrowsException()
{
UrlboxOptions options = new UrlboxOptions(url: "https://doesnotexistZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.com");
var exception = Assert.ThrowsExceptionAsync<ArgumentException>(async () => await urlbox.RenderAsync(options));
Assert.IsTrue(exception.Result.Message.Contains("Could not make post request to https://api.urlbox.com/v1/render/async"));
}



}

[TestClass]
Expand Down
13 changes: 9 additions & 4 deletions Urlbox/Urlbox/UrlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ private string ToQueryString(UrlboxOptions options)
{
var value = prop.GetValue(options, null);
return value != null &&
!(value is bool && (bool)value == false) && // skip false if bool
!(value is int && (int)value == 0) && // skip 0's if int
!(value is double && (double)value == 0.0) && // skip 0's if double
!(value is string && string.IsNullOrEmpty((string)value)); // skip empty strings if string
!(value is bool && (bool)value == false) && // skip false if bool
!(value is int && (int)value == 0) && // skip 0's if int
!(value is double && (double)value == 0.0) && // skip 0's if double
!(value is string && string.IsNullOrEmpty((string)value)) && // skip empty strings if string
!(value is string[] arr && arr.Length == 0); // skip empty arrays
})
.Select(prop => new KeyValuePair<string, string>(prop.Name, ConvertToString(prop.GetValue(options))))
// .Where(pair => !string.IsNullOrEmpty(pair.Value)) // Skip empty values
Expand All @@ -61,6 +62,10 @@ private static string FormatKeyName(string input)

private static string ConvertToString(object value)
{
if (value is string[] stringArray)
{
return $"[{string.Join(",", stringArray)}]";
}

var result = Convert.ToString(value);
if (result.Equals("False") || result.Equals("True"))
Expand Down
2 changes: 1 addition & 1 deletion Urlbox/Urlbox/Urlbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public UrlboxOptions(string url = null, string html = null)
public bool BlockAds { get; set; }
public bool HideCookieBanners { get; set; }
public bool ClickAccept { get; set; }
public bool BlockUrls { get; set; }
public string[] BlockUrls { get; set; }
public bool BlockImages { get; set; }
public bool BlockFonts { get; set; }
public bool BlockMedias { get; set; }
Expand Down

0 comments on commit a33a6db

Please sign in to comment.