From da1c25ba78c652995886db7171cdfd36fb9c3433 Mon Sep 17 00:00:00 2001 From: Martin Alejandro Escobar Espinel Date: Tue, 29 Aug 2023 14:58:37 -0500 Subject: [PATCH] adding tests for top level variables --- chopper/test/base_test.dart | 141 ++++ chopper/test/ensure_build_test.dart | 20 +- .../test/test_service_variable.chopper.dart | 641 ++++++++++++++++++ chopper/test/test_service_variable.dart | 220 ++++++ chopper_generator/test/ensure_build_test.dart | 21 +- .../test/test_service_variable.chopper.dart | 641 ++++++++++++++++++ .../test/test_service_variable.dart | 220 ++++++ 7 files changed, 1892 insertions(+), 12 deletions(-) create mode 100644 chopper/test/test_service_variable.chopper.dart create mode 100644 chopper/test/test_service_variable.dart create mode 100644 chopper_generator/test/test_service_variable.chopper.dart create mode 100644 chopper_generator/test/test_service_variable.dart diff --git a/chopper/test/base_test.dart b/chopper/test/base_test.dart index 3e6a6f01..dee04012 100644 --- a/chopper/test/base_test.dart +++ b/chopper/test/base_test.dart @@ -9,8 +9,10 @@ import 'package:http/testing.dart'; import 'package:test/test.dart'; import 'test_service.dart'; +import 'test_service_variable.dart'; final baseUrl = Uri.parse('http://localhost:8000'); +const String testEnv = 'https://localhost:4000'; void main() { ChopperClient buildClient([ @@ -22,6 +24,7 @@ void main() { services: [ // the generated service HttpTestService.create(), + HttpTestServiceVariable.create(), ], client: httpClient, errorConverter: errorConverter, @@ -35,9 +38,12 @@ void main() { final chopper = buildClient(httpClient); final service = chopper.getService(); + final serviceVariable = chopper.getService(); expect(service, isNotNull); + expect(serviceVariable, isNotNull); expect(service, isA()); + expect(serviceVariable, isA()); }); test('get service errors', () async { @@ -86,6 +92,28 @@ void main() { httpClient.close(); }); + test('GET Variable', () async { + final httpClient = MockClient((request) async { + expect( + request.url.toString(), + equals('$testEnv/get/1234'), + ); + expect(request.method, equals('GET')); + + return http.Response('get response', 200); + }); + + final chopper = buildClient(httpClient); + final service = chopper.getService(); + + final response = await service.getTest('1234', dynamicHeader: ''); + + expect(response.body, equals('get response')); + expect(response.statusCode, equals(200)); + + httpClient.close(); + }); + test('GET stream', () async { final httpClient = MockClient.streaming((request, stream) async { expect( @@ -231,6 +259,29 @@ void main() { httpClient.close(); }); + test('POST Variable', () async { + final httpClient = MockClient((request) async { + expect( + request.url.toString(), + equals('$testEnv/post'), + ); + expect(request.method, equals('POST')); + expect(request.body, equals('post body')); + + return http.Response('post response', 200); + }); + + final chopper = buildClient(httpClient); + final service = chopper.getService(); + + final response = await service.postTest('post body'); + + expect(response.body, equals('post response')); + expect(response.statusCode, equals(200)); + + httpClient.close(); + }); + test('POST with streamed body', () async { final httpClient = MockClient((request) async { expect( @@ -282,6 +333,29 @@ void main() { httpClient.close(); }); + test('PUT Variable', () async { + final httpClient = MockClient((request) async { + expect( + request.url.toString(), + equals('$testEnv/put/1234'), + ); + expect(request.method, equals('PUT')); + expect(request.body, equals('put body')); + + return http.Response('put response', 200); + }); + + final chopper = buildClient(httpClient); + final service = chopper.getService(); + + final response = await service.putTest('1234', 'put body'); + + expect(response.body, equals('put response')); + expect(response.statusCode, equals(200)); + + httpClient.close(); + }); + test('PATCH', () async { final httpClient = MockClient((request) async { expect( @@ -305,6 +379,29 @@ void main() { httpClient.close(); }); + test('PATCH Variable', () async { + final httpClient = MockClient((request) async { + expect( + request.url.toString(), + equals('$testEnv/patch/1234'), + ); + expect(request.method, equals('PATCH')); + expect(request.body, equals('patch body')); + + return http.Response('patch response', 200); + }); + + final chopper = buildClient(httpClient); + final service = chopper.getService(); + + final response = await service.patchTest('1234', 'patch body'); + + expect(response.body, equals('patch response')); + expect(response.statusCode, equals(200)); + + httpClient.close(); + }); + test('DELETE', () async { final httpClient = MockClient((request) async { expect( @@ -327,6 +424,28 @@ void main() { httpClient.close(); }); + test('DELETE Variable', () async { + final httpClient = MockClient((request) async { + expect( + request.url.toString(), + equals('$testEnv/delete/1234'), + ); + expect(request.method, equals('DELETE')); + + return http.Response('delete response', 200); + }); + + final chopper = buildClient(httpClient); + final service = chopper.getService(); + + final response = await service.deleteTest('1234'); + + expect(response.body, equals('delete response')); + expect(response.statusCode, equals(200)); + + httpClient.close(); + }); + test('Head', () async { final httpClient = MockClient((request) async { expect( @@ -349,6 +468,28 @@ void main() { httpClient.close(); }); + test('Head Variable', () async { + final httpClient = MockClient((request) async { + expect( + request.url.toString(), + equals('$testEnv/head'), + ); + expect(request.method, equals('HEAD')); + + return http.Response('head response', 200); + }); + + final chopper = buildClient(httpClient); + final service = chopper.getService(); + + final response = await service.headTest(); + + expect(response.body, equals('head response')); + expect(response.statusCode, equals(200)); + + httpClient.close(); + }); + test('const headers', () async { final client = MockClient((http.Request req) async { expect(req.headers.containsKey('foo'), isTrue); diff --git a/chopper/test/ensure_build_test.dart b/chopper/test/ensure_build_test.dart index 73f46337..a599c5d2 100644 --- a/chopper/test/ensure_build_test.dart +++ b/chopper/test/ensure_build_test.dart @@ -6,11 +6,19 @@ import 'package:test/test.dart'; void main() { test( 'ensure_build', - () => expectBuildClean( - packageRelativeDirectory: 'chopper', - gitDiffPathArguments: [ - 'test/test_service.chopper.dart', - ], - ), + () { + expectBuildClean( + packageRelativeDirectory: 'chopper', + gitDiffPathArguments: [ + 'test/test_service.chopper.dart', + ], + ); + expectBuildClean( + packageRelativeDirectory: 'chopper', + gitDiffPathArguments: [ + 'test/test_service_variable.chopper.dart', + ], + ); + }, ); } diff --git a/chopper/test/test_service_variable.chopper.dart b/chopper/test/test_service_variable.chopper.dart new file mode 100644 index 00000000..c4423e71 --- /dev/null +++ b/chopper/test/test_service_variable.chopper.dart @@ -0,0 +1,641 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'test_service_variable.dart'; + +// ************************************************************************** +// ChopperGenerator +// ************************************************************************** + +// ignore_for_file: always_put_control_body_on_new_line, always_specify_types, prefer_const_declarations, unnecessary_brace_in_string_interps +final class _$HttpTestServiceVariable extends HttpTestServiceVariable { + _$HttpTestServiceVariable([ChopperClient? client]) { + if (client == null) return; + this.client = client; + } + + @override + final definitionType = HttpTestServiceVariable; + + @override + Future> getTest( + String id, { + required String dynamicHeader, + }) { + final Uri $url = Uri.parse('${service}/get/${id}'); + final Map $headers = { + 'test': dynamicHeader, + }; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + headers: $headers, + ); + return client.send($request); + } + + @override + Future> headTest() { + final Uri $url = Uri.parse('${service}/head'); + final Request $request = Request( + 'HEAD', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future> optionsTest() { + final Uri $url = Uri.parse('${service}/options'); + final Request $request = Request( + 'OPTIONS', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future>>> getStreamTest() { + final Uri $url = Uri.parse('${service}/get'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send>, int>($request); + } + + @override + Future> getAll() { + final Uri $url = Uri.parse('${service}'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future> getAllWithTrailingSlash() { + final Uri $url = Uri.parse('${service}/'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future> getQueryTest({ + String name = '', + int? number, + int? def = 42, + }) { + final Uri $url = Uri.parse('${service}/query'); + final Map $params = { + 'name': name, + 'int': number, + 'default_value': def, + }; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest(Map query) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = query; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest2( + Map query, { + bool? test, + }) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = {'test': test}; + $params.addAll(query); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest3({ + String name = '', + int? number, + Map filters = const {}, + }) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = { + 'name': name, + 'number': number, + }; + $params.addAll(filters); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest4({ + String name = '', + int? number, + Map? filters, + }) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = { + 'name': name, + 'number': number, + }; + $params.addAll(filters ?? const {}); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest5({Map? filters}) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = filters ?? const {}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getBody(dynamic body) { + final Uri $url = Uri.parse('${service}/get_body'); + final $body = body; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> postTest(String data) { + final Uri $url = Uri.parse('${service}/post'); + final $body = data; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> postStreamTest(Stream> byteStream) { + final Uri $url = Uri.parse('${service}/post'); + final $body = byteStream; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> putTest( + String test, + String data, + ) { + final Uri $url = Uri.parse('${service}/put/${test}'); + final $body = data; + final Request $request = Request( + 'PUT', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> deleteTest(String id) { + final Uri $url = Uri.parse('${service}/delete/${id}'); + final Map $headers = { + 'foo': 'bar', + }; + final Request $request = Request( + 'DELETE', + $url, + client.baseUrl, + headers: $headers, + ); + return client.send($request); + } + + @override + Future> patchTest( + String id, + String data, + ) { + final Uri $url = Uri.parse('${service}/patch/${id}'); + final $body = data; + final Request $request = Request( + 'PATCH', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> mapTest(Map map) { + final Uri $url = Uri.parse('${service}/map'); + final $body = map; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> postForm(Map fields) { + final Uri $url = Uri.parse('${service}/form/body'); + final $body = fields; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send( + $request, + requestConverter: convertForm, + ); + } + + @override + Future> postFormUsingHeaders(Map fields) { + final Uri $url = Uri.parse('${service}/form/body'); + final Map $headers = { + 'content-type': 'application/x-www-form-urlencoded', + }; + final $body = fields; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + headers: $headers, + ); + return client.send($request); + } + + @override + Future> postFormFields( + String foo, + int bar, + ) { + final Uri $url = Uri.parse('${service}/form/body/fields'); + final $body = { + 'foo': foo, + 'bar': bar, + }; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send( + $request, + requestConverter: convertForm, + ); + } + + @override + Future> forceJsonTest(Map map) { + final Uri $url = Uri.parse('${service}/map/json'); + final $body = map; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send( + $request, + requestConverter: customConvertRequest, + responseConverter: customConvertResponse, + ); + } + + @override + Future> postResources( + Map a, + Map b, + ) { + final Uri $url = Uri.parse('${service}/multi'); + final List $parts = [ + PartValue>( + '1', + a, + ), + PartValue>( + '2', + b, + ), + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postFile(List bytes) { + final Uri $url = Uri.parse('${service}/file'); + final List $parts = [ + PartValueFile>( + 'file', + bytes, + ) + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postImage(List imageData) { + final Uri $url = Uri.parse('${service}/image'); + final List $parts = [ + PartValueFile>( + 'image', + imageData, + ) + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postMultipartFile( + MultipartFile file, { + String? id, + }) { + final Uri $url = Uri.parse('${service}/file'); + final List $parts = [ + PartValue( + 'id', + id, + ), + PartValueFile( + 'file', + file, + ), + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postListFiles(List files) { + final Uri $url = Uri.parse('${service}/files'); + final List $parts = [ + PartValueFile>( + 'files', + files, + ) + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postMultipartList({ + required List ints, + required List doubles, + required List nums, + required List strings, + }) { + final Uri $url = Uri.parse('${service}/multipart_list'); + final List $parts = [ + PartValue>( + 'ints', + ints, + ), + PartValue>( + 'doubles', + doubles, + ), + PartValue>( + 'nums', + nums, + ), + PartValue>( + 'strings', + strings, + ), + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future fullUrl() { + final Uri $url = Uri.parse('https://test.com'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future>> listString() { + final Uri $url = Uri.parse('${service}/list/string'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send, String>($request); + } + + @override + Future> noBody() { + final Uri $url = Uri.parse('${service}/no-body'); + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future> getUsingQueryParamIncludeNullQueryVars({ + String? foo, + String? bar, + String? baz, + }) { + final Uri $url = + Uri.parse('${service}/query_param_include_null_query_vars'); + final Map $params = { + 'foo': foo, + 'bar': bar, + 'baz': baz, + }; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + includeNullQueryVars: true, + ); + return client.send($request); + } + + @override + Future> getUsingListQueryParam(List value) { + final Uri $url = Uri.parse('${service}/list_query_param'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getUsingListQueryParamWithBrackets( + List value) { + final Uri $url = Uri.parse('${service}/list_query_param_with_brackets'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + useBrackets: true, + ); + return client.send($request); + } + + @override + Future> getUsingMapQueryParam(Map value) { + final Uri $url = Uri.parse('${service}/map_query_param'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getUsingMapQueryParamIncludeNullQueryVars( + Map value) { + final Uri $url = + Uri.parse('${service}/map_query_param_include_null_query_vars'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + includeNullQueryVars: true, + ); + return client.send($request); + } + + @override + Future> getUsingMapQueryParamWithBrackets( + Map value) { + final Uri $url = Uri.parse('${service}/map_query_param_with_brackets'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + useBrackets: true, + ); + return client.send($request); + } +} diff --git a/chopper/test/test_service_variable.dart b/chopper/test/test_service_variable.dart new file mode 100644 index 00000000..81532976 --- /dev/null +++ b/chopper/test/test_service_variable.dart @@ -0,0 +1,220 @@ +import 'dart:async'; +import 'dart:convert'; + +import 'package:chopper/chopper.dart'; +import 'package:http/http.dart' show MultipartFile; + +part 'test_service_variable.chopper.dart'; + +const String service = 'https://localhost:4000'; + +@ChopperApi(baseUrl: service) +abstract class HttpTestServiceVariable extends ChopperService { + static HttpTestServiceVariable create([ChopperClient? client]) => + _$HttpTestServiceVariable(client); + + @Get(path: 'get/{id}') + Future> getTest( + @Path() String id, { + @Header('test') required String dynamicHeader, + }); + + @Head(path: 'head') + Future headTest(); + + @Options(path: 'options') + Future optionsTest(); + + @Get(path: 'get') + Future>>> getStreamTest(); + + @Get(path: '') + Future getAll(); + + @Get(path: '/') + Future getAllWithTrailingSlash(); + + @Get(path: 'query') + Future getQueryTest({ + @Query('name') String name = '', + @Query('int') int? number, + @Query('default_value') int? def = 42, + }); + + @Get(path: 'query_map') + Future getQueryMapTest(@QueryMap() Map query); + + @Get(path: 'query_map') + Future getQueryMapTest2( + @QueryMap() Map query, { + @Query('test') bool? test, + }); + + @Get(path: 'query_map') + Future getQueryMapTest3({ + @Query('name') String name = '', + @Query('number') int? number, + @QueryMap() Map filters = const {}, + }); + + @Get(path: 'query_map') + Future getQueryMapTest4({ + @Query('name') String name = '', + @Query('number') int? number, + @QueryMap() Map? filters, + }); + + @Get(path: 'query_map') + Future getQueryMapTest5({ + @QueryMap() Map? filters, + }); + + @Get(path: 'get_body') + Future getBody(@Body() dynamic body); + + @Post(path: 'post') + Future postTest(@Body() String data); + + @Post(path: 'post') + Future postStreamTest(@Body() Stream> byteStream); + + @Put(path: 'put/{id}') + Future putTest(@Path('id') String test, @Body() String data); + + @Delete(path: 'delete/{id}', headers: {'foo': 'bar'}) + Future deleteTest(@Path() String id); + + @Patch(path: 'patch/{id}') + Future patchTest(@Path() String id, @Body() String data); + + @Post(path: 'map') + Future mapTest(@Body() Map map); + + @FactoryConverter(request: convertForm) + @Post(path: 'form/body') + Future postForm(@Body() Map fields); + + @Post(path: 'form/body', headers: {contentTypeKey: formEncodedHeaders}) + Future postFormUsingHeaders(@Body() Map fields); + + @FactoryConverter(request: convertForm) + @Post(path: 'form/body/fields') + Future postFormFields(@Field() String foo, @Field() int bar); + + @Post(path: 'map/json') + @FactoryConverter( + request: customConvertRequest, + response: customConvertResponse, + ) + Future forceJsonTest(@Body() Map map); + + @Post(path: 'multi') + @multipart + Future postResources( + @Part('1') Map a, + @Part('2') Map b, + ); + + @Post(path: 'file') + @multipart + Future postFile( + @PartFile('file') List bytes, + ); + + @Post(path: 'image') + @multipart + Future postImage( + @PartFile('image') List imageData, + ); + + @Post(path: 'file') + @multipart + Future postMultipartFile( + @PartFile() MultipartFile file, { + @Part() String? id, + }); + + @Post(path: 'files') + @multipart + Future postListFiles(@PartFile() List files); + + @Post(path: 'multipart_list') + @multipart + Future postMultipartList({ + @Part('ints') required List ints, + @Part('doubles') required List doubles, + @Part('nums') required List nums, + @Part('strings') required List strings, + }); + + @Get(path: 'https://test.com') + Future fullUrl(); + + @Get(path: '/list/string') + Future>> listString(); + + @Post(path: 'no-body') + Future noBody(); + + @Get(path: '/query_param_include_null_query_vars', includeNullQueryVars: true) + Future> getUsingQueryParamIncludeNullQueryVars({ + @Query('foo') String? foo, + @Query('bar') String? bar, + @Query('baz') String? baz, + }); + + @Get(path: '/list_query_param') + Future> getUsingListQueryParam( + @Query('value') List value, + ); + + @Get(path: '/list_query_param_with_brackets', useBrackets: true) + Future> getUsingListQueryParamWithBrackets( + @Query('value') List value, + ); + + @Get(path: '/map_query_param') + Future> getUsingMapQueryParam( + @Query('value') Map value, + ); + + @Get( + path: '/map_query_param_include_null_query_vars', + includeNullQueryVars: true, + ) + Future> getUsingMapQueryParamIncludeNullQueryVars( + @Query('value') Map value, + ); + + @Get(path: '/map_query_param_with_brackets', useBrackets: true) + Future> getUsingMapQueryParamWithBrackets( + @Query('value') Map value, + ); +} + +Request customConvertRequest(Request req) { + final r = JsonConverter().convertRequest(req); + + return applyHeader(r, 'customConverter', 'true'); +} + +Response customConvertResponse(Response res) => + res.copyWith(body: json.decode(res.body)); + +Request convertForm(Request req) { + req = applyHeader(req, contentTypeKey, formEncodedHeaders); + + if (req.body is Map) { + final body = {}; + + req.body.forEach((key, val) { + if (val != null) { + body[key.toString()] = val.toString(); + } + }); + + req = req.copyWith(body: body); + } + + return req; +} diff --git a/chopper_generator/test/ensure_build_test.dart b/chopper_generator/test/ensure_build_test.dart index cb5dc60f..c708d8d1 100644 --- a/chopper_generator/test/ensure_build_test.dart +++ b/chopper_generator/test/ensure_build_test.dart @@ -6,11 +6,20 @@ import 'package:test/test.dart'; void main() { test( 'ensure_build', - () => expectBuildClean( - packageRelativeDirectory: 'chopper_generator', - gitDiffPathArguments: [ - 'test/test_service.chopper.dart', - ], - ), + () { + expectBuildClean( + packageRelativeDirectory: 'chopper_generator', + gitDiffPathArguments: [ + 'test/test_service.chopper.dart', + ], + ); + + expectBuildClean( + packageRelativeDirectory: 'chopper_generator', + gitDiffPathArguments: [ + 'test/test_service_variable.chopper.dart', + ], + ); + }, ); } diff --git a/chopper_generator/test/test_service_variable.chopper.dart b/chopper_generator/test/test_service_variable.chopper.dart new file mode 100644 index 00000000..c4423e71 --- /dev/null +++ b/chopper_generator/test/test_service_variable.chopper.dart @@ -0,0 +1,641 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'test_service_variable.dart'; + +// ************************************************************************** +// ChopperGenerator +// ************************************************************************** + +// ignore_for_file: always_put_control_body_on_new_line, always_specify_types, prefer_const_declarations, unnecessary_brace_in_string_interps +final class _$HttpTestServiceVariable extends HttpTestServiceVariable { + _$HttpTestServiceVariable([ChopperClient? client]) { + if (client == null) return; + this.client = client; + } + + @override + final definitionType = HttpTestServiceVariable; + + @override + Future> getTest( + String id, { + required String dynamicHeader, + }) { + final Uri $url = Uri.parse('${service}/get/${id}'); + final Map $headers = { + 'test': dynamicHeader, + }; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + headers: $headers, + ); + return client.send($request); + } + + @override + Future> headTest() { + final Uri $url = Uri.parse('${service}/head'); + final Request $request = Request( + 'HEAD', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future> optionsTest() { + final Uri $url = Uri.parse('${service}/options'); + final Request $request = Request( + 'OPTIONS', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future>>> getStreamTest() { + final Uri $url = Uri.parse('${service}/get'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send>, int>($request); + } + + @override + Future> getAll() { + final Uri $url = Uri.parse('${service}'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future> getAllWithTrailingSlash() { + final Uri $url = Uri.parse('${service}/'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future> getQueryTest({ + String name = '', + int? number, + int? def = 42, + }) { + final Uri $url = Uri.parse('${service}/query'); + final Map $params = { + 'name': name, + 'int': number, + 'default_value': def, + }; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest(Map query) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = query; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest2( + Map query, { + bool? test, + }) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = {'test': test}; + $params.addAll(query); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest3({ + String name = '', + int? number, + Map filters = const {}, + }) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = { + 'name': name, + 'number': number, + }; + $params.addAll(filters); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest4({ + String name = '', + int? number, + Map? filters, + }) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = { + 'name': name, + 'number': number, + }; + $params.addAll(filters ?? const {}); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getQueryMapTest5({Map? filters}) { + final Uri $url = Uri.parse('${service}/query_map'); + final Map $params = filters ?? const {}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getBody(dynamic body) { + final Uri $url = Uri.parse('${service}/get_body'); + final $body = body; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> postTest(String data) { + final Uri $url = Uri.parse('${service}/post'); + final $body = data; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> postStreamTest(Stream> byteStream) { + final Uri $url = Uri.parse('${service}/post'); + final $body = byteStream; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> putTest( + String test, + String data, + ) { + final Uri $url = Uri.parse('${service}/put/${test}'); + final $body = data; + final Request $request = Request( + 'PUT', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> deleteTest(String id) { + final Uri $url = Uri.parse('${service}/delete/${id}'); + final Map $headers = { + 'foo': 'bar', + }; + final Request $request = Request( + 'DELETE', + $url, + client.baseUrl, + headers: $headers, + ); + return client.send($request); + } + + @override + Future> patchTest( + String id, + String data, + ) { + final Uri $url = Uri.parse('${service}/patch/${id}'); + final $body = data; + final Request $request = Request( + 'PATCH', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> mapTest(Map map) { + final Uri $url = Uri.parse('${service}/map'); + final $body = map; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send($request); + } + + @override + Future> postForm(Map fields) { + final Uri $url = Uri.parse('${service}/form/body'); + final $body = fields; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send( + $request, + requestConverter: convertForm, + ); + } + + @override + Future> postFormUsingHeaders(Map fields) { + final Uri $url = Uri.parse('${service}/form/body'); + final Map $headers = { + 'content-type': 'application/x-www-form-urlencoded', + }; + final $body = fields; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + headers: $headers, + ); + return client.send($request); + } + + @override + Future> postFormFields( + String foo, + int bar, + ) { + final Uri $url = Uri.parse('${service}/form/body/fields'); + final $body = { + 'foo': foo, + 'bar': bar, + }; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send( + $request, + requestConverter: convertForm, + ); + } + + @override + Future> forceJsonTest(Map map) { + final Uri $url = Uri.parse('${service}/map/json'); + final $body = map; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + body: $body, + ); + return client.send( + $request, + requestConverter: customConvertRequest, + responseConverter: customConvertResponse, + ); + } + + @override + Future> postResources( + Map a, + Map b, + ) { + final Uri $url = Uri.parse('${service}/multi'); + final List $parts = [ + PartValue>( + '1', + a, + ), + PartValue>( + '2', + b, + ), + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postFile(List bytes) { + final Uri $url = Uri.parse('${service}/file'); + final List $parts = [ + PartValueFile>( + 'file', + bytes, + ) + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postImage(List imageData) { + final Uri $url = Uri.parse('${service}/image'); + final List $parts = [ + PartValueFile>( + 'image', + imageData, + ) + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postMultipartFile( + MultipartFile file, { + String? id, + }) { + final Uri $url = Uri.parse('${service}/file'); + final List $parts = [ + PartValue( + 'id', + id, + ), + PartValueFile( + 'file', + file, + ), + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postListFiles(List files) { + final Uri $url = Uri.parse('${service}/files'); + final List $parts = [ + PartValueFile>( + 'files', + files, + ) + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future> postMultipartList({ + required List ints, + required List doubles, + required List nums, + required List strings, + }) { + final Uri $url = Uri.parse('${service}/multipart_list'); + final List $parts = [ + PartValue>( + 'ints', + ints, + ), + PartValue>( + 'doubles', + doubles, + ), + PartValue>( + 'nums', + nums, + ), + PartValue>( + 'strings', + strings, + ), + ]; + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + parts: $parts, + multipart: true, + ); + return client.send($request); + } + + @override + Future fullUrl() { + final Uri $url = Uri.parse('https://test.com'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future>> listString() { + final Uri $url = Uri.parse('${service}/list/string'); + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + ); + return client.send, String>($request); + } + + @override + Future> noBody() { + final Uri $url = Uri.parse('${service}/no-body'); + final Request $request = Request( + 'POST', + $url, + client.baseUrl, + ); + return client.send($request); + } + + @override + Future> getUsingQueryParamIncludeNullQueryVars({ + String? foo, + String? bar, + String? baz, + }) { + final Uri $url = + Uri.parse('${service}/query_param_include_null_query_vars'); + final Map $params = { + 'foo': foo, + 'bar': bar, + 'baz': baz, + }; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + includeNullQueryVars: true, + ); + return client.send($request); + } + + @override + Future> getUsingListQueryParam(List value) { + final Uri $url = Uri.parse('${service}/list_query_param'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getUsingListQueryParamWithBrackets( + List value) { + final Uri $url = Uri.parse('${service}/list_query_param_with_brackets'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + useBrackets: true, + ); + return client.send($request); + } + + @override + Future> getUsingMapQueryParam(Map value) { + final Uri $url = Uri.parse('${service}/map_query_param'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + ); + return client.send($request); + } + + @override + Future> getUsingMapQueryParamIncludeNullQueryVars( + Map value) { + final Uri $url = + Uri.parse('${service}/map_query_param_include_null_query_vars'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + includeNullQueryVars: true, + ); + return client.send($request); + } + + @override + Future> getUsingMapQueryParamWithBrackets( + Map value) { + final Uri $url = Uri.parse('${service}/map_query_param_with_brackets'); + final Map $params = {'value': value}; + final Request $request = Request( + 'GET', + $url, + client.baseUrl, + parameters: $params, + useBrackets: true, + ); + return client.send($request); + } +} diff --git a/chopper_generator/test/test_service_variable.dart b/chopper_generator/test/test_service_variable.dart new file mode 100644 index 00000000..81532976 --- /dev/null +++ b/chopper_generator/test/test_service_variable.dart @@ -0,0 +1,220 @@ +import 'dart:async'; +import 'dart:convert'; + +import 'package:chopper/chopper.dart'; +import 'package:http/http.dart' show MultipartFile; + +part 'test_service_variable.chopper.dart'; + +const String service = 'https://localhost:4000'; + +@ChopperApi(baseUrl: service) +abstract class HttpTestServiceVariable extends ChopperService { + static HttpTestServiceVariable create([ChopperClient? client]) => + _$HttpTestServiceVariable(client); + + @Get(path: 'get/{id}') + Future> getTest( + @Path() String id, { + @Header('test') required String dynamicHeader, + }); + + @Head(path: 'head') + Future headTest(); + + @Options(path: 'options') + Future optionsTest(); + + @Get(path: 'get') + Future>>> getStreamTest(); + + @Get(path: '') + Future getAll(); + + @Get(path: '/') + Future getAllWithTrailingSlash(); + + @Get(path: 'query') + Future getQueryTest({ + @Query('name') String name = '', + @Query('int') int? number, + @Query('default_value') int? def = 42, + }); + + @Get(path: 'query_map') + Future getQueryMapTest(@QueryMap() Map query); + + @Get(path: 'query_map') + Future getQueryMapTest2( + @QueryMap() Map query, { + @Query('test') bool? test, + }); + + @Get(path: 'query_map') + Future getQueryMapTest3({ + @Query('name') String name = '', + @Query('number') int? number, + @QueryMap() Map filters = const {}, + }); + + @Get(path: 'query_map') + Future getQueryMapTest4({ + @Query('name') String name = '', + @Query('number') int? number, + @QueryMap() Map? filters, + }); + + @Get(path: 'query_map') + Future getQueryMapTest5({ + @QueryMap() Map? filters, + }); + + @Get(path: 'get_body') + Future getBody(@Body() dynamic body); + + @Post(path: 'post') + Future postTest(@Body() String data); + + @Post(path: 'post') + Future postStreamTest(@Body() Stream> byteStream); + + @Put(path: 'put/{id}') + Future putTest(@Path('id') String test, @Body() String data); + + @Delete(path: 'delete/{id}', headers: {'foo': 'bar'}) + Future deleteTest(@Path() String id); + + @Patch(path: 'patch/{id}') + Future patchTest(@Path() String id, @Body() String data); + + @Post(path: 'map') + Future mapTest(@Body() Map map); + + @FactoryConverter(request: convertForm) + @Post(path: 'form/body') + Future postForm(@Body() Map fields); + + @Post(path: 'form/body', headers: {contentTypeKey: formEncodedHeaders}) + Future postFormUsingHeaders(@Body() Map fields); + + @FactoryConverter(request: convertForm) + @Post(path: 'form/body/fields') + Future postFormFields(@Field() String foo, @Field() int bar); + + @Post(path: 'map/json') + @FactoryConverter( + request: customConvertRequest, + response: customConvertResponse, + ) + Future forceJsonTest(@Body() Map map); + + @Post(path: 'multi') + @multipart + Future postResources( + @Part('1') Map a, + @Part('2') Map b, + ); + + @Post(path: 'file') + @multipart + Future postFile( + @PartFile('file') List bytes, + ); + + @Post(path: 'image') + @multipart + Future postImage( + @PartFile('image') List imageData, + ); + + @Post(path: 'file') + @multipart + Future postMultipartFile( + @PartFile() MultipartFile file, { + @Part() String? id, + }); + + @Post(path: 'files') + @multipart + Future postListFiles(@PartFile() List files); + + @Post(path: 'multipart_list') + @multipart + Future postMultipartList({ + @Part('ints') required List ints, + @Part('doubles') required List doubles, + @Part('nums') required List nums, + @Part('strings') required List strings, + }); + + @Get(path: 'https://test.com') + Future fullUrl(); + + @Get(path: '/list/string') + Future>> listString(); + + @Post(path: 'no-body') + Future noBody(); + + @Get(path: '/query_param_include_null_query_vars', includeNullQueryVars: true) + Future> getUsingQueryParamIncludeNullQueryVars({ + @Query('foo') String? foo, + @Query('bar') String? bar, + @Query('baz') String? baz, + }); + + @Get(path: '/list_query_param') + Future> getUsingListQueryParam( + @Query('value') List value, + ); + + @Get(path: '/list_query_param_with_brackets', useBrackets: true) + Future> getUsingListQueryParamWithBrackets( + @Query('value') List value, + ); + + @Get(path: '/map_query_param') + Future> getUsingMapQueryParam( + @Query('value') Map value, + ); + + @Get( + path: '/map_query_param_include_null_query_vars', + includeNullQueryVars: true, + ) + Future> getUsingMapQueryParamIncludeNullQueryVars( + @Query('value') Map value, + ); + + @Get(path: '/map_query_param_with_brackets', useBrackets: true) + Future> getUsingMapQueryParamWithBrackets( + @Query('value') Map value, + ); +} + +Request customConvertRequest(Request req) { + final r = JsonConverter().convertRequest(req); + + return applyHeader(r, 'customConverter', 'true'); +} + +Response customConvertResponse(Response res) => + res.copyWith(body: json.decode(res.body)); + +Request convertForm(Request req) { + req = applyHeader(req, contentTypeKey, formEncodedHeaders); + + if (req.body is Map) { + final body = {}; + + req.body.forEach((key, val) { + if (val != null) { + body[key.toString()] = val.toString(); + } + }); + + req = req.copyWith(body: body); + } + + return req; +}