Skip to content

Commit

Permalink
🔖 release v7.0.6 (#496)
Browse files Browse the repository at this point in the history
# chopper

## 7.0.6

- #493
- #497
- #495

# chopper_built_value

## 2.0.1

- #495

# chopper_generator

## 7.0.3

- #493
- #495

---------

Co-authored-by: Klemen Tusar <techouse@gmail.com>
Co-authored-by: Martin Alejandro Escobar Espinel <56127727+martinale14@users.noreply.github.com>
  • Loading branch information
techouse and martinale14 authored Sep 2, 2023
1 parent c0133d7 commit 7833e70
Show file tree
Hide file tree
Showing 18 changed files with 2,028 additions and 36 deletions.
6 changes: 6 additions & 0 deletions chopper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 7.0.6

- The @ChopperApi annotation's baseUrl property can be used as a top level constant string variable ([#493](https://github.com/lejard-h/chopper/pull/493))
- Fix ChopperClient.send() sending wrong request when using an Authenticator ([#497](https://github.com/lejard-h/chopper/pull/497))
- Add pub.dev topics to package metadata ([#495](https://github.com/lejard-h/chopper/pull/495))

## 7.0.5

- Fix documentation links in README ([#488](https://github.com/lejard-h/chopper/pull/488))
Expand Down
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
7 changes: 2 additions & 5 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,8 @@ base class ChopperClient {
dynamic res = Response(response, response.body);

if (authenticator != null) {
final Request? updatedRequest = await authenticator!.authenticate(
request,
res,
request,
);
final Request? updatedRequest =
await authenticator!.authenticate(req, res, request);

if (updatedRequest != null) {
res = await send<BodyType, InnerType>(
Expand Down
19 changes: 16 additions & 3 deletions chopper/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@ class JsonConverter implements Converter, ErrorConverter {
Request encodeJson(Request request) {
final String? contentType = request.headers[contentTypeKey];

return (contentType?.contains(jsonHeaders) ?? false)
? request.copyWith(body: json.encode(request.body))
: request;
if ((contentType?.contains(jsonHeaders) ?? false) &&
(request.body.runtimeType != String || !isJson(request.body))) {
return request.copyWith(body: json.encode(request.body));
}

return request;
}

FutureOr<Response> decodeJson<BodyType, InnerType>(Response response) async {
Expand Down Expand Up @@ -255,6 +258,16 @@ class JsonConverter implements Converter, ErrorConverter {

static Request requestFactory(Request request) =>
const JsonConverter().convertRequest(request);

@visibleForTesting
static bool isJson(dynamic data) {
try {
json.decode(data);
return true;
} catch (_) {
return false;
}
}
}

/// A [Converter] implementation that converts only [Request]s having a [Map] as their body.
Expand Down
8 changes: 7 additions & 1 deletion chopper/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chopper
description: Chopper is an http client generator using source_gen, inspired by Retrofit
version: 7.0.5
version: 7.0.6
documentation: https://hadrien-lejard.gitbook.io/chopper
repository: https://github.com/lejard-h/chopper

Expand Down Expand Up @@ -30,3 +30,9 @@ dev_dependencies:
dependency_overrides:
chopper_generator:
path: ../chopper_generator

topics:
- api
- client
- http
- rest
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
19 changes: 19 additions & 0 deletions chopper/test/converter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ void main() {

expect(converted.body, equals({'foo': 'bar'}));
});

test('JsonConverter.isJson', () {
expect(JsonConverter.isJson('{"foo":"bar"}'), isTrue);
expect(JsonConverter.isJson('foo'), isFalse);
expect(JsonConverter.isJson(''), isFalse);
expect(JsonConverter.isJson(null), isFalse);
expect(JsonConverter.isJson(42), isFalse);
expect(JsonConverter.isJson([]), isFalse);
expect(JsonConverter.isJson([1, 2, 3]), isFalse);
expect(JsonConverter.isJson(['a', 'b', 'c']), isFalse);
expect(JsonConverter.isJson({}), isFalse);
expect(
JsonConverter.isJson({
'foo': 'bar',
'list': [1, 2, 3],
}),
isFalse,
);
});
});

test('respects content-type headers', () {
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',
],
);
},
);
}
Loading

0 comments on commit 7833e70

Please sign in to comment.