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

Fix incorrect url generation when using new baseurl #520

Merged
merged 5 commits into from
Oct 12, 2023
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
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;
}
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