-
Notifications
You must be signed in to change notification settings - Fork 926
Add new splitQuery API accepting Ktor Url or string #2931
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
Open
Luna712
wants to merge
1
commit into
recloudstream:master
Choose a base branch
from
Luna712:splitquery-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
library/src/commonTest/kotlin/com/lagradost/cloudstream3/SplitQueryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package com.lagradost.cloudstream3 | ||
|
|
||
| import io.ktor.http.Url | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class SplitQueryTest { | ||
|
|
||
| @Test | ||
| fun splitsBasicQueryParameters() { | ||
| val url = Url("https://example.com/path?foo=bar&baz=qux") | ||
| val result = splitQuery(url) | ||
| assertEquals(mapOf("foo" to "bar", "baz" to "qux"), result) | ||
| } | ||
|
|
||
| @Test | ||
| fun decodesUrlEncodedKeysAndValues() { | ||
| val url = Url("https://example.com/path?na%20me=hello%20world&sp%26ec=a%2Bb") | ||
| val result = splitQuery(url) | ||
| assertEquals(mapOf("na me" to "hello world", "sp&ec" to "a+b"), result) | ||
| } | ||
|
|
||
| @Test | ||
| fun returnsEmptyMapWhenThereIsNoQueryString() { | ||
| val url = Url("https://example.com/path") | ||
| val result = splitQuery(url) | ||
| assertTrue(result.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun keepsOnlyFirstValueForRepeatedKeys() { | ||
| val url = Url("https://example.com/path?a=1&a=2&a=3") | ||
| val result = splitQuery(url) | ||
| assertEquals(mapOf("a" to "1"), result) | ||
| } | ||
|
|
||
| @Test | ||
| fun handlesParameterWithNoValue() { | ||
| val url = Url("https://example.com/path?flag&foo=bar") | ||
| val result = splitQuery(url) | ||
| assertEquals("bar", result["foo"]) | ||
| assertEquals("", result["flag"]) | ||
| } | ||
|
|
||
| @Test | ||
| fun stringOverloadSplitsBasicQueryParameters() { | ||
| val result = splitQuery("https://example.com/path?foo=bar&baz=qux") | ||
| assertEquals(mapOf("foo" to "bar", "baz" to "qux"), result) | ||
| } | ||
|
|
||
| @Test | ||
| fun stringOverloadDecodesUrlEncodedKeysAndValues() { | ||
| val result = splitQuery("https://example.com/path?na%20me=hello%20world&sp%26ec=a%2Bb") | ||
| assertEquals(mapOf("na me" to "hello world", "sp&ec" to "a+b"), result) | ||
| } | ||
|
|
||
| @Test | ||
| fun stringOverloadReturnsEmptyMapWhenThereIsNoQueryString() { | ||
| val result = splitQuery("https://example.com/path") | ||
| assertTrue(result.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun stringOverloadKeepsOnlyFirstValueForRepeatedKeys() { | ||
| val result = splitQuery("https://example.com/path?a=1&a=2&a=3") | ||
| assertEquals(mapOf("a" to "1"), result) | ||
| } | ||
|
|
||
| @Test | ||
| fun stringOverloadHandlesParameterWithNoValue() { | ||
| val result = splitQuery("https://example.com/path?flag&foo=bar") | ||
| assertEquals("bar", result["foo"]) | ||
| assertEquals("", result["flag"]) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would prefer to keep the API-surface somewhat dependency-agnostic. There is no need to make the same mistake as
splitQuery(url: java.net.URL)again, when we can just expose a string argument instead.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.
What I was aiming for was to expose a way to accept a direct URL as well as String. By passing a URL it can sometimes be a bit more type safe as well, and also as we shift towards ktor URL more, sometimes a URL may be wanted directly, in which case, I just thought it was better and more straightforward to accept a URL with a separate convenience method accepting a string only as well. I am open to changes though if you think it should be done differently?
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.
I am kinda considering whether this should instead be an extension function on String in StringUtils though, in which case it would make a little more sense to simply drop the URL overload and just accept that extension method. I am honestly not 100% sure on the best option here...
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.
Hmm. Maybe better not in StringUtils (or is it, still haven't decided that) but perhaps it also being called something like
splitUrlParametersorgetUrlParameterswould make more sense and be more clear thansplitQuery?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.
I am thinking maybe just name
splitQuery,parseUrlParametersinstead. It's a more clear naming scheme, and is more consistent with other Url-based naming.