Skip to content

Commit

Permalink
api: Send simple User-Agent header
Browse files Browse the repository at this point in the history
Fixes: zulip#406
Fixes: zulip#460

[chris: changed commit message; changed user-agent string; included
in image requests]
  • Loading branch information
sirpengi authored and gnprice committed Feb 6, 2024
1 parent 7cbb743 commit 4d5b3e1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 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 @@ -201,6 +202,13 @@ Map<String, String> authHeader({required String email, required String apiKey})
};
}

Map<String, String> userAgentHeader() {
return {
// TODO(#467) include platform, platform version, and app version
'User-Agent': 'ZulipFlutter',
};
}

Map<String, String>? encodeParameters(Map<String, dynamic>? params) {
return params?.map((k, v) =>
MapEntry(k, v is RawParameter ? v.value : jsonEncode(v)));
Expand Down
1 change: 1 addition & 0 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ class RealmContentNetworkImage extends StatelessWidget {
if (src.origin == account.realmUrl.origin) ...authHeader(
email: account.email, apiKey: account.apiKey,
),
...userAgentHeader(),
},
cacheWidth: cacheWidth,
cacheHeight: cacheHeight,
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 @@ -22,7 +22,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 @@ -52,6 +55,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 @@ -82,7 +86,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) => it
..field.equals('file')
Expand Down Expand Up @@ -114,6 +121,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
7 changes: 5 additions & 2 deletions test/widgets/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,16 @@ void main() {

testWidgets('includes auth header if `src` on-realm', (tester) async {
check(await actualHeaders(tester, Uri.parse('https://chat.example/image.png')))
.deepEquals({'Authorization': [authHeaders['Authorization']!]});
.deepEquals({
'Authorization': [authHeaders['Authorization']!],
'User-Agent': [userAgentHeader()['User-Agent']!],
});
debugNetworkImageHttpClientProvider = null;
});

testWidgets('excludes auth header if `src` off-realm', (tester) async {
check(await actualHeaders(tester, Uri.parse('https://other.example/image.png')))
.deepEquals({});
.deepEquals({'User-Agent': [userAgentHeader()['User-Agent']!]});
debugNetworkImageHttpClientProvider = null;
});

Expand Down

0 comments on commit 4d5b3e1

Please sign in to comment.