Skip to content

Commit

Permalink
Merge pull request #977 from mixpanel/fix-content-type-flush
Browse files Browse the repository at this point in the history
Set content-type to application/json as required by Mixpanel ingestion API
  • Loading branch information
zihejia authored Jan 22, 2022
2 parents c1b5d7d + 0ba0068 commit 43a1383
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion HelloMixpanel/HelloMixpanelTests/AutomaticEventsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ - (void)testSession {
- (void)testUpdated {
Mixpanel *testMixpanel = [[Mixpanel alloc] initWithToken:[self randomTokenId] andFlushInterval:60];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"Mixpanel"];
NSDictionary* infoDict = [NSBundle bundleForClass:[self class]].infoDictionary;
NSDictionary* infoDict = [NSBundle mainBundle].infoDictionary;
NSString* appVersionValue = infoDict[@"CFBundleShortVersionString"];
NSString* savedVersionValue = [defaults stringForKey:@"MPAppVersion"];
XCTAssert(appVersionValue == savedVersionValue, @"saved version and current version need to be the same");
Expand Down
8 changes: 4 additions & 4 deletions HelloMixpanel/HelloMixpanelTests/MPNetworkTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ - (void)tearDown {
#pragma mark - Request Creation
- (void)testRequestForTrackEndpoint {
NSString *body = @"track test body";
NSURLRequest *request = [self.network buildPostRequestForEndpoint:MPNetworkEndpointTrack andBody:body];
NSURLRequest *request = [self.network buildPostRequestForEndpoint:MPNetworkEndpointTrack withQueryItems:nil andBody:body];
XCTAssertEqualObjects(request.URL, [NSURL URLWithString:@"http://localhost:31337/track/"]);
XCTAssert([request.HTTPMethod isEqualToString:@"POST"]);
XCTAssertEqualObjects(request.HTTPBody, [body dataUsingEncoding:NSUTF8StringEncoding]);
XCTAssert([request.allHTTPHeaderFields[@"Accept-Encoding"] isEqualToString:@"gzip"]);
XCTAssert([request.allHTTPHeaderFields[@"Content-Type"] isEqualToString:@"application/json"]);
}

- (void)testRequestForPeopleEndpoint {
NSString *body = @"engage test body";
NSURLRequest *request = [self.network buildPostRequestForEndpoint:MPNetworkEndpointEngage andBody:body];
NSURLRequest *request = [self.network buildPostRequestForEndpoint:MPNetworkEndpointEngage withQueryItems:nil andBody:body];
XCTAssertEqualObjects(request.URL, [NSURL URLWithString:@"http://localhost:31337/engage/"]);
XCTAssert([request.HTTPMethod isEqualToString:@"POST"]);
XCTAssertEqualObjects(request.HTTPBody, [body dataUsingEncoding:NSUTF8StringEncoding]);
XCTAssert([request.allHTTPHeaderFields[@"Accept-Encoding"] isEqualToString:@"gzip"]);
XCTAssert([request.allHTTPHeaderFields[@"Content-Type"] isEqualToString:@"application/json"]);
}

#pragma mark - Query Creation
Expand Down
8 changes: 4 additions & 4 deletions HelloMixpanel/HelloMixpanelTests/TestConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ static NSString *const kDefaultServerString = @"https://api.mixpanel.com";

#pragma mark - Stub Helpers
static inline LSStubRequestDSL *stubEngage() {
return stubRequest(@"POST", @"https://api.mixpanel.com/engage/".regex).withHeader(@"Accept-Encoding", @"gzip");
return stubRequest(@"POST", @"https://api.mixpanel.com/engage/".regex).withHeader(@"Content-Type", @"application/json");
}

static inline LSStubRequestDSL *stubTrack() {
return stubRequest(@"POST", @"https://api.mixpanel.com/track/".regex).withHeader(@"Accept-Encoding", @"gzip");
return stubRequest(@"POST", @"https://api.mixpanel.com/track/".regex).withHeader(@"Content-Type", @"application/json");
}

static inline LSStubRequestDSL *stubGroups() {
return stubRequest(@"POST", @"https://api.mixpanel.com/groups/".regex).withHeader(@"Accept-Encoding", @"gzip");
return stubRequest(@"POST", @"https://api.mixpanel.com/groups/".regex).withHeader(@"Content-Type", @"application/json");
}

static inline LSStubRequestDSL *stubDecide() {
return stubRequest(@"POST", @"https://api.mixpanel.com/decide(.*?)".regex).withHeader(@"Accept-Encoding", @"gzip");
return stubRequest(@"POST", @"https://api.mixpanel.com/decide(.*?)".regex).withHeader(@"Content-Type", @"application/json");
}

2 changes: 1 addition & 1 deletion Sources/AutomaticEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ - (void)initializeEvents:(MixpanelPeople *)peopleInstance {
[defaults synchronize];
}

NSDictionary* infoDict = [NSBundle bundleForClass:[self class]].infoDictionary;
NSDictionary* infoDict = [NSBundle mainBundle].infoDictionary;
if (defaults != nil && infoDict != nil) {
NSString* appVersionKey = @"MPAppVersion";
NSString* appVersionValue = infoDict[@"CFBundleShortVersionString"];
Expand Down
1 change: 1 addition & 0 deletions Sources/MPNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef NS_ENUM(NSUInteger, MPNetworkEndpoint) {
withQueryItems:(NSArray <NSURLQueryItem *> *)queryItems;

- (NSURLRequest *)buildPostRequestForEndpoint:(MPNetworkEndpoint)endpoint
withQueryItems:(NSArray <NSURLQueryItem *> *)queryItems
andBody:(NSString *)body;

@end
11 changes: 6 additions & 5 deletions Sources/MPNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ - (void)flushQueue:(NSArray *)queue endpoint:(MPNetworkEndpoint)endpoint persist
[ids addObject:entity[@"id"]];
}];

NSString *requestData = [MPJSONHandler encodedJSONString:batch];
NSString *postBody = [NSString stringWithFormat:@"ip=%d&data=%@", self.useIPAddressForGeoLocation, requestData];
MPLogDebug(@"%@ flushing %lu of %lu to %lu: %@", self, (unsigned long)batch.count, (unsigned long)queueCopyForFlushing.count, endpoint, queueCopyForFlushing);
NSURLRequest *request = [self buildPostRequestForEndpoint:endpoint andBody:postBody];
NSString *requestData = [MPJSONHandler encodedJSONString:batch];
NSURLQueryItem *useIPAddressForGeoLocation = [NSURLQueryItem queryItemWithName:@"ip" value:self.useIPAddressForGeoLocation ? @"1": @"0"];
NSURLRequest *request = [self buildPostRequestForEndpoint:endpoint withQueryItems:@[useIPAddressForGeoLocation] andBody:requestData];

[self updateNetworkActivityIndicator:YES];

Expand Down Expand Up @@ -210,11 +210,12 @@ - (NSURLRequest *)buildGetRequestForEndpoint:(MPNetworkEndpoint)endpoint
}

- (NSURLRequest *)buildPostRequestForEndpoint:(MPNetworkEndpoint)endpoint
withQueryItems:(NSArray <NSURLQueryItem *> *)queryItems
andBody:(NSString *)body
{
return [self buildRequestForEndpoint:[MPNetwork pathForEndpoint:endpoint]
byHTTPMethod:@"POST"
withQueryItems:nil
withQueryItems:queryItems
andBody:body];
}

Expand All @@ -235,7 +236,7 @@ - (NSURLRequest *)buildRequestForEndpoint:(NSString *)endpoint

// Build request from URL
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:components.URL];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:method];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];

