-
Notifications
You must be signed in to change notification settings - Fork 1
[PIMOB-4287]: adding base url prefix to Frames SDK for multi-region #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ed27d58
8c5b4c9
80963da
2861377
0a5ee24
57e489a
36d532b
3dd091f
95aa941
19a922e
41b06d2
dd8eaee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
| package com.checkout.base.model | ||
|
|
||
| import com.checkout.base.util.EnvironmentConstants | ||
|
|
||
| public enum class Environment(public val url: String) { | ||
| PRODUCTION(EnvironmentConstants.PRODUCTION_SERVER_URL), | ||
| SANDBOX(EnvironmentConstants.SANDBOX_SERVER_URL), | ||
| public enum class Environment { | ||
| PRODUCTION, | ||
| SANDBOX, | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,32 @@ | ||||||||
| package com.checkout.logging.utils | ||||||||
|
|
||||||||
| import com.checkout.base.model.Environment | ||||||||
| import com.checkout.base.util.EnvironmentConstants.PRODUCTION_LOGGING | ||||||||
| import com.checkout.base.util.EnvironmentConstants.PRODUCTION_SERVER_URL | ||||||||
| import com.checkout.base.util.EnvironmentConstants.SANDBOX_LOGGING | ||||||||
| import com.checkout.base.util.EnvironmentConstants.SANDBOX_SERVER_URL | ||||||||
| import com.checkout.base.util.HTTPS_PROTOCOL | ||||||||
|
|
||||||||
| internal fun Environment.toBaseUrl(baseUrlPrefix: String? = null) = when (this) { | ||||||||
| Environment.PRODUCTION -> PRODUCTION_SERVER_URL.applyPrefix(baseUrlPrefix) | ||||||||
| Environment.SANDBOX -> SANDBOX_SERVER_URL.applyPrefix(baseUrlPrefix) | ||||||||
| } | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit for formatting consistency between functions' spacing
Suggested change
|
||||||||
| internal fun Environment.toLoggingEnvironment() = when (this) { | ||||||||
| Environment.PRODUCTION -> com.checkout.eventlogger.Environment.PRODUCTION | ||||||||
| Environment.SANDBOX -> com.checkout.eventlogger.Environment.SANDBOX | ||||||||
| } | ||||||||
|
|
||||||||
| internal fun Environment.toLoggingName() = when (this) { | ||||||||
| Environment.PRODUCTION -> "production" | ||||||||
| Environment.SANDBOX -> "sandbox" | ||||||||
| Environment.PRODUCTION -> PRODUCTION_LOGGING | ||||||||
| Environment.SANDBOX -> SANDBOX_LOGGING | ||||||||
| } | ||||||||
|
|
||||||||
| private fun String.applyPrefix(prefix: String?): String { | ||||||||
| val validatedPrefix = prefix?.baseUrlPrefixValidator() ?: return this | ||||||||
| return this.replace(HTTPS_PROTOCOL, "$HTTPS_PROTOCOL$validatedPrefix.") | ||||||||
| } | ||||||||
|
|
||||||||
| private fun String?.baseUrlPrefixValidator() = this | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function is declared as an extension on String?, but it's always called via the safe-call operator |
||||||||
| ?.takeIf { prefix -> | ||||||||
| prefix.all { char -> char in 'a'..'z' || char in 'A'..'Z' || char in '0'..'9' } && prefix.isNotEmpty() | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reorder the isNotEmpty() check to come first. In the validator, prefix.all { ... } returns true for an empty string (vacuous truth in Kotlin). The logic is correct because
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The validator only allows [a-zA-Z0-9]. Per RFC 1123, valid subdomain labels can also contain hyphens (-), e.g., my-subdomain. If this restriction is intentional, it might be worth a brief comment explaining why hyphens are excluded. If not, the range check should include char == '-'. |
||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package com.checkout | ||
|
|
||
| import com.checkout.base.model.Environment | ||
| import com.checkout.base.util.EnvironmentConstants | ||
| import com.checkout.base.util.EnvironmentConstants.PRODUCTION_SERVER_URL | ||
| import com.checkout.logging.utils.toBaseUrl | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| internal class EnvironmentExtensionTest { | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl PRODUCTION with null subDomainPrefix returns production server URL`() { | ||
| val result = Environment.PRODUCTION.toBaseUrl(null) | ||
| assertEquals(EnvironmentConstants.PRODUCTION_SERVER_URL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl PRODUCTION with no subDomainPrefix argument returns production server URL`() { | ||
| val result = Environment.PRODUCTION.toBaseUrl() | ||
| assertEquals(EnvironmentConstants.PRODUCTION_SERVER_URL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl PRODUCTION with subDomainPrefix returns custom production URL`() { | ||
| val result = Environment.PRODUCTION.toBaseUrl("custom") | ||
| assertEquals("https://custom.api.checkout.com/tokens", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl PRODUCTION with alphanumeric subDomainPrefix returns custom production URL`() { | ||
| val result = Environment.PRODUCTION.toBaseUrl("mySubdomain123") | ||
| assertEquals("https://mySubdomain123.api.checkout.com/tokens", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl SANDBOX with null subDomainPrefix returns sandbox server URL`() { | ||
| val result = Environment.SANDBOX.toBaseUrl(null) | ||
| assertEquals(EnvironmentConstants.SANDBOX_SERVER_URL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl SANDBOX with no subDomainPrefix argument returns sandbox server URL`() { | ||
| val result = Environment.SANDBOX.toBaseUrl() | ||
| assertEquals(EnvironmentConstants.SANDBOX_SERVER_URL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl SANDBOX with subDomainPrefix returns custom sandbox URL`() { | ||
| val result = Environment.SANDBOX.toBaseUrl("custom") | ||
| assertEquals("https://custom.api.sandbox.checkout.com/tokens", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl SANDBOX with alphanumeric subDomainPrefix returns custom sandbox URL`() { | ||
| val result = Environment.SANDBOX.toBaseUrl("test99") | ||
| assertEquals("https://test99.api.sandbox.checkout.com/tokens", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when toBaseUrl with empty string subDomainPrefix uses default URL for production`() { | ||
| val result = Environment.PRODUCTION.toBaseUrl("") | ||
| assertEquals("https://api.checkout.com/tokens", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `toBaseUrl PRODUCTION returns default URL for any non-alphanumeric prefix`() { | ||
zane-smith-cko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val invalidPrefixList = listOf( | ||
| "invalid_prefix", | ||
| "invalid-prefix", | ||
| "invalid.prefix", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the top 3 look like they could be valid prefixes - worth checking |
||
| "invalid@prefix", | ||
| "invalid!prefix", | ||
| "invalid#prefix", | ||
| "invalid\$prefix", | ||
| "invalid%prefix", | ||
| "invalid^prefix", | ||
| "invalid&prefix", | ||
| "invalid*prefix", | ||
| "invalid(prefix", | ||
| "invalid)prefix", | ||
| "invalid prefix", | ||
| ) | ||
|
|
||
| invalidPrefixList.forEach { invalidPrefix -> | ||
| val result = Environment.PRODUCTION.toBaseUrl(invalidPrefix) | ||
| assertEquals( | ||
| PRODUCTION_SERVER_URL, | ||
| result, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.