Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an OPTIONS request type #215

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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