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

Mock note #133

Merged
merged 2 commits into from
Apr 14, 2020
Merged
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
49 changes: 48 additions & 1 deletion faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,53 @@ Request _addQuery(Request req) {
}
```

## Mock ChopperClient for testing

Chopper is built on top of `http` package.

So, one can just use the mocking API of the HTTP package.
https://pub.dev/documentation/http/latest/testing/MockClient-class.html

Also, you can follow this code by [ozburo](https://github.com/ozburo):
```dart
import 'dart:convert';

import 'package:chopper/chopper.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';

part 'api_service.chopper.dart';

@ChopperApi()
abstract class ApiService extends ChopperService {
static ApiService create() {
final client = ChopperClient(
client: MockClient((request) async {
Map result = mockData[request.url.path]?.firstWhere((mockQueryParams) {
if (mockQueryParams['id'] == request.url.queryParameters['id']) return true;
return false;
}, orElse: () => null);
if (result == null) {
return http.Response(
json.encode({'error': "not found"}), 404);
}
return http.Response(json.encode(result), 200);
}),
baseUrl: 'https://mysite.com/api',
services: [
_$ApiService(),
],
converter: JsonConverter(),
errorConverter: JsonConverter(),
);
return _$ApiService(client);
}

@Get(path: "/get")
Future<Response> get(@Query() String url);
}
```

## Use Https certificate

Chopper is built on top of `http` package and you can override the inner http client.
Expand All @@ -53,7 +100,7 @@ import 'dart:io';
import 'package:http/io_client.dart' as http;

final ioc = new HttpClient();
ioc.badCertificateCallback = (X509Certificate cert, String host, int port)
ioc.badCertificateCallback = (X509Certificate cert, String host, int port)
=> true;

final chopper = ChopperClient(
Expand Down