Skip to content

Commit

Permalink
feat(repo): add ability to override base url, with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AJCJ1 authored and cjroebuck committed Jan 21, 2025
1 parent 3a3d5f7 commit e16e7dd
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 12 deletions.
File renamed without changes.
55 changes: 55 additions & 0 deletions Urlbox.MsTest/Resource/UrlboxBaseUrlTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Configuration;
using UrlboxSDK;

[TestClass]
public class UrlboxRegionTest
{
private Urlbox urlbox;
private UrlGenerator urlGenerator;

[TestInitialize]
public void TestInitialize()
{
// Build configuration to load user secrets
var builder = new ConfigurationBuilder()
.AddUserSecrets<UrlTests>();

IConfiguration configuration = builder.Build();

// Attempt to load from environment variables first (for GH Actions)
var urlboxKey = Environment.GetEnvironmentVariable("URLBOX_KEY")
?? configuration["URLBOX_KEY"]; // Fallback to User Secrets for local dev

var urlboxSecret = Environment.GetEnvironmentVariable("URLBOX_SECRET")
?? configuration["URLBOX_SECRET"]; // Fallback to User Secrets for local dev

if (string.IsNullOrEmpty(urlboxKey) || string.IsNullOrEmpty(urlboxSecret))
{
throw new ArgumentException("Please configure a URLBox key and secret.");
}
// With genuine API key and Secret
urlbox = new Urlbox(urlboxKey, urlboxSecret, "webhook_secret");
urlGenerator = new UrlGenerator("MY_API_KEY", "secret");
}


[TestMethod]
public void Baseurl_NotSet()
{
var fromCredentials = Urlbox.FromCredentials("MY_API_KEY", "secret", "webhook_secret");
Assert.IsInstanceOfType(fromCredentials, typeof(Urlbox));
var fromNew = Urlbox.FromCredentials("MY_API_KEY", "secret", "webhook_secret");
Assert.IsInstanceOfType(fromNew, typeof(Urlbox));
}

[TestMethod]
public void Baseurl_included()
{
var fromCredentials = Urlbox.FromCredentials("MY_API_KEY", "secret", "webhook_secret", "someBaseUrl");
Assert.IsInstanceOfType(fromCredentials, typeof(Urlbox));
var fromNew = Urlbox.FromCredentials("MY_API_KEY", "secret", "webhook_secret", "someBaseUrl");
Assert.IsInstanceOfType(fromNew, typeof(Urlbox));
}
}
27 changes: 26 additions & 1 deletion Urlbox.MsTest/Resource/UrlboxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public class UrlTests
};

private Urlbox urlbox;
private Urlbox urlboxEu;
private Urlbox dummyUrlbox;
private UrlGenerator urlGenerator;

Expand All @@ -143,6 +144,7 @@ public void TestInitialize()
}
// With genuine API key and Secret
urlbox = new Urlbox(urlboxKey, urlboxSecret, "webhook_secret");
urlboxEu = new Urlbox(urlboxKey, urlboxSecret, "webhook_secret", "https://api-eu.urlbox.com");
urlGenerator = new UrlGenerator("MY_API_KEY", "secret");

// With dummy API key and Secret
Expand Down Expand Up @@ -186,6 +188,17 @@ public void GenerateRenderLink_WithAllOptions()
);
}

