Skip to content

Commit

Permalink
Accept responses where the ID was converted to a String (flutter#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkonyi authored Sep 2, 2020
1 parent 995611c commit 8f189db
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.3-dev (unreleased)

* Accept responses even if the server converts the ID to a String.

## 2.2.2

* Fix `Peer.close()` throwing `Bad state: Future already completed`.
Expand Down
8 changes: 6 additions & 2 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ class Client {
/// resolved.
void _handleSingleResponse(response) {
if (!_isResponseValid(response)) return;
var request = _pendingRequests.remove(response['id']);
var id = response['id'];
id = (id is String) ? int.parse(id) : id;
var request = _pendingRequests.remove(id);
if (response.containsKey('result')) {
request.completer.complete(response['result']);
} else {
Expand All @@ -212,7 +214,9 @@ class Client {
bool _isResponseValid(response) {
if (response is! Map) return false;
if (response['jsonrpc'] != '2.0') return false;
if (!_pendingRequests.containsKey(response['id'])) return false;
var id = response['id'];
id = (id is String) ? int.parse(id) : id;
if (!_pendingRequests.containsKey(id)) return false;
if (response.containsKey('result')) return true;

if (!response.containsKey('error')) return false;
Expand Down
21 changes: 21 additions & 0 deletions test/client/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ void main() {
completion(equals('bar')));
});

test('sends a message and returns the response with String id', () {
controller.expectRequest((request) {
expect(
request,
allOf([
containsPair('jsonrpc', '2.0'),
containsPair('method', 'foo'),
containsPair('params', {'param': 'value'})
]));

return {
'jsonrpc': '2.0',
'result': 'bar',
'id': request['id'].toString()
};
});

expect(controller.client.sendRequest('foo', {'param': 'value'}),
completion(equals('bar')));
});

test('sends a notification and expects no response', () {
controller.expectRequest((request) {
expect(
Expand Down

0 comments on commit 8f189db

Please sign in to comment.