Skip to content

Commit

Permalink
chore: Add SDK version to User-Agent header (#3722)
Browse files Browse the repository at this point in the history
* adds sdk version to user agent

* adds user agent sdk info tests

* review changes
  • Loading branch information
kahest authored Mar 8, 2024
1 parent baeabab commit 2b19b82
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Sources/Sentry/SentryNSURLRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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];
}
Expand Down
16 changes: 14 additions & 2 deletions Tests/SentryTests/SentryNSURLRequestTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Nimble
@testable import Sentry
import SentryTestUtils
import XCTest
Expand All @@ -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)"
}
}

0 comments on commit 2b19b82

Please sign in to comment.