Skip to content

Commit

Permalink
Storekittest headers (#407)
Browse files Browse the repository at this point in the history
* added new headers for storekitTest receipts

* added build version header

* reverted accidental changes

* removed X-Is-StoreKitTest-Receipt from headers since that will now be auto-determined by the backend

* cleaned up RCHTTPClient, added unit tests

* added watchOS compatibility by using the idfv from WKInterfaceDevice

* made idfv static since it doesn't have to be an instance property

* removed unnecessary import
  • Loading branch information
aboedo committed Dec 1, 2020
1 parent 3fb05c0 commit 71e9113
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Purchases/Misc/RCCrossPlatformSupport.h
Expand Up @@ -46,6 +46,15 @@
#define UI_DEVICE_AVAILABLE 0
#endif

// Should match available platforms in
// https://developer.apple.com/documentation/watchkit/wkinterfacedevice?language=objc
#if TARGET_OS_WATCH
#define WKINTERFACE_DEVICE_AVAILABLE 1
#else
#define WKINTERFACE_DEVICE_AVAILABLE 0
#endif


// Should match available platforms in
// https://developer.apple.com/documentation/iad/adclient?language=objc
#if TARGET_OS_IOS
Expand Down
2 changes: 2 additions & 0 deletions Purchases/Misc/RCSystemInfo.h
Expand Up @@ -26,7 +26,9 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSString *)frameworkVersion;
+ (NSString *)systemVersion;
+ (NSString *)appVersion;
+ (NSString *)buildVersion;
+ (NSString *)platformHeader;
+ (nullable NSString *)identifierForVendor;

+ (NSURL *)serverHostURL;
+ (nullable NSURL *)proxyURL;
Expand Down
14 changes: 14 additions & 0 deletions Purchases/Misc/RCSystemInfo.m
Expand Up @@ -62,10 +62,24 @@ + (NSString *)appVersion {
return version ?: @"";
}

+ (NSString *)buildVersion {
NSString *version = NSBundle.mainBundle.infoDictionary[@"CFBundleVersion"];
return version ?: @"";
}

+ (NSString *)platformHeader {
return self.forceUniversalAppStore ? @"iOS" : PLATFORM_HEADER;
}

+ (nullable NSString *)identifierForVendor {
#if UI_DEVICE_AVAILABLE
return UIDevice.currentDevice.identifierForVendor.UUIDString;
#elif WKINTERFACE_DEVICE_AVAILABLE
return WKInterfaceDevice.currentDevice.identifierForVendor.UUIDString;
#endif
return nil;
}

+ (NSURL *)defaultServerHostURL {
return [NSURL URLWithString:defaultServerHostName];
}
Expand Down
6 changes: 6 additions & 0 deletions Purchases/Networking/RCHTTPClient.m
Expand Up @@ -201,6 +201,7 @@ - (NSDictionary *)defaultHeaders {
@"X-Platform-Version": RCSystemInfo.systemVersion,
@"X-Platform-Flavor": self.systemInfo.platformFlavor,
@"X-Client-Version": RCSystemInfo.appVersion,
@"X-Client-Build-Version": RCSystemInfo.buildVersion,
@"X-Observer-Mode-Enabled": observerMode
}];

Expand All @@ -209,6 +210,11 @@ - (NSDictionary *)defaultHeaders {
headers[@"X-Platform-Flavor-Version"] = platformFlavorVersion;
}

NSString * _Nullable idfv = RCSystemInfo.identifierForVendor;
if (idfv) {
headers[@"X-Apple-Device-Identifier"] = idfv;
}

return headers;
}

Expand Down
34 changes: 34 additions & 0 deletions PurchasesTests/Networking/HTTPClientTests.swift
Expand Up @@ -319,6 +319,40 @@ class HTTPClientTests: XCTestCase {
expect(headerPresent).toEventually(equal(true))
}

func testAlwaysPassesClientBuildVersion() {
let path = "/a_random_path"
var headerPresent = false

let version = Bundle.main.infoDictionary!["CFBundleVersion"] as! String

stub(condition: hasHeaderNamed("X-Client-Build-Version", value: version )) { request in
headerPresent = true
return HTTPStubsResponse(data: Data.init(), statusCode:200, headers:nil)
}

self.client.performRequest("POST", serially: true, path: path, body: Dictionary.init(),
headers: ["test_header": "value"], completionHandler:nil)

expect(headerPresent).toEventually(equal(true))
}

func testAlwaysPassesAppleDeviceIdentifier() {
let path = "/a_random_path"
var headerPresent = false

let idfv = UIDevice.current.identifierForVendor!.uuidString

stub(condition: hasHeaderNamed("X-Apple-Device-Identifier", value: idfv )) { request in
headerPresent = true
return HTTPStubsResponse(data: Data.init(), statusCode:200, headers:nil)
}

self.client.performRequest("POST", serially: true, path: path, body: Dictionary.init(),
headers: ["test_header": "value"], completionHandler:nil)

expect(headerPresent).toEventually(equal(true))
}

func testDefaultsPlatformFlavorToNative() {
let path = "/a_random_path"
var headerPresent = false
Expand Down

0 comments on commit 71e9113

Please sign in to comment.