forked from dart-archive/vm_service_drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper to convert Observatory URI to WS URI
This is to make it easier to reuse in devtools+devtools_server. See flutter/devtools#563 (comment).
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,18 @@ | ||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// Map the URI (which may already be Observatory web app) to a WebSocket URI | ||
/// for the VM service. | ||
/// | ||
/// If the URI is already a VM Service WebSocket URI it will not be modified. | ||
Uri getVmWsUriFromObservatoryUri(Uri uri) { | ||
final isSecure = uri.isScheme('wss') || uri.isScheme('https'); | ||
final scheme = isSecure ? 'wss' : 'ws'; | ||
|
||
final path = uri.path.endsWith('/ws') | ||
? uri.path | ||
: (uri.path.endsWith('/') ? '${uri.path}ws' : '${uri.path}/ws'); | ||
|
||
return uri.replace(scheme: scheme, path: path); | ||
} |
This file contains 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,66 @@ | ||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:vm_service_lib/utils.dart'; | ||
|
||
@TestOn('vm') | ||
void main() { | ||
group('getVmWsUriFromObservatoryUri', () { | ||
void check(String input, String expected) { | ||
final inputUri = Uri.parse(input); | ||
final actualUri = getVmWsUriFromObservatoryUri(inputUri); | ||
expect(actualUri.toString(), equals(expected)); | ||
} | ||
|
||
test('handles http URIs', | ||
() => check('http://localhost:123/', 'ws://localhost:123/ws')); | ||
test('handles https URIs', | ||
() => check('https://localhost:123/', 'wss://localhost:123/ws')); | ||
test('handles ws URIs', | ||
() => check('ws://localhost:123/', 'ws://localhost:123/ws')); | ||
test('handles wss URIs', | ||
() => check('wss://localhost:123/', 'wss://localhost:123/ws')); | ||
test( | ||
'handles http URIs with tokens', | ||
() => check( | ||
'http://localhost:123/ABCDEF=/', 'ws://localhost:123/ABCDEF=/ws')); | ||
test( | ||
'handles https URIs with tokens', | ||
() => check('https://localhost:123/ABCDEF=/', | ||
'wss://localhost:123/ABCDEF=/ws')); | ||
test( | ||
'handles ws URIs with tokens', | ||
() => check( | ||
'ws://localhost:123/ABCDEF=/', 'ws://localhost:123/ABCDEF=/ws')); | ||
test( | ||
'handles wss URIs with tokens', | ||
() => check( | ||
'wss://localhost:123/ABCDEF=/', 'wss://localhost:123/ABCDEF=/ws')); | ||
test('handles http URIs without trailing slashes', | ||
() => check('http://localhost:123', 'ws://localhost:123/ws')); | ||
test('handles https URIs without trailing slashes', | ||
() => check('https://localhost:123', 'wss://localhost:123/ws')); | ||
test('handles ws URIs without trailing slashes', | ||
() => check('ws://localhost:123', 'ws://localhost:123/ws')); | ||
test('handles wss URIs without trailing slashes', | ||
() => check('wss://localhost:123', 'wss://localhost:123/ws')); | ||
test( | ||
'handles http URIs without trailing slashes with tokens', | ||
() => check( | ||
'http://localhost:123/ABCDEF=', 'ws://localhost:123/ABCDEF=/ws')); | ||
test( | ||
'handles https URIs without trailing slashes with tokens', | ||
() => check( | ||
'https://localhost:123/ABCDEF=', 'wss://localhost:123/ABCDEF=/ws')); | ||
test( | ||
'handles ws URIs without trailing slashes with tokens', | ||
() => check( | ||
'ws://localhost:123/ABCDEF=', 'ws://localhost:123/ABCDEF=/ws')); | ||
test( | ||
'handles wss URIs without trailing slashes with tokens', | ||
() => check( | ||
'wss://localhost:123/ABCDEF=', 'wss://localhost:123/ABCDEF=/ws')); | ||
}); | ||
} |