-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(neon_http_client): split into separate interceptor client pa…
…ckage Signed-off-by: Nikolas Rimikis <[email protected]>
- Loading branch information
Showing
36 changed files
with
430 additions
and
120 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 @@ | ||
../../assets/AGPL-3.0.txt |
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,6 @@ | ||
include: package:neon_lints/dart.yaml | ||
|
||
custom_lint: | ||
rules: | ||
- avoid_exports: false | ||
- avoid_dart_io: false |
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,10 @@ | ||
platforms: | ||
- vm | ||
- chrome | ||
|
||
define_platforms: | ||
chromium: | ||
name: Chromium | ||
extends: chrome | ||
settings: | ||
executable: chromium |
5 changes: 5 additions & 0 deletions
5
packages/interceptor_http_client/lib/interceptor_http_client.dart
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,5 @@ | ||
/// A http client that allows to register interceptors for requests and responses. | ||
library; | ||
|
||
export 'src/interceptor_http_client.dart'; | ||
export 'src/interceptors/interceptors.dart'; |
95 changes: 95 additions & 0 deletions
95
packages/interceptor_http_client/lib/src/interceptor_http_client.dart
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,95 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:built_collection/built_collection.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:interceptor_http_client/src/interceptors/interceptors.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
/// An exception caused by an error in a [InterceptorHttpClient]. | ||
abstract class InterceptorHttpClientException extends http.ClientException { | ||
/// An exception caused by an error in a [InterceptorHttpClient]. | ||
InterceptorHttpClientException(super.message, [super.uri]); | ||
} | ||
|
||
/// An exception caused by a [HttpInterceptor]. | ||
final class InterceptionException extends InterceptorHttpClientException { | ||
/// Creates a new interceptor failure exception. | ||
InterceptionException(super.message, [super.uri]); | ||
} | ||
|
||
/// A http client for intercepting requests and responses. | ||
class InterceptorHttpClient with http.BaseClient { | ||
/// Creates a new interceptor http client. | ||
const InterceptorHttpClient({ | ||
required http.Client baseClient, | ||
required this.interceptors, | ||
}) : _baseClient = baseClient; | ||
|
||
/// The underlying HTTP client. | ||
final http.Client _baseClient; | ||
|
||
/// The list of enabled interceptors. | ||
@visibleForTesting | ||
@protected | ||
final BuiltList<HttpInterceptor> interceptors; | ||
|
||
@override | ||
Future<http.StreamedResponse> send(http.BaseRequest request) async { | ||
var interceptedRequest = request; | ||
for (final interceptor in interceptors) { | ||
if (interceptor.shouldInterceptRequest(interceptedRequest)) { | ||
try { | ||
interceptedRequest = await interceptor.interceptRequest( | ||
request: interceptedRequest, | ||
); | ||
} catch (error, stackTrace) { | ||
if (error is http.ClientException) { | ||
rethrow; | ||
} | ||
|
||
Error.throwWithStackTrace( | ||
InterceptionException('Failed to intercept request', request.url), | ||
stackTrace, | ||
); | ||
} | ||
} | ||
} | ||
|
||
var interceptedResponse = await _baseClient.send(interceptedRequest); | ||
|
||
Uri url; | ||
if (interceptedResponse case http.BaseResponseWithUrl(url: final responseUrl)) { | ||
url = responseUrl; | ||
} else { | ||
url = interceptedRequest.url; | ||
} | ||
|
||
for (final interceptor in interceptors) { | ||
if (interceptor.shouldInterceptResponse(interceptedResponse)) { | ||
try { | ||
interceptedResponse = await interceptor.interceptResponse( | ||
response: interceptedResponse, | ||
url: url, | ||
); | ||
} catch (error, stackTrace) { | ||
if (error is http.ClientException) { | ||
rethrow; | ||
} | ||
Error.throwWithStackTrace( | ||
InterceptionException('Failed to intercept response', request.url), | ||
stackTrace, | ||
); | ||
} | ||
} | ||
} | ||
|
||
return interceptedResponse; | ||
} | ||
|
||
@override | ||
void close() { | ||
_baseClient.close(); | ||
|
||
super.close(); | ||
} | ||
} |
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
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
2 changes: 2 additions & 0 deletions
2
packages/interceptor_http_client/lib/src/interceptors/interceptors.dart
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,2 @@ | ||
export 'cookie_interceptor.dart'; | ||
export 'http_interceptor.dart'; |
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,29 @@ | ||
name: interceptor_http_client | ||
description: A http client with request and response interceptors. | ||
version: 0.1.0 | ||
publish_to: none | ||
|
||
environment: | ||
sdk: ^3.0.0 | ||
|
||
dependencies: | ||
built_collection: ^5.0.0 | ||
cookie_store: | ||
git: | ||
url: https://github.com/nextcloud/neon | ||
path: packages/cookie_store | ||
http: ^1.0.0 | ||
meta: ^1.0.0 | ||
|
||
dev_dependencies: | ||
http_client_conformance_tests: | ||
git: | ||
url: https://github.com/dart-lang/http | ||
path: pkgs/http_client_conformance_tests | ||
ref: 76512c4cbf987361421030349fd1946e63e33359 | ||
mocktail: ^1.0.4 | ||
neon_lints: | ||
git: | ||
url: https://github.com/nextcloud/neon | ||
path: packages/neon_lints | ||
test: ^1.25.8 |
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,6 @@ | ||
# melos_managed_dependency_overrides: cookie_store,dynamite_runtime,neon_lints,nextcloud | ||
dependency_overrides: | ||
cookie_store: | ||
path: ../cookie_store | ||
neon_lints: | ||
path: ../neon_lints |
44 changes: 44 additions & 0 deletions
44
packages/interceptor_http_client/test/client_conformance_test.dart
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,44 @@ | ||
import 'package:built_collection/built_collection.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:http_client_conformance_tests/http_client_conformance_tests.dart'; | ||
import 'package:interceptor_http_client/src/interceptor_http_client.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group( | ||
'Interceptor Client VM conformance test', | ||
() { | ||
testAll( | ||
() => InterceptorHttpClient( | ||
baseClient: http.Client(), | ||
interceptors: BuiltList(), | ||
), | ||
canReceiveSetCookieHeaders: true, | ||
canSendCookieHeaders: true, | ||
); | ||
}, | ||
onPlatform: const { | ||
'browser': [Skip()], | ||
}, | ||
); | ||
|
||
group( | ||
'Interceptor Client browser conformance test', | ||
() { | ||
testAll( | ||
() => InterceptorHttpClient( | ||
baseClient: http.Client(), | ||
interceptors: BuiltList(), | ||
), | ||
redirectAlwaysAllowed: true, | ||
canStreamRequestBody: false, | ||
canStreamResponseBody: false, | ||
canWorkInIsolates: false, | ||
supportsMultipartRequest: false, | ||
); | ||
}, | ||
onPlatform: const { | ||
'dart-vm': [Skip()], | ||
}, | ||
); | ||
} |
133 changes: 133 additions & 0 deletions
133
packages/interceptor_http_client/test/interceptor_http_client_test.dart
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,133 @@ | ||
import 'package:built_collection/built_collection.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:http/testing.dart'; | ||
import 'package:interceptor_http_client/interceptor_http_client.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class _MockInterceptor extends Mock implements HttpInterceptor {} | ||
|
||
class _FakeUri extends Fake implements Uri {} | ||
|
||
void main() { | ||
final uri = Uri.parse('http://example.com'); | ||
Request fakeRequest() { | ||
return Request('PUT', uri); | ||
} | ||
|
||
StreamedResponse fakeResponse() { | ||
return StreamedResponse(const Stream.empty(), 200); | ||
} | ||
|
||
final mockedClient = MockClient((request) async { | ||
return Response.fromStream(fakeResponse()); | ||
}); | ||
late InterceptorHttpClient client; | ||
|
||
setUpAll(() { | ||
registerFallbackValue(_FakeUri()); | ||
registerFallbackValue(fakeResponse()); | ||
registerFallbackValue(fakeRequest()); | ||
}); | ||
|
||
tearDown(() { | ||
client.close(); | ||
}); | ||
|
||
group(InterceptorHttpClient, () { | ||
group('interceptors', () { | ||
late HttpInterceptor interceptor; | ||
|
||
setUp(() { | ||
interceptor = _MockInterceptor(); | ||
|
||
client = InterceptorHttpClient( | ||
baseClient: mockedClient, | ||
interceptors: BuiltList([interceptor]), | ||
); | ||
}); | ||
|
||
test('does not intercept', () async { | ||
when(() => interceptor.shouldInterceptRequest(any())).thenReturn(false); | ||
when(() => interceptor.shouldInterceptResponse(any())).thenReturn(false); | ||
|
||
final request = Request('GET', uri); | ||
await client.send(request); | ||
|
||
verifyNever( | ||
() => interceptor.interceptRequest( | ||
request: any(named: 'request'), | ||
), | ||
); | ||
verifyNever( | ||
() => interceptor.interceptResponse( | ||
response: any(named: 'response'), | ||
url: any(named: 'url'), | ||
), | ||
); | ||
}); | ||
|
||
test('does intercept', () async { | ||
when(() => interceptor.shouldInterceptRequest(any())).thenReturn(true); | ||
when(() => interceptor.shouldInterceptResponse(any())).thenReturn(true); | ||
when( | ||
() => interceptor.interceptRequest(request: any(named: 'request')), | ||
).thenReturn(fakeRequest()); | ||
when( | ||
() => interceptor.interceptResponse(response: any(named: 'response'), url: any(named: 'url')), | ||
).thenReturn(fakeResponse()); | ||
|
||
final request = Request('GET', uri); | ||
await client.send(request); | ||
|
||
verify( | ||
() => interceptor.interceptRequest( | ||
request: any(named: 'request', that: equals(request)), | ||
), | ||
).called(1); | ||
verify( | ||
() => interceptor.interceptResponse( | ||
response: any(named: 'response'), | ||
url: any(named: 'url', that: equals(uri)), | ||
), | ||
).called(1); | ||
}); | ||
|
||
test('rethrows errors as InterceptionFailure', () async { | ||
when(() => interceptor.shouldInterceptRequest(any())).thenReturn(true); | ||
when( | ||
() => interceptor.interceptRequest(request: any(named: 'request')), | ||
).thenThrow(StateError('message')); | ||
|
||
expect( | ||
client.get(uri), | ||
throwsA( | ||
isA<InterceptionException>().having( | ||
(e) => e.uri, | ||
'uri', | ||
uri, | ||
), | ||
), | ||
); | ||
|
||
when(() => interceptor.shouldInterceptRequest(any())).thenReturn(true); | ||
when(() => interceptor.interceptRequest(request: any(named: 'request'))).thenReturn(fakeRequest()); | ||
when(() => interceptor.shouldInterceptResponse(any())).thenReturn(true); | ||
when( | ||
() => interceptor.interceptResponse(response: any(named: 'response'), url: any(named: 'url')), | ||
).thenThrow(StateError('message')); | ||
|
||
expect( | ||
client.get(uri), | ||
throwsA( | ||
isA<InterceptionException>().having( | ||
(e) => e.uri, | ||
'uri', | ||
uri, | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
}); | ||
} |
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
Oops, something went wrong.