From 2b19b824dceb9cfbc46e41c29829d188161043d8 Mon Sep 17 00:00:00 2001 From: Karl Heinz Struggl Date: Fri, 8 Mar 2024 04:08:02 -0800 Subject: [PATCH] chore: Add SDK version to User-Agent header (#3722) * adds sdk version to user agent * adds user agent sdk info tests * review changes --- Sources/Sentry/SentryNSURLRequest.m | 8 ++++++-- Tests/SentryTests/SentryNSURLRequestTests.swift | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Sources/Sentry/SentryNSURLRequest.m b/Sources/Sentry/SentryNSURLRequest.m index be6bc9b3dae..d8556589870 100644 --- a/Sources/Sentry/SentryNSURLRequest.m +++ b/Sources/Sentry/SentryNSURLRequest.m @@ -58,7 +58,9 @@ - (_Nullable instancetype)initStoreRequestWithDsn:(SentryDsn *)dsn self.HTTPMethod = @"POST"; [self setValue:authHeader forHTTPHeaderField:@"X-Sentry-Auth"]; [self setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; - [self setValue:SentryMeta.sdkName forHTTPHeaderField:@"User-Agent"]; + [self setValue:[NSString + stringWithFormat:@"%@/%@", SentryMeta.sdkName, SentryMeta.versionString] + forHTTPHeaderField:@"User-Agent"]; [self setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"]; self.HTTPBody = [data sentry_gzippedWithCompressionLevel:-1 error:error]; } @@ -93,7 +95,9 @@ - (instancetype)initEnvelopeRequestWithURL:(NSURL *)url [self setValue:authHeader forHTTPHeaderField:@"X-Sentry-Auth"]; } [self setValue:@"application/x-sentry-envelope" forHTTPHeaderField:@"Content-Type"]; - [self setValue:SentryMeta.sdkName forHTTPHeaderField:@"User-Agent"]; + [self setValue:[NSString + stringWithFormat:@"%@/%@", SentryMeta.sdkName, SentryMeta.versionString] + forHTTPHeaderField:@"User-Agent"]; [self setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"]; self.HTTPBody = [data sentry_gzippedWithCompressionLevel:-1 error:error]; } diff --git a/Tests/SentryTests/SentryNSURLRequestTests.swift b/Tests/SentryTests/SentryNSURLRequestTests.swift index 48ba83178a4..9550e8aea65 100644 --- a/Tests/SentryTests/SentryNSURLRequestTests.swift +++ b/Tests/SentryTests/SentryNSURLRequestTests.swift @@ -1,3 +1,4 @@ +import Nimble @testable import Sentry import SentryTestUtils import XCTest @@ -11,10 +12,21 @@ class SentryNSURLRequestTests: XCTestCase { func testRequestWithEnvelopeEndpoint() { let request = try! SentryNSURLRequest(envelopeRequestWith: SentryNSURLRequestTests.dsn(), andData: Data()) - XCTAssertTrue(request.url!.absoluteString.hasSuffix("/envelope/")) + expect(request.url!.absoluteString).to(endWith("/envelope/")) } + func testRequestWithStoreEndpoint() { let request = try! SentryNSURLRequest(storeRequestWith: SentryNSURLRequestTests.dsn(), andData: Data()) - XCTAssertTrue(request.url!.absoluteString.hasSuffix("/store/")) + expect(request.url!.absoluteString).to(endWith("/store/")) + } + + func testRequestWithEnvelopeEndpoint_hasUserAgentWithSdkNameAndVersion() { + let request = try! SentryNSURLRequest(envelopeRequestWith: SentryNSURLRequestTests.dsn(), andData: Data()) + expect(request.allHTTPHeaderFields?["User-Agent"]) == "\(SentryMeta.sdkName)/\(SentryMeta.versionString)" + } + + func testRequestWithStoreEndpoint_hasUserAgentWithSdkNameAndVersion() { + let request = try! SentryNSURLRequest(storeRequestWith: SentryNSURLRequestTests.dsn(), andData: Data()) + expect(request.allHTTPHeaderFields?["User-Agent"]) == "\(SentryMeta.sdkName)/\(SentryMeta.versionString)" } }