Skip to content

Commit f4b67c4

Browse files
committed
Added test cases for flutter
1 parent 4af3709 commit f4b67c4

8 files changed

+87
-19
lines changed

README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,33 @@
22

33
For Flutter:
44
```dart
5-
var client = new Client('ws://localhost:3000', 100);
5+
var client = new FlutterSocketClient('ws://localhost:3000');
6+
```
7+
8+
For Web:
9+
```dart
10+
var client = new WebSocketClient('ws://localhost:3000');
11+
```
12+
13+
Inspired by Socket.io
14+
```dart
615
client.authenticate({
7-
"scope": "Mobile",
8-
"version": "v1_0",
9-
"platform": "Android",
10-
"device": "Sony Xperia Z",
11-
"osVersion": "1234",
12-
"ipAddress": "127.0.0.1"
16+
"scope": "Mobile",
17+
"version": "v1_0",
18+
"platform": "Android",
19+
"device": "Sony Xperia Z",
20+
"osVersion": "1234",
21+
"ipAddress": "127.0.0.1"
1322
});
1423
client.onConnection(() async {
15-
client.on("UserAuthSignIn", (Message message) async {
24+
client.on("UserAuthSignIn", (Message message) async {
1625
expect("UserAuthSignIn", equals(message.event));
1726
expect('{"status": "OK"}', equals(message.message));
18-
});
19-
await client.emit("UserAuthSignIn", {});
27+
});
28+
client.onDisconnection(() async {
29+
});
30+
await client.emit("UserAuthSignIn", {});
31+
await client.emit("UserAuthSignIn", {});
2032
});
2133
await client.connect();
2234
```

lib/flutter_socket_client.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ class FlutterSocketClient extends Client implements SocketClient {
4040
return this;
4141
}
4242

43-
Future initialize() async {
43+
Future<SocketClient> initialize() async {
4444
_client = await WebSocket.connect(this.url);
45+
return this;
4546
}
4647

4748
@override

lib/src/client.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ abstract class Client {
5656
}
5757

5858
Future<String> transport(String message) async {
59-
print('Transport : ' + message);
6059
if (message.toString().startsWith('connected')) {
6160
return socket != null && socket.isConnected() ? '1' : '0';
6261
}
6362
if (message.toString().startsWith('ws://')) {
64-
socket = await getSocket().connect();
63+
socket = await getSocket().initialize();
6564
if (socket.isConnected()) {
6665
// Create new stream for new connection
6766
responseStream = new StreamController.broadcast();
@@ -111,21 +110,19 @@ abstract class Client {
111110
}
112111
var callback = eventListeners[message.event];
113112
if (callback == null) {
114-
print('Can not process event ' + message.event);
113+
print('Can not handle event : ' + message.event);
115114
return 0;
116115
}
117116
await callback(message);
118117
return 0;
119118
}
120119

121120
Future<Message> emit(String eventName, dynamic data) async {
122-
print('Emit');
123121
final isConnected = await transport('connected');
124122
var connected = '1';
125123
if (isConnected.toString() != '1') {
126124
connected = await transport(this.url);
127125
}
128-
print('Connected ' + connected.toString());
129126
if (connected.toString() == '1') {
130127
var requestMessage = new Message(eventName, data);
131128
String responseText = null;

lib/src/socket_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ library socket.client;
2727
import 'dart:async';
2828

2929
abstract class SocketClient {
30-
Future initialize();
30+
Future<SocketClient> initialize();
3131
Future connect();
3232
bool isConnected();
3333
Future add(String message);

lib/web_socket_client.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class WebSocketClient extends Client.Client implements SocketClient {
3838
return this;
3939
}
4040

41-
Future initialize() async {
41+
Future<SocketClient> initialize() async {
4242
_client = new WebSocket(this.url);
43+
return this;
4344
}
4445

4546
@override

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ environment:
88

99
dev_dependencies:
1010
test: ^0.12.0
11+
browser_unittest: ^0.4.0

test/socket_flutter_test.dart renamed to test/flutter_socket_client_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import 'package:socket_client_dart/src/client.dart';
2727
import 'package:socket_client_dart/flutter_socket_client.dart';
2828

2929
void main() {
30-
test("FlutterClient:integration", () async {
30+
test("FlutterSocketClient:integration", () async {
3131
// Client connection is not allowed to retry
3232
// another connection is connected
3333
var client = new FlutterSocketClient('ws://localhost:3000');

test/web_socket_client_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2018 Food Tiny Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are met:
5+
//
6+
// Redistributions of source code must retain the above copyright
7+
// notice, this list of conditions and the following disclaimer.
8+
// Redistributions in binary form must reproduce the above
9+
// copyright notice, this list of conditions and the following disclaimer
10+
// in the documentation and/or other materials provided with the
11+
// distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
25+
import 'package:unittest/unittest.dart';
26+
import 'package:unittest/html_config.dart';
27+
import 'package:socket_client_dart/src/client.dart';
28+
import 'package:socket_client_dart/web_socket_client.dart';
29+
30+
void main() {
31+
useHtmlConfiguration();
32+
test("WebSocketClient:integration", () async {
33+
// Client connection is not allowed to retry
34+
// another connection is connected
35+
var client = new WebSocketClient('ws://localhost:3000');
36+
client.authenticate({
37+
"scope": "Mobile",
38+
"version": "v1_0",
39+
"platform": "Android",
40+
"device": "Sony Xperia Z",
41+
"osVersion": "1234",
42+
"ipAddress": "127.0.0.1"
43+
});
44+
client.onConnection(() async {
45+
client.on("UserAuthSignIn", (Message message) async {
46+
expect("UserAuthSignIn", equals(message.event));
47+
expect('{"status": "OK"}', equals(message.message));
48+
});
49+
client.onDisconnection(() async {
50+
});
51+
await client.emit("UserAuthSignIn", {});
52+
await client.emit("UserAuthSignIn", {});
53+
});
54+
await client.connect();
55+
});
56+
}

0 commit comments

Comments
 (0)