Skip to content

Commit 4d9f662

Browse files
Fix network logger tests
1 parent 4f291d2 commit 4d9f662

File tree

1 file changed

+105
-115
lines changed

1 file changed

+105
-115
lines changed

test/network_logger_test.dart

Lines changed: 105 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void main() {
2727
const String path = '/posts';
2828

2929
late InstabugCustomHttpClient instabugCustomHttpClient;
30+
late MockHttpClientRequest mockRequest;
31+
late MockHttpClientResponse mockResponse;
3032

3133
setUpAll(() async {
3234
const MethodChannel('instabug_flutter')
@@ -39,6 +41,11 @@ void main() {
3941
instabugCustomHttpClient = InstabugCustomHttpClient();
4042
instabugCustomHttpClient.client = MockHttpClient();
4143
instabugCustomHttpClient.logger = MockHttpClientLogger();
44+
45+
mockRequest = MockHttpClientRequest();
46+
mockResponse = MockHttpClientResponse();
47+
48+
when<dynamic>(mockRequest.close()).thenAnswer((_) async => mockResponse);
4249
});
4350

4451
tearDown(() async {
@@ -49,226 +56,209 @@ void main() {
4956
() async {
5057
when<dynamic>(
5158
(instabugCustomHttpClient.client as MockHttpClient).getUrl(any))
52-
.thenAnswer((_) async => MockHttpClientRequest());
59+
.thenAnswer((_) async => mockRequest);
5360

54-
final request = await instabugCustomHttpClient.getUrl(Uri.parse(url));
55-
final response = await request.close();
56-
expect(request, isInstanceOf<HttpClientRequest>());
57-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
58-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
59-
.called(1);
61+
await instabugCustomHttpClient.getUrl(Uri.parse(url));
62+
63+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
64+
verify(
65+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
6066
});
6167

6268
test('expect instabug custom http client GET to return request and log',
6369
() async {
6470
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
6571
.get(any, any, any))
66-
.thenAnswer((_) async => MockHttpClientRequest());
72+
.thenAnswer((_) async => mockRequest);
6773

68-
final request = await instabugCustomHttpClient.get(url, port, path);
69-
final response = await request.close();
70-
expect(request, isInstanceOf<HttpClientRequest>());
71-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
72-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
73-
.called(1);
74+
await instabugCustomHttpClient.get(url, port, path);
75+
76+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
77+
verify(
78+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
7479
});
7580

7681
test(
7782
'expect instabug custom http client DELETE URL to return request and log',
7883
() async {
7984
when<dynamic>(
8085
(instabugCustomHttpClient.client as MockHttpClient).deleteUrl(any))
81-
.thenAnswer((_) async => MockHttpClientRequest());
86+
.thenAnswer((_) async => mockRequest);
8287

83-
final request = await instabugCustomHttpClient.deleteUrl(Uri.parse(url));
84-
final response = await request.close();
85-
expect(request, isInstanceOf<HttpClientRequest>());
86-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
87-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
88-
.called(1);
88+
await instabugCustomHttpClient.deleteUrl(Uri.parse(url));
89+
90+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
91+
verify(
92+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
8993
});
9094

9195
test('expect instabug custom http client DELETE to return request and log',
9296
() async {
9397
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
9498
.delete(any, any, any))
95-
.thenAnswer((_) async => MockHttpClientRequest());
99+
.thenAnswer((_) async => mockRequest);
96100

97-
final request = await instabugCustomHttpClient.delete(url, port, path);
98-
final response = await request.close();
99-
expect(request, isInstanceOf<HttpClientRequest>());
100-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
101-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
102-
.called(1);
101+
await instabugCustomHttpClient.delete(url, port, path);
102+
103+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
104+
verify(
105+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
103106
});
104107

105108
test('expect instabug custom http client POST URL to return request and log',
106109
() async {
107110
when<dynamic>(
108111
(instabugCustomHttpClient.client as MockHttpClient).postUrl(any))
109-
.thenAnswer((_) async => MockHttpClientRequest());
112+
.thenAnswer((_) async => mockRequest);
110113

111-
final request = await instabugCustomHttpClient.postUrl(Uri.parse(url));
112-
final response = await request.close();
113-
expect(request, isInstanceOf<HttpClientRequest>());
114-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
115-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
116-
.called(1);
114+
await instabugCustomHttpClient.postUrl(Uri.parse(url));
115+
116+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
117+
verify(
118+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
117119
});
118120

119121
test('expect instabug custom http client POST to return request and log',
120122
() async {
121123
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
122124
.post(any, any, any))
123-
.thenAnswer((_) async => MockHttpClientRequest());
125+
.thenAnswer((_) async => mockRequest);
124126

125-
final request = await instabugCustomHttpClient.post(url, port, path);
126-
final response = await request.close();
127-
expect(request, isInstanceOf<HttpClientRequest>());
128-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
129-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
130-
.called(1);
127+
await instabugCustomHttpClient.post(url, port, path);
128+
129+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
130+
verify(
131+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
131132
});
132133

133134
test('expect instabug custom http client HEAD URL to return request and log',
134135
() async {
135136
when<dynamic>(
136137
(instabugCustomHttpClient.client as MockHttpClient).headUrl(any))
137-
.thenAnswer((_) async => MockHttpClientRequest());
138+
.thenAnswer((_) async => mockRequest);
138139

139-
final request = await instabugCustomHttpClient.headUrl(Uri.parse(url));
140-
final response = await request.close();
141-
expect(request, isInstanceOf<HttpClientRequest>());
142-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
143-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
144-
.called(1);
140+
await instabugCustomHttpClient.headUrl(Uri.parse(url));
141+
142+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
143+
verify(
144+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
145145
});
146146

147147
test('expect instabug custom http client HEAD to return request and log',
148148
() async {
149149
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
150150
.head(any, any, any))
151-
.thenAnswer((_) async => MockHttpClientRequest());
151+
.thenAnswer((_) async => mockRequest);
152152

153-
final request = await instabugCustomHttpClient.head(url, port, path);
154-
final response = await request.close();
155-
expect(request, isInstanceOf<HttpClientRequest>());
156-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
157-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
158-
.called(1);
153+
await instabugCustomHttpClient.head(url, port, path);
154+
155+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
156+
verify(
157+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
159158
});
160159

161160
test('expect instabug custom http client PATCH URL to return request and log',
162161
() async {
163162
when<dynamic>(
164163
(instabugCustomHttpClient.client as MockHttpClient).patchUrl(any))
165-
.thenAnswer((_) async => MockHttpClientRequest());
164+
.thenAnswer((_) async => mockRequest);
166165

167-
final request = await instabugCustomHttpClient.patchUrl(Uri.parse(url));
168-
final response = await request.close();
169-
expect(request, isInstanceOf<HttpClientRequest>());
170-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
171-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
172-
.called(1);
166+
await instabugCustomHttpClient.patchUrl(Uri.parse(url));
167+
168+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
169+
verify(
170+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
173171
});
174172

175173
test('expect instabug custom http client PATCH to return request and log',
176174
() async {
177175
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
178176
.patch(any, any, any))
179-
.thenAnswer((_) async => MockHttpClientRequest());
177+
.thenAnswer((_) async => mockRequest);
180178

181-
final request = await instabugCustomHttpClient.patch(url, port, path);
182-
final response = await request.close();
183-
expect(request, isInstanceOf<HttpClientRequest>());
184-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
185-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
186-
.called(1);
179+
await instabugCustomHttpClient.patch(url, port, path);
180+
181+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
182+
verify(
183+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
187184
});
188185

189186
test('expect instabug custom http client OPEN URL to return request and log',
190187
() async {
191188
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
192189
.openUrl(any, any))
193-
.thenAnswer((_) async => MockHttpClientRequest());
194-
195-
final request =
196-
await instabugCustomHttpClient.openUrl('GET', Uri.parse(url));
197-
final response = await request.close();
198-
expect(request, isInstanceOf<HttpClientRequest>());
199-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
200-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
201-
.called(1);
190+
.thenAnswer((_) async => mockRequest);
191+
192+
await instabugCustomHttpClient.openUrl('GET', Uri.parse(url));
193+
194+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
195+
verify(
196+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
202197
});
203198

204199
test('expect instabug custom http client OPEN to return request and log',
205200
() async {
206201
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
207202
.open(any, any, any, any))
208-
.thenAnswer((_) async => MockHttpClientRequest());
203+
.thenAnswer((_) async => mockRequest);
209204

210-
final request = await instabugCustomHttpClient.open('GET', url, port, path);
211-
final response = await request.close();
212-
expect(request, isInstanceOf<HttpClientRequest>());
213-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
214-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
215-
.called(1);
205+
await instabugCustomHttpClient.open('GET', url, port, path);
206+
207+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
208+
verify(
209+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
216210
});
217211

218212
test('expect instabug custom http client PUT URL to return request and log',
219213
() async {
220214
when<dynamic>(
221215
(instabugCustomHttpClient.client as MockHttpClient).putUrl(any))
222-
.thenAnswer((_) async => MockHttpClientRequest());
216+
.thenAnswer((_) async => mockRequest);
223217

224-
final request = await instabugCustomHttpClient.putUrl(Uri.parse(url));
225-
final response = await request.close();
226-
expect(request, isInstanceOf<HttpClientRequest>());
227-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
228-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
229-
.called(1);
218+
await instabugCustomHttpClient.putUrl(Uri.parse(url));
219+
220+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
221+
verify(
222+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
230223
});
231224

232225
test('expect instabug custom http client PUT to return request and log',
233226
() async {
234227
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
235228
.put(any, any, any))
236-
.thenAnswer((_) async => MockHttpClientRequest());
229+
.thenAnswer((_) async => mockRequest);
237230

238-
final request = await instabugCustomHttpClient.put(url, port, path);
239-
final response = await request.close();
240-
expect(request, isInstanceOf<HttpClientRequest>());
241-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
242-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
243-
.called(1);
231+
await instabugCustomHttpClient.put(url, port, path);
232+
233+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
234+
verify(
235+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
244236
});
245237

246238
test('expect instabug custom http client POST URL to return request and log',
247239
() async {
248240
when<dynamic>(
249241
(instabugCustomHttpClient.client as MockHttpClient).postUrl(any))
250-
.thenAnswer((_) async => MockHttpClientRequest());
242+
.thenAnswer((_) async => mockRequest);
251243

252-
final request = await instabugCustomHttpClient.postUrl(Uri.parse(url));
253-
final response = await request.close();
254-
expect(request, isInstanceOf<HttpClientRequest>());
255-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
256-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
257-
.called(1);
244+
await instabugCustomHttpClient.postUrl(Uri.parse(url));
245+
246+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
247+
verify(
248+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
258249
});
259250

260251
test('expect instabug custom http client POST to return request and log',
261252
() async {
262253
when<dynamic>((instabugCustomHttpClient.client as MockHttpClient)
263254
.post(any, any, any))
264-
.thenAnswer((_) async => MockHttpClientRequest());
255+
.thenAnswer((_) async => mockRequest);
265256

266-
final request = await instabugCustomHttpClient.post(url, port, path);
267-
final response = await request.close();
268-
expect(request, isInstanceOf<HttpClientRequest>());
269-
verify(instabugCustomHttpClient.logger.onRequest(request)).called(1);
270-
verify(instabugCustomHttpClient.logger.onResponse(response, request))
271-
.called(1);
257+
await instabugCustomHttpClient.post(url, port, path);
258+
259+
verify(instabugCustomHttpClient.logger.onRequest(mockRequest));
260+
verify(
261+
instabugCustomHttpClient.logger.onResponse(mockResponse, mockRequest));
272262
});
273263

274264
test('expect instabug custom http client to get client autoUncompress',
@@ -411,7 +401,7 @@ void main() {
411401
test('Stress test on GET URL method', () async {
412402
when<dynamic>(
413403
(instabugCustomHttpClient.client as MockHttpClient).getUrl(any))
414-
.thenAnswer((_) async => MockHttpClientRequest());
404+
.thenAnswer((_) async => mockRequest);
415405

416406
for (int i = 0; i < 10000; i++) {
417407
await instabugCustomHttpClient.getUrl(Uri.parse(url));

0 commit comments

Comments
 (0)