Skip to content

Commit

Permalink
adds head request method, test methods
Browse files Browse the repository at this point in the history
  • Loading branch information
urusai88 committed Dec 30, 2023
1 parent d0b1c49 commit ee64743
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/src/client_x.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ extension TioClientX<E> on Tio<E> {
onSendProgress: onSendProgress,
);

TioRequestProxy<T, E> head<T>(
String path, {
Object? data,
JSON? queryParameters,
Options? options,
CancelToken? cancelToken,
}) =>
TioRequestProxy(
this,
path,
method: 'HEAD',
data: data,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
);

TioRequestProxy<T, E> patch<T>(
String path, {
Object? data,
Expand Down
16 changes: 16 additions & 0 deletions test/_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ Future<void> _serverListener(HttpRequest req) async {
final segments = req.uri.pathSegments.map((e) => e.toLowerCase()).toList();

switch (segments) {
case ['method']:
final method = switch (req.method.toUpperCase().trim()) {
'GET' => 'GET',
'POST' => 'POST',
'PUT' => 'PUT',
'HEAD' => 'HEAD',
'PATCH' => 'PATCH',
'DELETE' => 'DELETE',
_ => 'UNKNOWN METHOD ${req.method}',
};
if (req.method.toUpperCase().trim() == 'GET') {
resp.headers.set('X-GET', 'TRUE');
}
resp.write(method);
case ['long_job']:
await Future<void>.delayed(const Duration(seconds: 10));
case ['posts']:
Expand Down Expand Up @@ -99,6 +113,8 @@ Future<void> _serverListener(HttpRequest req) async {
case ['404_json']:
resp.statusCode = errorCode;
_writeJson(resp, errorJson, sendStatusCode: false);
case _:
resp.write('UNKNOWN SEGMENTS $segments');
}

await resp.close();
Expand Down
18 changes: 18 additions & 0 deletions test/_test_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import '_typedefs.dart';
class TestTioService extends TioService<MyResponseError> {
const TestTioService({required super.client});

Future<MyResponse<String>> methodGet() =>
client.get<String>('/method').string();

Future<MyResponse<String>> methodPost() =>
client.post<String>('/method').string();

Future<MyResponse<String>> methodPut() =>
client.put<String>('/method').string();

Future<MyResponse<String>> methodHead() =>
client.head<String>('/method').string();

Future<MyResponse<String>> methodPatch() =>
client.patch<String>('/method').string();

Future<MyResponse<String>> methodDelete() =>
client.delete<String>('/method').string();

Future<MyResponse<void>> long(CancelToken cancelToken) =>
client.get<void>('/long_job', cancelToken: cancelToken).empty();

Expand Down
36 changes: 36 additions & 0 deletions test/methods_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:test/test.dart';

import '_internal.dart';

void main() {
upAndDownTest();

void testMethod(String method, Future<MyResponse<String>> Function() fn) {
test(
'method $method',
() async => expect(
fn(),
completion(
isA<MySuccess<String>>().having((s) => s.result, 'result',
predicate((result) {
if (method == 'HEAD') {
return result == '';
}
return result == method;
})).having(
(s) => s.response?.requestOptions.method,
'response.requestOptions.method',
method,
),
),
),
);
}

testMethod('GET', testService.methodGet);
testMethod('POST', testService.methodPost);
testMethod('PUT', testService.methodPut);
testMethod('HEAD', testService.methodHead);
testMethod('PATCH', testService.methodPatch);
testMethod('DELETE', testService.methodDelete);
}

0 comments on commit ee64743

Please sign in to comment.