[TestMethod]
public void GenerateRenderLink_eu()
{
var output = urlboxEu.GenerateRenderLink(urlboxAllOptions);

Assert.AreEqual(
"https://api-eu.urlbox.com/v1/rDksAC9TwlPFqvWw/png?accept_lang=test&accuracy=123&allow_infinite=true&authorization=test&bg_color=test&block_ads=true&block_fetch=true&block_fonts=true&block_frames=true&block_images=true&block_medias=true&block_scripts=true&block_sockets=true&block_styles=true&block_urls=test%2Ctest2&block_xhr=true&cdn_host=test&click=test&click_accept=true&click_all=test&clip=test&cookie=test&css=test&dark_mode=true&delay=123&detect_full_height=true&disable_js=true&disable_ligatures=true&download=test&engine_version=test&fail_if_selector_missing=true&fail_if_selector_present=true&fail_on4xx=true&fail_on5xx=true&force=true&full_page=true&full_page_mode=stitch&full_width=true&gpu=true&header=test&height=123&hide_cookie_banners=true&hide_selector=test&highlight=test&highlight_bg=test&highlight_fg=test&hover=test&img_bg=test&img_fit=contain&img_pad=12%2C10%2C10%2C10&img_position=northeast&js=test&latitude=0.12&longitude=0.12&max_height=123&max_section_height=123&media=print&pdf_auto_crop=true&pdf_background=true&pdf_footer=test&pdf_header=test&pdf_margin=default&pdf_margin_bottom=123&pdf_margin_left=123&pdf_margin_right=123&pdf_margin_top=123&pdf_orientation=portrait&pdf_page_height=123&pdf_page_range=test&pdf_page_size=Tabloid&pdf_page_width=123&pdf_scale=0.12&pdf_show_footer=true&pdf_show_header=true&platform=Linux%20x86_64&proxy=test&quality=123&readable=true&reduced_motion=true&response_type=json&retina=true&s3_bucket=test&s3_endpoint=test&s3_path=test&s3_region=test&s3_storage_class=standard&scroll_delay=123&scroll_increment=400&scroll_to=test&selector=test&skip_scroll=true&thumb_height=123&thumb_width=123&timeout=123&transparent=true&ttl=123&tz=test&unique=test&url=https%3A%2F%2Furlbox.com&user_agent=test&use_s3=true&wait_for=test&wait_timeout=123&wait_to_leave=test&wait_until=domloaded&webhook_url=https%3A%2F%2Fan-ngrok-endpoint&width=123",
output
);
}

[TestMethod]
public void GenerateRenderLink_withMultipleCookies()
{
Expand Down Expand Up @@ -333,7 +346,7 @@ public void GenerateRenderLink_ShouldRemoveFormatFromQueryString()
Format = UrlboxOptions.FormatOption.png,
FullPage = true
};
var output = urlGenerator.GenerateRenderLink(options);
var output = urlGenerator.GenerateRenderLink(Urlbox.BASE_URL, options);

Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/png?full_page=true&url=https%3A%2F%2Furlbox.com", output);
}
Expand All @@ -350,6 +363,18 @@ public async Task RenderSync_Succeeds()
Assert.IsNotNull(result.Size);
}

[TestMethod]
public async Task RenderSync_Succeeds_eu()
{
UrlboxOptions options = Urlbox.Options(url: "https://urlbox.com").Build();
options.ClickAccept = true;
SyncUrlboxResponse result = await urlboxEu.Render(options);

Assert.IsInstanceOfType(result, typeof(SyncUrlboxResponse));
Assert.IsNotNull(result.RenderUrl);
Assert.IsNotNull(result.Size);
}

