Skip to content

Commit f859f57

Browse files
committed
api: Add ApiConnection.patch
The tests, ApiConnection.{post,patch,delete}, are mostly similar to each other, because the majority of them are testing that the params are parsed into the body with the same content type. If we find the need to update these test cases with new examples, it will be ripe to refactor them with a helper. Until then, just duplicate them for simplicity. Signed-off-by: Zixuan James Li <[email protected]>
1 parent eeb5c04 commit f859f57

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/api/core.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ class ApiConnection {
224224
return send(routeName, fromJson, request);
225225
}
226226

227+
Future<T> patch<T>(String routeName, T Function(Map<String, dynamic>) fromJson,
228+
String path, Map<String, dynamic>? params) async {
229+
final url = realmUrl.replace(path: "/api/v1/$path");
230+
final request = http.Request('PATCH', url);
231+
if (params != null) {
232+
request.bodyFields = encodeParameters(params)!;
233+
}
234+
return send(routeName, fromJson, request);
235+
}
236+
227237
Future<T> delete<T>(String routeName, T Function(Map<String, dynamic>) fromJson,
228238
String path, Map<String, dynamic>? params) async {
229239
final url = realmUrl.replace(path: "/api/v1/$path");

test/api/core_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,37 @@ void main() {
202202
checkRequest(['asdf'.codeUnits], 100, filename: null);
203203
});
204204

205+
test('ApiConnection.patch', () async {
206+
void checkRequest(Map<String, dynamic>? params, String expectedBody, {bool expectContentType = true}) {
207+
finish(FakeApiConnection.with_(account: eg.selfAccount, (connection) async {
208+
connection.prepare(json: {});
209+
await connection.patch(kExampleRouteName, (json) => json, 'example/route', params);
210+
check(connection.lastRequest!).isA<http.Request>()
211+
..method.equals('PATCH')
212+
..url.asString.equals('${eg.realmUrl.origin}/api/v1/example/route')
213+
..headers.deepEquals({
214+
...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
215+
...kFallbackUserAgentHeader,
216+
if (expectContentType)
217+
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
218+
})
219+
..body.equals(expectedBody);
220+
}));
221+
}
222+
223+
checkRequest(null, '', expectContentType: false);
224+
checkRequest({}, '');
225+
checkRequest({'x': 3}, 'x=3');
226+
checkRequest({'x': 3, 'y': 4}, 'x=3&y=4');
227+
checkRequest({'x': null}, 'x=null');
228+
checkRequest({'x': true}, 'x=true');
229+
checkRequest({'x': 'foo'}, 'x=%22foo%22');
230+
checkRequest({'x': [1, 2]}, 'x=%5B1%2C2%5D');
231+
checkRequest({'x': {'y': 1}}, 'x=%7B%22y%22%3A1%7D');
232+
checkRequest({'x': RawParameter('foo')}, 'x=foo');
233+
checkRequest({'x': RawParameter('foo'), 'y': 'bar'}, 'x=foo&y=%22bar%22');
234+
});
235+
205236
test('ApiConnection.delete', () async {
206237
void checkRequest(Map<String, dynamic>? params, String expectedBody, {bool expectContentType = true}) {
207238
finish(FakeApiConnection.with_(account: eg.selfAccount, (connection) async {

0 commit comments

Comments
 (0)