Skip to content

Commit

Permalink
core: Send hardcoded User-Agent header in requests
Browse files Browse the repository at this point in the history
This is required for the server to automatically mark
sent messages as read. See `sent_by_human` in
`zulip/zerver/models.py`.

Fixes: zulip#440
  • Loading branch information
sirpengi committed Dec 14, 2023
1 parent c06855e commit 9bb9d81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/api/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class ApiConnection {
assert(debugLog("${request.method} ${request.url}"));

addAuth(request);
request.headers.addAll(userAgentHeader());

final http.StreamedResponse response;
try {
Expand Down Expand Up @@ -198,6 +199,13 @@ Map<String, String> authHeader({required String email, required String apiKey})
};
}

// TODO(#453): Create real User-Agent string
Map<String, String> userAgentHeader() {
return {
'User-Agent': 'ZulipMobile/?.?.? (Android 14)',
};
}

Map<String, String>? encodeParameters(Map<String, dynamic>? params) {
return params?.map((k, v) =>
MapEntry(k, v is RawParameter ? v.value : jsonEncode(v)));
Expand Down
12 changes: 10 additions & 2 deletions test/api/core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ void main() {
check(connection.lastRequest!).isA<http.Request>()
..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('');
});
}
Expand Down Expand Up @@ -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',
})
Expand Down Expand Up @@ -83,7 +87,10 @@ void main() {
check(connection.lastRequest!).isA<http.MultipartRequest>()
..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')
Expand Down Expand Up @@ -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',
})
Expand Down

0 comments on commit 9bb9d81

Please sign in to comment.