Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
28bedb10
Unverified
Commit
28bedb10
authored
Aug 20, 2019
by
Zachary Anderson
Committed by
GitHub
Aug 20, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[flutter_tool] Move http request close under try-catch (#38894)
parent
c20113e2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
22 deletions
+83
-22
net.dart
packages/flutter_tools/lib/src/base/net.dart
+4
-1
net_test.dart
packages/flutter_tools/test/general.shard/base/net_test.dart
+79
-21
No files found.
packages/flutter_tools/lib/src/base/net.dart
View file @
28bedb10
...
...
@@ -48,12 +48,14 @@ Future<List<int>> _attempt(Uri url, { bool onlyHeaders = false }) async {
httpClient
=
HttpClient
();
}
HttpClientRequest
request
;
HttpClientResponse
response
;
try
{
if
(
onlyHeaders
)
{
request
=
await
httpClient
.
headUrl
(
url
);
}
else
{
request
=
await
httpClient
.
getUrl
(
url
);
}
response
=
await
request
.
close
();
}
on
ArgumentError
catch
(
error
)
{
final
String
overrideUrl
=
platform
.
environment
[
'FLUTTER_STORAGE_BASE_URL'
];
if
(
overrideUrl
!=
null
&&
url
.
toString
().
contains
(
overrideUrl
))
{
...
...
@@ -82,7 +84,8 @@ Future<List<int>> _attempt(Uri url, { bool onlyHeaders = false }) async {
printTrace
(
'Download error:
$error
'
);
return
null
;
}
final
HttpClientResponse
response
=
await
request
.
close
();
assert
(
response
!=
null
);
// If we're making a HEAD request, we're only checking to see if the URL is
// valid.
if
(
onlyHeaders
)
{
...
...
packages/flutter_tools/test/general.shard/base/net_test.dart
View file @
28bedb10
...
...
@@ -34,7 +34,7 @@ void main() {
expect
(
testLogger
.
errorText
,
isEmpty
);
expect
(
error
,
isNull
);
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClient
(
500
),
HttpClientFactory:
()
=>
()
=>
Fake
HttpClient
(
500
),
});
testUsingContext
(
'retry from network error'
,
()
async
{
...
...
@@ -57,7 +57,7 @@ void main() {
expect
(
testLogger
.
errorText
,
isEmpty
);
expect
(
error
,
isNull
);
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClient
(
200
),
HttpClientFactory:
()
=>
()
=>
Fake
HttpClient
(
200
),
});
testUsingContext
(
'retry from SocketException'
,
()
async
{
...
...
@@ -81,7 +81,7 @@ void main() {
expect
(
error
,
isNull
);
expect
(
testLogger
.
traceText
,
contains
(
'Download error: SocketException'
));
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClientThrowing
(
HttpClientFactory:
()
=>
()
=>
Fake
HttpClientThrowing
(
const
io
.
SocketException
(
'test exception handling'
),
),
});
...
...
@@ -101,7 +101,7 @@ void main() {
expect
(
error
,
startsWith
(
'test failed'
));
expect
(
testLogger
.
traceText
,
contains
(
'HandshakeException'
));
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClientThrowing
(
HttpClientFactory:
()
=>
()
=>
Fake
HttpClientThrowing
(
const
io
.
HandshakeException
(
'test exception handling'
),
),
});
...
...
@@ -122,7 +122,7 @@ void main() {
expect
(
testLogger
.
errorText
,
contains
(
'Invalid argument'
));
expect
(
error
,
contains
(
'FLUTTER_STORAGE_BASE_URL'
));
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClientThrowing
(
HttpClientFactory:
()
=>
()
=>
Fake
HttpClientThrowing
(
ArgumentError
(
'test exception handling'
),
),
Platform:
()
=>
FakePlatform
.
fromPlatform
(
const
LocalPlatform
())
...
...
@@ -152,7 +152,33 @@ void main() {
expect
(
error
,
isNull
);
expect
(
testLogger
.
traceText
,
contains
(
'Download error: HttpException'
));
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
MockHttpClientThrowing
(
HttpClientFactory:
()
=>
()
=>
FakeHttpClientThrowing
(
const
io
.
HttpException
(
'test exception handling'
),
),
});
testUsingContext
(
'retry from HttpException when request throws'
,
()
async
{
String
error
;
FakeAsync
().
run
((
FakeAsync
time
)
{
fetchUrl
(
Uri
.
parse
(
'http://example.invalid/'
)).
then
((
List
<
int
>
value
)
{
error
=
'test completed unexpectedly'
;
},
onError:
(
dynamic
exception
)
{
error
=
'test failed unexpectedly:
$exception
'
;
});
expect
(
testLogger
.
statusText
,
''
);
time
.
elapse
(
const
Duration
(
milliseconds:
10000
));
expect
(
testLogger
.
statusText
,
'Download failed -- attempting retry 1 in 1 second...
\n
'
'Download failed -- attempting retry 2 in 2 seconds...
\n
'
'Download failed -- attempting retry 3 in 4 seconds...
\n
'
'Download failed -- attempting retry 4 in 8 seconds...
\n
'
,
);
});
expect
(
testLogger
.
errorText
,
isEmpty
);
expect
(
error
,
isNull
);
expect
(
testLogger
.
traceText
,
contains
(
'Download error: HttpException'
));
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
FakeHttpClientThrowingRequest
(
const
io
.
HttpException
(
'test exception handling'
),
),
});
...
...
@@ -178,7 +204,7 @@ void main() {
expect
(
error
,
isNull
);
expect
(
actualResult
,
isNull
);
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClient
(
500
),
HttpClientFactory:
()
=>
()
=>
Fake
HttpClient
(
500
),
});
testUsingContext
(
'remote file non-existant'
,
()
async
{
...
...
@@ -186,7 +212,7 @@ void main() {
final
bool
result
=
await
doesRemoteFileExist
(
invalid
);
expect
(
result
,
false
);
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClient
(
404
),
HttpClientFactory:
()
=>
()
=>
Fake
HttpClient
(
404
),
});
testUsingContext
(
'remote file server error'
,
()
async
{
...
...
@@ -194,7 +220,7 @@ void main() {
final
bool
result
=
await
doesRemoteFileExist
(
valid
);
expect
(
result
,
false
);
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClient
(
500
),
HttpClientFactory:
()
=>
()
=>
Fake
HttpClient
(
500
),
});
testUsingContext
(
'remote file exists'
,
()
async
{
...
...
@@ -202,12 +228,12 @@ void main() {
final
bool
result
=
await
doesRemoteFileExist
(
valid
);
expect
(
result
,
true
);
},
overrides:
<
Type
,
Generator
>{
HttpClientFactory:
()
=>
()
=>
Mock
HttpClient
(
200
),
HttpClientFactory:
()
=>
()
=>
Fake
HttpClient
(
200
),
});
}
class
Mock
HttpClientThrowing
implements
io
.
HttpClient
{
Mock
HttpClientThrowing
(
this
.
exception
);
class
Fake
HttpClientThrowing
implements
io
.
HttpClient
{
Fake
HttpClientThrowing
(
this
.
exception
);
final
Object
exception
;
...
...
@@ -222,19 +248,19 @@ class MockHttpClientThrowing implements io.HttpClient {
}
}
class
Mock
HttpClient
implements
io
.
HttpClient
{
Mock
HttpClient
(
this
.
statusCode
);
class
Fake
HttpClient
implements
io
.
HttpClient
{
Fake
HttpClient
(
this
.
statusCode
);
final
int
statusCode
;
@override
Future
<
io
.
HttpClientRequest
>
getUrl
(
Uri
url
)
async
{
return
Mock
HttpClientRequest
(
statusCode
);
return
Fake
HttpClientRequest
(
statusCode
);
}
@override
Future
<
io
.
HttpClientRequest
>
headUrl
(
Uri
url
)
async
{
return
Mock
HttpClientRequest
(
statusCode
);
return
Fake
HttpClientRequest
(
statusCode
);
}
@override
...
...
@@ -243,14 +269,46 @@ class MockHttpClient implements io.HttpClient {
}
}
class
MockHttpClientRequest
implements
io
.
HttpClientRequest
{
MockHttpClientRequest
(
this
.
statusCode
);
class
FakeHttpClientThrowingRequest
implements
io
.
HttpClient
{
FakeHttpClientThrowingRequest
(
this
.
exception
);
final
Object
exception
;
@override
Future
<
io
.
HttpClientRequest
>
getUrl
(
Uri
url
)
async
{
return
FakeHttpClientRequestThrowing
(
exception
);
}
@override
dynamic
noSuchMethod
(
Invocation
invocation
)
{
throw
'io.HttpClient -
$invocation
'
;
}
}
class
FakeHttpClientRequest
implements
io
.
HttpClientRequest
{
FakeHttpClientRequest
(
this
.
statusCode
);
final
int
statusCode
;
@override
Future
<
io
.
HttpClientResponse
>
close
()
async
{
return
MockHttpClientResponse
(
statusCode
);
return
FakeHttpClientResponse
(
statusCode
);
}
@override
dynamic
noSuchMethod
(
Invocation
invocation
)
{
throw
'io.HttpClientRequest -
$invocation
'
;
}
}
class
FakeHttpClientRequestThrowing
implements
io
.
HttpClientRequest
{
FakeHttpClientRequestThrowing
(
this
.
exception
);
final
Object
exception
;
@override
Future
<
io
.
HttpClientResponse
>
close
()
async
{
throw
exception
;
}
@override
...
...
@@ -259,8 +317,8 @@ class MockHttpClientRequest implements io.HttpClientRequest {
}
}
class
Mock
HttpClientResponse
implements
io
.
HttpClientResponse
{
Mock
HttpClientResponse
(
this
.
statusCode
);
class
Fake
HttpClientResponse
implements
io
.
HttpClientResponse
{
Fake
HttpClientResponse
(
this
.
statusCode
);
@override
final
int
statusCode
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment