Skip to content

Commit

Permalink
Adding support for top level variables (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinale14 committed Aug 31, 2023
1 parent c7b67a6 commit e706150
Show file tree
Hide file tree
Showing 9 changed files with 1,956 additions and 25 deletions.
2 changes: 2 additions & 0 deletions chopper/lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import 'package:meta/meta.dart';
@immutable
final class ChopperApi {
/// A part of a URL that every request defined inside a class annotated with [ChopperApi] will be prefixed with.
///
/// The `baseUrl` can be a top level constant string variable.
final String baseUrl;

const ChopperApi({
Expand Down
141 changes: 141 additions & 0 deletions chopper/test/base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -22,6 +24,7 @@ void main() {
services: [
// the generated service
HttpTestService.create(),
HttpTestServiceVariable.create(),
],
client: httpClient,
errorConverter: errorConverter,
Expand All @@ -35,9 +38,12 @@ void main() {

final chopper = buildClient(httpClient);
final service = chopper.getService<HttpTestService>();
final serviceVariable = chopper.getService<HttpTestServiceVariable>();

expect(service, isNotNull);
expect(serviceVariable, isNotNull);
expect(service, isA<HttpTestService>());
expect(serviceVariable, isA<HttpTestServiceVariable>());
});

test('get service errors', () async {
Expand Down Expand Up @@ -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<HttpTestServiceVariable>();

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(
Expand Down Expand Up @@ -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<HttpTestServiceVariable>();

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(
Expand Down Expand Up @@ -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<HttpTestServiceVariable>();

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(
Expand All @@ -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<HttpTestServiceVariable>();

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(
Expand All @@ -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<HttpTestServiceVariable>();

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(
Expand All @@ -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<HttpTestServiceVariable>();

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);
Expand Down
20 changes: 14 additions & 6 deletions chopper/test/ensure_build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
);
},
);
}

0 comments on commit e706150

Please sign in to comment.