From 6db5784f03b36ef483c0a3cea7b44905f20fe201 Mon Sep 17 00:00:00 2001 From: Shane Farmer Date: Tue, 23 Mar 2021 04:00:32 +1100 Subject: [PATCH] Add an OPTIONS request type (#215) * Add an OPTIONS request type * Null safety fix Co-authored-by: Ivan Terekhin --- chopper/lib/src/annotations.dart | 14 +++++++++++++ chopper/lib/src/base.dart | 17 ++++++++++++++++ chopper/lib/src/constants.dart | 1 + chopper/test/client_test.dart | 25 ++++++++++++++++++++++++ chopper/test/test_service.chopper.dart | 7 +++++++ chopper/test/test_service.dart | 3 +++ chopper_generator/lib/src/generator.dart | 1 + 7 files changed, 68 insertions(+) diff --git a/chopper/lib/src/annotations.dart b/chopper/lib/src/annotations.dart index 9d92ef9a..332b55de 100644 --- a/chopper/lib/src/annotations.dart +++ b/chopper/lib/src/annotations.dart @@ -262,6 +262,20 @@ class Head extends Method { ); } +@immutable +class Options extends Method { + const Options({ + bool optionalBody = true, + String path = '', + Map headers = const {}, + }) : super( + HttpMethod.Options, + optionalBody: optionalBody, + path: path, + headers: headers, + ); +} + /// A function that should convert the body of a [Request] to the HTTP representation. typedef ConvertRequest = FutureOr Function(Request request); diff --git a/chopper/lib/src/base.dart b/chopper/lib/src/base.dart index ca8c11c5..9733846a 100644 --- a/chopper/lib/src/base.dart +++ b/chopper/lib/src/base.dart @@ -454,6 +454,23 @@ class ChopperClient { ), ); + /// Makes a HTTP OPTIONS request using the [send] function. + Future> options( + String url, { + Map headers = const {}, + Map? parameters, + String? baseUrl, + }) => + send( + Request( + HttpMethod.Options, + url, + baseUrl ?? this.baseUrl, + headers: headers, + parameters: parameters, + ), + ); + /// Disposes this [ChopperClient] to clean up memory. /// /// **Warning**: If a custom [http.Client] was provided while creating this `ChopperClient`, diff --git a/chopper/lib/src/constants.dart b/chopper/lib/src/constants.dart index aca08c45..21f388aa 100644 --- a/chopper/lib/src/constants.dart +++ b/chopper/lib/src/constants.dart @@ -12,4 +12,5 @@ class HttpMethod { static const String Delete = 'DELETE'; static const String Patch = 'PATCH'; static const String Head = 'HEAD'; + static const String Options = 'OPTIONS'; } diff --git a/chopper/test/client_test.dart b/chopper/test/client_test.dart index 12e04900..81c6d0c8 100644 --- a/chopper/test/client_test.dart +++ b/chopper/test/client_test.dart @@ -149,6 +149,31 @@ void main() { expect(response.body, equals('delete response')); expect(response.statusCode, equals(200)); + httpClient.close(); + }); + test('OPTIONS', () async { + final httpClient = MockClient((request) async { + expect( + request.url.toString(), + equals('$baseUrl/test/get?key=val'), + ); + expect(request.method, equals('OPTIONS')); + expect(request.headers['foo'], equals('bar')); + expect(request.headers['int'], equals('42')); + + return http.Response('get response', 200); + }); + + final chopper = buildClient(httpClient); + final response = await chopper.options( + '/test/get', + headers: {'int': '42'}, + parameters: {'key': 'val'}, + ); + + expect(response.body, equals('get response')); + expect(response.statusCode, equals(200)); + httpClient.close(); }); }); diff --git a/chopper/test/test_service.chopper.dart b/chopper/test/test_service.chopper.dart index e37dc7f1..f4bbc473 100644 --- a/chopper/test/test_service.chopper.dart +++ b/chopper/test/test_service.chopper.dart @@ -31,6 +31,13 @@ class _$HttpTestService extends HttpTestService { return client.send($request); } + @override + Future> optionsTest() { + final $url = '/test/options'; + final $request = Request('OPTIONS', $url, client.baseUrl); + return client.send($request); + } + @override Future>>> getStreamTest() { final $url = '/test/get'; diff --git a/chopper/test/test_service.dart b/chopper/test/test_service.dart index 9dacedb8..1fcd9940 100644 --- a/chopper/test/test_service.dart +++ b/chopper/test/test_service.dart @@ -21,6 +21,9 @@ abstract class HttpTestService extends ChopperService { @Head(path: 'head') Future headTest(); + @Options(path: 'options') + Future optionsTest(); + @Get(path: 'get') Future>>> getStreamTest(); diff --git a/chopper_generator/lib/src/generator.dart b/chopper_generator/lib/src/generator.dart index 4efacb35..1ce46a94 100644 --- a/chopper_generator/lib/src/generator.dart +++ b/chopper_generator/lib/src/generator.dart @@ -338,6 +338,7 @@ class ChopperGenerator extends GeneratorForAnnotation { chopper.Patch, chopper.Method, chopper.Head, + chopper.Options, ]; DartType? _genericOf(DartType? type) {