From 9bb9d81afd97a30c62e93cc8d4864345cdcea32c Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Wed, 13 Dec 2023 16:57:27 +0000 Subject: [PATCH] core: Send hardcoded User-Agent header in requests This is required for the server to automatically mark sent messages as read. See `sent_by_human` in `zulip/zerver/models.py`. Fixes: #440 --- lib/api/core.dart | 8 ++++++++ test/api/core_test.dart | 12 ++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/api/core.dart b/lib/api/core.dart index b28da078b8..5762a526f3 100644 --- a/lib/api/core.dart +++ b/lib/api/core.dart @@ -80,6 +80,7 @@ class ApiConnection { assert(debugLog("${request.method} ${request.url}")); addAuth(request); + request.headers.addAll(userAgentHeader()); final http.StreamedResponse response; try { @@ -198,6 +199,13 @@ Map authHeader({required String email, required String apiKey}) }; } +// TODO(#453): Create real User-Agent string +Map userAgentHeader() { + return { + 'User-Agent': 'ZulipMobile/?.?.? (Android 14)', + }; +} + Map? encodeParameters(Map? params) { return params?.map((k, v) => MapEntry(k, v is RawParameter ? v.value : jsonEncode(v))); diff --git a/test/api/core_test.dart b/test/api/core_test.dart index c5883b386f..c3e9cbb735 100644 --- a/test/api/core_test.dart +++ b/test/api/core_test.dart @@ -23,7 +23,10 @@ void main() { check(connection.lastRequest!).isA() ..method.equals('GET') ..url.asString.equals('${eg.realmUrl.origin}$expectedRelativeUrl') - ..headers.deepEquals(authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey)) + ..headers.deepEquals({ + ...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey), + ...userAgentHeader(), + }) ..body.equals(''); }); } @@ -53,6 +56,7 @@ void main() { ..url.asString.equals('${eg.realmUrl.origin}/api/v1/example/route') ..headers.deepEquals({ ...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey), + ...userAgentHeader(), if (expectContentType) 'content-type': 'application/x-www-form-urlencoded; charset=utf-8', }) @@ -83,7 +87,10 @@ void main() { check(connection.lastRequest!).isA() ..method.equals('POST') ..url.asString.equals('${eg.realmUrl.origin}/api/v1/example/route') - ..headers.deepEquals(authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey)) + ..headers.deepEquals({ + ...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey), + ...userAgentHeader(), + }) ..fields.deepEquals({}) ..files.single.which(it() ..field.equals('file') @@ -115,6 +122,7 @@ void main() { ..url.asString.equals('${eg.realmUrl.origin}/api/v1/example/route') ..headers.deepEquals({ ...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey), + ...userAgentHeader(), if (expectContentType) 'content-type': 'application/x-www-form-urlencoded; charset=utf-8', })