-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(share_plus): Fix url encoding for web, Linux and Windows #236
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c3113d7
Fix url encoding
ueman 8bf2d4a
Fix Windows
ueman 4095672
Build query params the right way
ueman 9fd790b
Added tests
ueman 79aac1c
Improve tests
ueman 3ab2371
Merge branch 'main' into fix/url-encoding
miquelbeltran 01b214e
bump versions and changelogs
miquelbeltran 7a8b188
add test dependencies
miquelbeltran 7c64831
fix tests
ueman 2e5009f
empty commit
miquelbeltran 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:share_plus_linux/share_plus_linux.dart'; | ||
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; | ||
import 'package:url_launcher_platform_interface/link.dart'; | ||
|
||
void main() { | ||
test('url encoding is correct', () { | ||
final mock = MockUrlLauncherPlatform(); | ||
UrlLauncherPlatform.instance = mock; | ||
|
||
ShareLinux().share('foo&bar', subject: 'bar&foo'); | ||
|
||
expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar'); | ||
ueman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
test('throws when url_launcher can\'t launch uri', () async { | ||
ueman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final mock = MockUrlLauncherPlatform(); | ||
mock.canLaunchMockValue = false; | ||
UrlLauncherPlatform.instance = mock; | ||
|
||
expect(() async => await ShareLinux().share('foo bar'), throwsException); | ||
}); | ||
} | ||
|
||
class MockUrlLauncherPlatform extends UrlLauncherPlatform { | ||
ueman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String? url; | ||
bool canLaunchMockValue = true; | ||
|
||
@override | ||
LinkDelegate? get linkDelegate => throw UnimplementedError(); | ||
|
||
@override | ||
Future<bool> canLaunch(String url) async { | ||
return canLaunchMockValue; | ||
} | ||
|
||
@override | ||
Future<bool> launch( | ||
String url, { | ||
required bool useSafariVC, | ||
required bool useWebView, | ||
required bool enableJavaScript, | ||
required bool enableDomStorage, | ||
required bool universalLinksOnly, | ||
required Map<String, String> headers, | ||
String? webOnlyWindowName, | ||
}) async { | ||
this.url = url; | ||
return true; | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
packages/share_plus_windows/test/share_plus_windows_test.dart
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,51 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:share_plus_windows/share_plus_windows.dart'; | ||
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; | ||
import 'package:url_launcher_platform_interface/link.dart'; | ||
|
||
void main() { | ||
test('url encoding is correct', () { | ||
final mock = MockUrlLauncherPlatform(); | ||
UrlLauncherPlatform.instance = mock; | ||
|
||
ShareWindows().share('foo&bar', subject: 'bar&foo'); | ||
|
||
expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar'); | ||
}); | ||
|
||
test('throws when url_launcher can\'t launch uri', () async { | ||
final mock = MockUrlLauncherPlatform(); | ||
mock.canLaunchMockValue = false; | ||
UrlLauncherPlatform.instance = mock; | ||
|
||
expect(() async => await ShareWindows().share('foo bar'), throwsException); | ||
}); | ||
} | ||
|
||
class MockUrlLauncherPlatform extends UrlLauncherPlatform { | ||
String? url; | ||
bool canLaunchMockValue = true; | ||
|
||
@override | ||
LinkDelegate? get linkDelegate => throw UnimplementedError(); | ||
|
||
@override | ||
Future<bool> canLaunch(String url) async { | ||
return canLaunchMockValue; | ||
} | ||
|
||
@override | ||
Future<bool> launch( | ||
String url, { | ||
required bool useSafariVC, | ||
required bool useWebView, | ||
required bool enableJavaScript, | ||
required bool enableDomStorage, | ||
required bool universalLinksOnly, | ||
required Map<String, String> headers, | ||
String? webOnlyWindowName, | ||
}) async { | ||
this.url = url; | ||
return true; | ||
} | ||
} |
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.
very nice!!
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.
FYI, this is actually wrong due to a Dart bug (which is why I didn't suggest this format when I filed the issue). See dart-lang/sdk#43838, especially dart-lang/sdk#43838 (comment).
This will fix the issue I filed, but may cause spaces to be converted to
+
depending on the handler.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.
That's unfortunate 😅 Are there more gotchas which I should be aware of before attempting another fix?
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.
None that I know of, no. I would suggest including spaces in the values in your unit tests on the next round, so that tests will prevent someone accidentally falling into this trap again in the future when maintaining these plugins (although hopefully the issue will be fixed in Dart itself at some point).