[TestMethod]
public async Task RenderSync_SucceedsWithAllSideRenders()
{
Expand Down
19 changes: 11 additions & 8 deletions Urlbox/Urlbox/Resource/Urlbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public sealed class Urlbox : IUrlbox
private readonly UrlGenerator urlGenerator;
private readonly UrlboxWebhookValidator? urlboxWebhookValidator;
private readonly HttpClient httpClient;
public const string BASE_URL = "https://api.urlbox.com";
private readonly string baseUrl;
public const string DOMAIN = "urlbox.com";
public const string BASE_URL = "https://api." + DOMAIN;
private const string SYNC_ENDPOINT = "/v1/render/sync";
private const string ASYNC_ENDPOINT = "/v1/render/async";
private const string STATUS_ENDPOINT = BASE_URL + "/v1/render";
private const string STATUS_ENDPOINT = "/v1/render";
public const int DEFAULT_TIMEOUT = 60000; // 60 seconds

/// <summary>
Expand All @@ -34,7 +36,7 @@ public static UrlboxOptionsBuilder Options(
string? html = null
) => new(url, html);

public Urlbox(string key, string secret, string? webhookSecret = null)
public Urlbox(string key, string secret, string? webhookSecret = null, string? baseUrl = BASE_URL)
{
if (String.IsNullOrEmpty(key))
{
Expand All @@ -45,6 +47,7 @@ public Urlbox(string key, string secret, string? webhookSecret = null)
throw new ArgumentException("Please provide your Urlbox.com API Secret");
}
this.secret = secret;
this.baseUrl = baseUrl ?? BASE_URL;
urlGenerator = new UrlGenerator(key, secret);
httpClient = new HttpClient();
if (!String.IsNullOrEmpty(webhookSecret))
Expand All @@ -64,9 +67,9 @@ public Urlbox(string key, string secret, string? webhookSecret = null)
/// <param name="client"></param>
/// <returns>A new instance of the Urlbox class.</returns>
/// <exception cref="ArgumentException">Thrown when there is no api key or secret</exception>
public static Urlbox FromCredentials(string apiKey, string apiSecret, string webhookSecret)
public static Urlbox FromCredentials(string apiKey, string apiSecret, string? webhookSecret, string? baseUrl = BASE_URL)
{
return new Urlbox(apiKey, apiSecret, webhookSecret);
return new Urlbox(apiKey, apiSecret, webhookSecret, baseUrl);
}

/// <summary>
Expand Down Expand Up @@ -345,7 +348,7 @@ public string GeneratePDFUrl(UrlboxOptions options, bool sign = false)
/// <returns>A render link URL to render the content.</returns>
public string GenerateRenderLink(UrlboxOptions options, string format = "png", bool sign = false)
{
return urlGenerator.GenerateRenderLink(options, format, sign);
return urlGenerator.GenerateRenderLink(this.baseUrl, options, format, sign);
}

/// <summary>
Expand All @@ -356,7 +359,7 @@ public string GenerateRenderLink(UrlboxOptions options, string format = "png", b
/// <returns>A render link URL to render the content.</returns>
public string GenerateSignedRenderLink(UrlboxOptions options, string format = "png")
{
return urlGenerator.GenerateRenderLink(options, format, true);
return urlGenerator.GenerateRenderLink(this.baseUrl, options, format, true);
}

// ** Status and Validation Methods **
Expand All @@ -367,7 +370,7 @@ public string GenerateSignedRenderLink(UrlboxOptions options, string format = "p
/// <returns></returns>
public async Task<AsyncUrlboxResponse> GetStatus(string renderId)
{
string statusUrl = $"{STATUS_ENDPOINT}/{renderId}";
string statusUrl = $"{this.baseUrl}{STATUS_ENDPOINT}/{renderId}";
HttpResponseMessage response = await httpClient.GetAsync(statusUrl);
if (response.IsSuccessStatusCode)
{
Expand Down
6 changes: 3 additions & 3 deletions Urlbox/Urlbox/UrlGenerator/UrlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ private static string ConvertToString(object value)
/// <param name="options"></param>
/// <param name="format"></param>
/// <returns>The Urlbox Render Link</returns>
public string GenerateRenderLink(UrlboxOptions options, string format = "png", bool sign = false)
public string GenerateRenderLink(string baseUrl, UrlboxOptions options, string format = "png", bool sign = false)
{
var queryString = ToQueryString(options);
if (sign)
{
return string.Format(Urlbox.BASE_URL + "/v1/{0}/{1}/{2}?{3}",
return string.Format(baseUrl + "/v1/{0}/{1}/{2}?{3}",
this.key,
generateToken(queryString),
format,
Expand All @@ -115,7 +115,7 @@ public string GenerateRenderLink(UrlboxOptions options, string format = "png", b
}
else
{
return string.Format(Urlbox.BASE_URL + "/v1/{0}/{1}?{2}",
return string.Format(baseUrl + "/v1/{0}/{1}?{2}",
this.key,
format,
queryString
Expand Down

0 comments on commit e16e7dd

Please sign in to comment.