Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send RFC 7231 Formatted User Agent #761

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Analytics.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
6 changes: 5 additions & 1 deletion Analytics/Classes/Internal/SEGHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ - (instancetype)initWithRequestFactory:(SEGRequestFactory)requestFactory
}
_sessionsByWriteKey = [NSMutableDictionary dictionary];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPAdditionalHeaders = @{ @"Accept-Encoding" : @"gzip" };
config.HTTPAdditionalHeaders = @{
@"Accept-Encoding" : @"gzip",
@"User-Agent" : [NSString stringWithFormat:@"analytics-ios/%@", [SEGAnalytics version]],
};
_genericSession = [NSURLSession sessionWithConfiguration:config];
}
return self;
Expand All @@ -46,6 +49,7 @@ - (NSURLSession *)sessionForWriteKey:(NSString *)writeKey
@"Content-Encoding" : @"gzip",
@"Content-Type" : @"application/json",
@"Authorization" : [@"Basic " stringByAppendingString:[[self class] authorizationHeader:writeKey]],
@"User-Agent" : [NSString stringWithFormat:@"analytics-ios/%@", [SEGAnalytics version]],
};
session = [NSURLSession sessionWithConfiguration:config];
self.sessionsByWriteKey[writeKey] = session;
Expand Down
27 changes: 18 additions & 9 deletions AnalyticsTests/HTTPClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class HTTPClientTest: QuickSpec {

describe("settingsForWriteKey") {
it("succeeds for 2xx response") {
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)!
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withHeaders(["Accept-Encoding" : "gzip" ])!
.andReturn(200)!
.withHeaders(["Content-Type" : "application/json"])!
Expand All @@ -62,7 +63,8 @@ class HTTPClientTest: QuickSpec {
}

it("fails for non 2xx response") {
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)!
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withHeaders(["Accept-Encoding" : "gzip" ])!
.andReturn(400)!
.withHeaders(["Content-Type" : "application/json" ])!
Expand All @@ -77,7 +79,8 @@ class HTTPClientTest: QuickSpec {
}

it("fails for json error") {
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)!
_ = stubRequest("GET", "https://cdn-settings.segment.com/v1/projects/foo/settings" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withHeaders(["Accept-Encoding":"gzip"])!
.andReturn(200)!
.withHeaders(["Content-Type":"application/json"])!
Expand Down Expand Up @@ -113,7 +116,8 @@ class HTTPClientTest: QuickSpec {


it("does not ask to retry for 2xx response") {
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)!
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withJsonGzippedBody(batch as AnyObject)
.withWriteKey("bar")
.andReturn(200)
Expand All @@ -127,7 +131,8 @@ class HTTPClientTest: QuickSpec {
}

it("asks to retry for 3xx response") {
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)!
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withJsonGzippedBody(batch as AnyObject)
.withWriteKey("bar")
.andReturn(304)
Expand All @@ -141,7 +146,8 @@ class HTTPClientTest: QuickSpec {
}

it("does not ask to retry for 4xx response") {
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)!
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withJsonGzippedBody(batch as AnyObject)
.withWriteKey("bar")
.andReturn(401)
Expand All @@ -155,7 +161,8 @@ class HTTPClientTest: QuickSpec {
}

it("asks to retry for 5xx response") {
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)!
_ = stubRequest("POST", "https://api.segment.io/v1/batch" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withJsonGzippedBody(batch as AnyObject)
.withWriteKey("bar")
.andReturn(504)
Expand Down Expand Up @@ -193,7 +200,8 @@ class HTTPClientTest: QuickSpec {
]

it("succeeds for 2xx response") {
_ = stubRequest("POST", "https://mobile-service.segment.com/v1/attribution" as NSString)!
_ = stubRequest("POST", "https://mobile-service.segment.com/v1/attribution" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withWriteKey("foo")
.andReturn(200)!
.withBody("{\"provider\": \"mock\"}" as NSString)
Expand All @@ -211,7 +219,8 @@ class HTTPClientTest: QuickSpec {
}

it("fails for non 2xx response") {
_ = stubRequest("POST", "https://mobile-service.segment.com/v1/attribution" as NSString)!
_ = stubRequest("POST", "https://mobile-service.segment.com/v1/attribution" as NSString)
.withHeader("User-Agent", "analytics-ios/" + SEGAnalytics.version())!
.withWriteKey("foo")
.andReturn(404)!
.withBody("not found" as NSString)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
4 changes: 2 additions & 2 deletions Examples/CocoapodsExample/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: ../../

SPEC CHECKSUMS:
Analytics: a4812262f46dde43a218c5663ca07d177f30372c
Analytics: 6541ce337e99d9f7a2240a8b3953940a7be5f998

PODFILE CHECKSUM: 20b258e875ed60a69591787e49bdc3ca9f7c4ad3

COCOAPODS: 1.4.0.rc.1
COCOAPODS: 1.3.1