Expand Down
13 changes: 7 additions & 6 deletions Sources/Mixpanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -1106,10 +1106,10 @@ - (NSDictionary *)collectAutomaticProperties
id deviceModel = [self deviceModel] ? : [NSNull null];

// Use setValue semantics to avoid adding keys where value can be nil.
[p setValue:[[NSBundle bundleForClass:[self class]] infoDictionary][@"CFBundleVersion"] forKey:@"$app_version"];
[p setValue:[[NSBundle bundleForClass:[self class]] infoDictionary][@"CFBundleShortVersionString"] forKey:@"$app_release"];
[p setValue:[[NSBundle bundleForClass:[self class]] infoDictionary][@"CFBundleVersion"] forKey:@"$app_build_number"];
[p setValue:[[NSBundle bundleForClass:[self class]] infoDictionary][@"CFBundleShortVersionString"] forKey:@"$app_version_string"];
[p setValue:[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"] forKey:@"$app_version"];
[p setValue:[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] forKey:@"$app_release"];
[p setValue:[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"] forKey:@"$app_build_number"];
[p setValue:[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] forKey:@"$app_version_string"];

#if !MIXPANEL_NO_REACHABILITY_SUPPORT
if (![Mixpanel isAppExtension]) {
Expand Down Expand Up @@ -1293,8 +1293,9 @@ - (void)applicationDidEnterBackground:(NSNotification *)notification
NSString *requestData = [MPJSONHandler encodedJSONString:@[@{@"event": @"Integration",
@"properties": @{@"token": @"85053bf24bba75239b16a601d9387e17", @"mp_lib": @"iphone",
@"distinct_id": self.apiToken, @"$lib_version": self.libVersion}}]];
NSString *postBody = [NSString stringWithFormat:@"ip=%d&data=%@", self.useIPAddressForGeoLocation, requestData];
NSURLRequest *request = [self.network buildPostRequestForEndpoint:MPNetworkEndpointTrack andBody:postBody];

NSURLQueryItem *useIPAddressForGeoLocation = [NSURLQueryItem queryItemWithName:@"ip" value:self.useIPAddressForGeoLocation ? @"1": @"0"];
NSURLRequest *request = [self.network buildPostRequestForEndpoint:MPNetworkEndpointTrack withQueryItems:@[useIPAddressForGeoLocation] andBody:requestData];
[[[MPNetwork sharedURLSession] dataTaskWithRequest:request completionHandler:^(NSData *responseData,
NSURLResponse *urlResponse,
NSError *error) {
Expand Down

0 comments on commit 43a1383

Please sign in to comment.