Skip to content

Commit 92f47a5

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 <zixuan@zulip.com>
1 parent cf19d68 commit 92f47a5

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
@@ -201,6 +201,37 @@ void main() {
201201
checkRequest(['asdf'.codeUnits], 100, filename: null);
202202
});
203203

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

0 commit comments

Comments
 (0)