Skip to content

Commit

Permalink
🔖 release chopper_generator v7.0.6 (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse authored Oct 13, 2023
1 parent 961bdce commit fcbe6bb
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 9 deletions.
36 changes: 36 additions & 0 deletions chopper/test/base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:test/test.dart';
import 'package:transparent_image/transparent_image.dart';

import 'test_service.dart';
import 'test_service_base_url.dart';
import 'test_service_variable.dart';

final baseUrl = Uri.parse('http://localhost:8000');
Expand All @@ -26,6 +27,7 @@ void main() {
// the generated service
HttpTestService.create(),
HttpTestServiceVariable.create(),
HttpTestServiceBaseUrl.create(),
],
client: httpClient,
errorConverter: errorConverter,
Expand Down Expand Up @@ -1164,6 +1166,23 @@ void main() {
await service.getAll();
});

test('Empty path gives no trailing slash new base url', () async {
final httpClient = MockClient((request) async {
expect(
request.url.toString(),
equals('$testEnv/test'),
);
expect(request.method, equals('GET'));

return http.Response('get response', 200);
});

final chopper = buildClient(httpClient);
final service = chopper.getService<HttpTestServiceBaseUrl>();

await service.getAll();
});

test('Slash in path gives a trailing slash', () async {
final httpClient = MockClient((request) async {
expect(
Expand All @@ -1181,6 +1200,23 @@ void main() {
await service.getAllWithTrailingSlash();
});

test('Slash in path gives a trailing slash new base url', () async {
final httpClient = MockClient((request) async {
expect(
request.url.toString(),
equals('$testEnv/test/'),
);
expect(request.method, equals('GET'));

return http.Response('get response', 200);
});

final chopper = buildClient(httpClient);
final service = chopper.getService<HttpTestServiceBaseUrl>();

await service.getAllWithTrailingSlash();
});

test('timeout', () async {
final httpClient = MockClient((http.Request req) async {
await Future.delayed(const Duration(minutes: 1));
Expand Down
10 changes: 3 additions & 7 deletions chopper/test/ensure_build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import 'package:test/test.dart';
void main() {
test(
'ensure_build',
() {
expectBuildClean(
() async {
await expectBuildClean(
packageRelativeDirectory: 'chopper',
gitDiffPathArguments: [
'test/test_service.chopper.dart',
],
);
expectBuildClean(
packageRelativeDirectory: 'chopper',
gitDiffPathArguments: [
'test/test_service_variable.chopper.dart',
'test/test_service_base_url.chopper.dart',
],
);
},
Expand Down
148 changes: 148 additions & 0 deletions chopper/test/test_service_base_url.chopper.dart

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

83 changes: 83 additions & 0 deletions chopper/test/test_service_base_url.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:async';
import 'dart:convert';

import 'package:chopper/chopper.dart';

part 'test_service_base_url.chopper.dart';

@ChopperApi(baseUrl: 'https://localhost:4000/test')
abstract class HttpTestServiceBaseUrl extends ChopperService {
static HttpTestServiceBaseUrl create([ChopperClient? client]) =>
_$HttpTestServiceBaseUrl(client);

@Get(path: '')
Future<Response> getAll();

@Get(path: '/')
Future<Response> getAllWithTrailingSlash();

@Get(path: '/list/string')
Future<Response<List<String>>> listString();

@Get(path: '/query_param_include_null_query_vars', includeNullQueryVars: true)
Future<Response<String>> getUsingQueryParamIncludeNullQueryVars({
@Query('foo') String? foo,
@Query('bar') String? bar,
@Query('baz') String? baz,
});

@Get(path: '/list_query_param')
Future<Response<String>> getUsingListQueryParam(
@Query('value') List<String> value,
);

@Get(path: '/list_query_param_with_brackets', useBrackets: true)
Future<Response<String>> getUsingListQueryParamWithBrackets(
@Query('value') List<String> value,
);

@Get(path: '/map_query_param')
Future<Response<String>> getUsingMapQueryParam(
@Query('value') Map<String, dynamic> value,
);

@Get(
path: '/map_query_param_include_null_query_vars',
includeNullQueryVars: true,
)
Future<Response<String>> getUsingMapQueryParamIncludeNullQueryVars(
@Query('value') Map<String, dynamic> value,
);

@Get(path: '/map_query_param_with_brackets', useBrackets: true)
Future<Response<String>> getUsingMapQueryParamWithBrackets(
@Query('value') Map<String, dynamic> value,
);
}

Request customConvertRequest(Request req) {
final r = JsonConverter().convertRequest(req);

return applyHeader(r, 'customConverter', 'true');
}

Response<T> customConvertResponse<T>(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 = <String, String>{};

req.body.forEach((key, val) {
if (val != null) {
body[key.toString()] = val.toString();
}
});

req = req.copyWith(body: body);
}

return req;
}
4 changes: 4 additions & 0 deletions chopper_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 7.0.6

- Fix incorrect url generation when using new baseUrl ([#520](https://github.com/lejard-h/chopper/pull/520))

## 7.0.5+1

- Fix Github release workflow permissions ([#512](https://github.com/lejard-h/chopper/pull/512))
Expand Down
17 changes: 16 additions & 1 deletion chopper_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,22 @@ final class ChopperGenerator
}
}

return _generateUri('$finalBaseUrl$path'.replaceAll('//', '/'));
if (finalBaseUrl.startsWith('http://') ||
finalBaseUrl.startsWith('https://')) {
final tempUri = Uri.tryParse(finalBaseUrl);

if (tempUri != null) {
final urlNoScheme =
'${tempUri.authority}${tempUri.path}$path'.replaceAll('//', '/');
return _generateUri(
'${tempUri.scheme}://$urlNoScheme',
);
}
}

return _generateUri(
'$finalBaseUrl$path'.replaceAll('//', '/'),
);
}

static Expression _generateUri(String url) =>
Expand Down
2 changes: 1 addition & 1 deletion chopper_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chopper_generator
description: Chopper is an http client generator using source_gen, inspired by Retrofit
version: 7.0.5+1
version: 7.0.6
documentation: https://hadrien-lejard.gitbook.io/chopper
repository: https://github.com/lejard-h/chopper

Expand Down

0 comments on commit fcbe6bb

Please sign in to comment.