Skip to content

Commit 9bb9d81

Browse files
committed
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
1 parent c06855e commit 9bb9d81

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/api/core.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class ApiConnection {
8080
assert(debugLog("${request.method} ${request.url}"));
8181

8282
addAuth(request);
83+
request.headers.addAll(userAgentHeader());
8384

8485
final http.StreamedResponse response;
8586
try {
@@ -198,6 +199,13 @@ Map<String, String> authHeader({required String email, required String apiKey})
198199
};
199200
}
200201

202+
// TODO(#453): Create real User-Agent string
203+
Map<String, String> userAgentHeader() {
204+
return {
205+
'User-Agent': 'ZulipMobile/?.?.? (Android 14)',
206+
};
207+
}
208+
201209
Map<String, String>? encodeParameters(Map<String, dynamic>? params) {
202210
return params?.map((k, v) =>
203211
MapEntry(k, v is RawParameter ? v.value : jsonEncode(v)));

test/api/core_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ void main() {
2323
check(connection.lastRequest!).isA<http.Request>()
2424
..method.equals('GET')
2525
..url.asString.equals('${eg.realmUrl.origin}$expectedRelativeUrl')
26-
..headers.deepEquals(authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey))
26+
..headers.deepEquals({
27+
...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
28+
...userAgentHeader(),
29+
})
2730
..body.equals('');
2831
});
2932
}
@@ -53,6 +56,7 @@ void main() {
5356
..url.asString.equals('${eg.realmUrl.origin}/api/v1/example/route')
5457
..headers.deepEquals({
5558
...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
59+
...userAgentHeader(),
5660
if (expectContentType)
5761
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
5862
})
@@ -83,7 +87,10 @@ void main() {
8387
check(connection.lastRequest!).isA<http.MultipartRequest>()
8488
..method.equals('POST')
8589
..url.asString.equals('${eg.realmUrl.origin}/api/v1/example/route')
86-
..headers.deepEquals(authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey))
90+
..headers.deepEquals({
91+
...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
92+
...userAgentHeader(),
93+
})
8794
..fields.deepEquals({})
8895
..files.single.which(it()
8996
..field.equals('file')
@@ -115,6 +122,7 @@ void main() {
115122
..url.asString.equals('${eg.realmUrl.origin}/api/v1/example/route')
116123
..headers.deepEquals({
117124
...authHeader(email: eg.selfAccount.email, apiKey: eg.selfAccount.apiKey),
125+
...userAgentHeader(),
118126
if (expectContentType)
119127
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
120128
})

0 commit comments

Comments
 (0)