Skip to content

Commit

Permalink
Add an OPTIONS request type (#215)
Browse files Browse the repository at this point in the history
* Add an OPTIONS request type

* Null safety fix

Co-authored-by: Ivan Terekhin <[email protected]>
  • Loading branch information
safarmer and JEuler authored Mar 22, 2021
1 parent 6309368 commit 6db5784
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions chopper/lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ class Head extends Method {
);
}

@immutable
class Options extends Method {
const Options({
bool optionalBody = true,
String path = '',
Map<String, String> 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<Request> Function(Request request);

Expand Down
17 changes: 17 additions & 0 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,23 @@ class ChopperClient {
),
);

/// Makes a HTTP OPTIONS request using the [send] function.
Future<Response<BodyType>> options<BodyType, InnerType>(
String url, {
Map<String, String> headers = const {},
Map<String, dynamic>? parameters,
String? baseUrl,
}) =>
send<BodyType, InnerType>(
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`,
Expand Down
1 change: 1 addition & 0 deletions chopper/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
25 changes: 25 additions & 0 deletions chopper/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
7 changes: 7 additions & 0 deletions chopper/test/test_service.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions chopper/test/test_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ abstract class HttpTestService extends ChopperService {
@Head(path: 'head')
Future<Response> headTest();

@Options(path: 'options')
Future<Response> optionsTest();

@Get(path: 'get')
Future<Response<Stream<List<int>>>> getStreamTest();

Expand Down
1 change: 1 addition & 0 deletions chopper_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ class ChopperGenerator extends GeneratorForAnnotation<chopper.ChopperApi> {
chopper.Patch,
chopper.Method,
chopper.Head,
chopper.Options,
];

DartType? _genericOf(DartType? type) {
Expand Down

0 comments on commit 6db5784

Please sign in to comment.