Skip to content

Commit

Permalink
Add URL session as configuration property (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
kattrali authored Jan 12, 2017
2 parents 8fd9e61 + 4308d19 commit dba4487
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 13 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

## TBD

### Enhancements

* Add support for customizing the `NSURLSession` used when sending error reports
to Bugsnag
[#127](https://github.com/bugsnag/bugsnag-cocoa/pull/127)

## 5.6.5 (7 Nov 2016)

### Bug fixes
Expand Down
8 changes: 8 additions & 0 deletions OSX/Bugsnag.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
8A7630E21D2523AE000D6737 /* Kiwi.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A7630E11D2523AE000D6737 /* Kiwi.framework */; };
8A7630E51D2524DB000D6737 /* BugsnagSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 8A7630DF1D252309000D6737 /* BugsnagSpec.m */; };
8A87352C1C6D3B1600EDBD5B /* BSGKSCrashReportWriter.h in Headers */ = {isa = PBXBuildFile; fileRef = 8A87352B1C6D3B1600EDBD5B /* BSGKSCrashReportWriter.h */; settings = {ATTRIBUTES = (Public, ); }; };
8AD9FA881E08633F002859A7 /* BugsnagConfigurationSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AD9FA841E0862DC002859A7 /* BugsnagConfigurationSpec.m */; };
8AD9FA891E086351002859A7 /* BugsnagConfigurationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AD9FA851E0862DC002859A7 /* BugsnagConfigurationTests.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -81,6 +83,8 @@
8A7630DF1D252309000D6737 /* BugsnagSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagSpec.m; path = ../Tests/BugsnagSpec.m; sourceTree = SOURCE_ROOT; };
8A7630E11D2523AE000D6737 /* Kiwi.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Kiwi.framework; path = "../../../../../Library/Developer/Xcode/DerivedData/OSX-fywomumlxcstijawyihsvjfwghcm/Build/Products/Debug/Kiwi.framework"; sourceTree = "<group>"; };
8A87352B1C6D3B1600EDBD5B /* BSGKSCrashReportWriter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BSGKSCrashReportWriter.h; path = ../Source/BSGKSCrashReportWriter.h; sourceTree = SOURCE_ROOT; };
8AD9FA841E0862DC002859A7 /* BugsnagConfigurationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagConfigurationSpec.m; path = ../Tests/BugsnagConfigurationSpec.m; sourceTree = SOURCE_ROOT; };
8AD9FA851E0862DC002859A7 /* BugsnagConfigurationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagConfigurationTests.m; path = ../Tests/BugsnagConfigurationTests.m; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -154,6 +158,8 @@
8A2C8FAF1C6BC1F700846019 /* Tests */ = {
isa = PBXGroup;
children = (
8AD9FA841E0862DC002859A7 /* BugsnagConfigurationSpec.m */,
8AD9FA851E0862DC002859A7 /* BugsnagConfigurationTests.m */,
8A2C8FE01C6BC38200846019 /* BugsnagBreadcrumbsTest.m */,
8A2C8FE11C6BC38200846019 /* BugsnagCrashReportTests.m */,
8A7630DF1D252309000D6737 /* BugsnagSpec.m */,
Expand Down Expand Up @@ -310,9 +316,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8AD9FA881E08633F002859A7 /* BugsnagConfigurationSpec.m in Sources */,
8A2C8FEA1C6BC38900846019 /* BugsnagBreadcrumbsTest.m in Sources */,
8A2C8FEC1C6BC38900846019 /* BugsnagSinkTests.m in Sources */,
8A7630E51D2524DB000D6737 /* BugsnagSpec.m in Sources */,
8AD9FA891E086351002859A7 /* BugsnagConfigurationTests.m in Sources */,
8A2C8FEB1C6BC38900846019 /* BugsnagCrashReportTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
5 changes: 5 additions & 0 deletions Source/BugsnagConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ typedef NSDictionary *_Nullable (^BugsnagBeforeNotifyHook)(
*/
@property(nonatomic, readwrite, retain, nullable) NSString *appVersion;

/**
* The URL session used to send requests to Bugsnag.
*/
@property(nonatomic, readwrite, strong, nonnull) NSURLSession *session;

/**
* Additional information about the state of the app or environment at the
* time the report was generated
Expand Down
3 changes: 3 additions & 0 deletions Source/BugsnagConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ - (id)init {
_notifyReleaseStages = nil;
_breadcrumbs = [BugsnagBreadcrumbs new];
_automaticallyCollectBreadcrumbs = YES;
if ([NSURLSession class]) {
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}
#if DEBUG
_releaseStage = @"development";
#else
Expand Down
16 changes: 5 additions & 11 deletions Source/BugsnagSink.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,9 @@ @interface Bugsnag ()
+ (BugsnagNotifier*)notifier;
@end

@interface BugsnagSink ()
@property (nonatomic, strong) NSURLSession *session;
@end

@implementation BugsnagSink

- (instancetype)init {
if (self = [super init])
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

return self;
}

// Entry point called by KSCrash when a report needs to be sent. Handles report filtering based on the configuration
// options for `notifyReleaseStages`.
// Removes all reports not meeting at least one of the following conditions:
Expand Down Expand Up @@ -132,7 +122,11 @@ - (void)sendReports:(NSArray <BugsnagCrashReport *>*)reports


if ([NSURLSession class]) {
NSURLSessionTask *task = [self.session uploadTaskWithRequest:request fromData:jsonData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSURLSession *session = [Bugsnag configuration].session;
if (!session) {
session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}
NSURLSessionTask *task = [session uploadTaskWithRequest:request fromData:jsonData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (onCompletion)
onCompletion(reports, error == nil, error);
}];
Expand Down
43 changes: 43 additions & 0 deletions Tests/BugsnagConfigurationSpec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// BugsnagConfigurationSpec.m
// Bugsnag
//
// Created by Delisa Mason on 11/30/16.
// Copyright 2016 Bugsnag. All rights reserved.
//

#import <Kiwi/Kiwi.h>
#import "BugsnagConfiguration.h"
#import "Bugsnag.h"

@interface SomeDelegate : NSObject<NSURLSessionTaskDelegate>
@property (nonatomic) BOOL didInvoke;
@end

@implementation SomeDelegate

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
self.didInvoke = YES;
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}

@end


SPEC_BEGIN(BugsnagConfigurationSpec)

describe(@"BugsnagConfiguration", ^{

it(@"sets the request session", ^{
SomeDelegate *delegate = [SomeDelegate new];
BugsnagConfiguration *config = [BugsnagConfiguration new];
config.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:delegate
delegateQueue:[NSOperationQueue mainQueue]];
[Bugsnag startBugsnagWithConfiguration:config];
[Bugsnag notify:[NSException exceptionWithName:@"oh no" reason:nil userInfo:nil]];
[[expectFutureValue(@(delegate.didInvoke)) shouldEventually] beYes];
});
});

SPEC_END
6 changes: 6 additions & 0 deletions Tests/BugsnagConfigurationTests.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#import <XCTest/XCTest.h>
#import "BugsnagConfiguration.h"
#import "Bugsnag.h"


@interface BugsnagConfigurationTests : XCTestCase
@end

@implementation BugsnagConfigurationTests

- (void)testDefaultSessionNotNil {
BugsnagConfiguration *config = [BugsnagConfiguration new];
XCTAssertNotNil(config.session);
}

- (void)testNotifyReleaseStagesDefaultSends {
BugsnagConfiguration *config = [BugsnagConfiguration new];
XCTAssertTrue([config shouldSendReports]);
Expand Down
3 changes: 1 addition & 2 deletions Tests/BugsnagSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ @implementation BugsnagTestError

beforeEach(^{
request = nil;
BugsnagSink *sink = [[KSCrash sharedInstance] valueForKeyPath:@"sink"];
NSURLSession *session = [sink valueForKeyPath:@"session"];
NSURLSession *session = [Bugsnag configuration].session;
[session stub:@selector(uploadTaskWithRequest:fromData:completionHandler:) withBlock:^id(NSArray *params) {
request = [params firstObject];
httpBody = params[1];
Expand Down
4 changes: 4 additions & 0 deletions iOS/Bugsnag.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
8A87351E1C6D2ADE00EDBD5B /* BSGKSCrashReportWriter.h in Headers */ = {isa = PBXBuildFile; fileRef = 8A87351D1C6D2ADE00EDBD5B /* BSGKSCrashReportWriter.h */; settings = {ATTRIBUTES = (Public, ); }; };
8AAA64011D2407CE00A9A123 /* Kiwi.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8AAA64001D2407CE00A9A123 /* Kiwi.framework */; };
8AAA64081D24088600A9A123 /* BugsnagSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AAA64071D24088600A9A123 /* BugsnagSpec.m */; };
8AE1BC951DEFCE8B00D16CEF /* BugsnagConfigurationSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AE1BC941DEFCE8B00D16CEF /* BugsnagConfigurationSpec.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -85,6 +86,7 @@
8AAA63FE1D2404BB00A9A123 /* Nocilla.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nocilla.framework; path = "../development/Nocilla/build/Debug-iphoneos/Nocilla.framework"; sourceTree = "<group>"; };
8AAA64001D2407CE00A9A123 /* Kiwi.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Kiwi.framework; path = "../development/Kiwi/build/Debug-iphoneos/Kiwi.framework"; sourceTree = "<group>"; };
8AAA64071D24088600A9A123 /* BugsnagSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagSpec.m; path = ../Tests/BugsnagSpec.m; sourceTree = SOURCE_ROOT; };
8AE1BC941DEFCE8B00D16CEF /* BugsnagConfigurationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagConfigurationSpec.m; path = ../Tests/BugsnagConfigurationSpec.m; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -161,6 +163,7 @@
children = (
8A2C8F8B1C6BBFDD00846019 /* BugsnagBreadcrumbsTest.m */,
8A2C8F8C1C6BBFDD00846019 /* BugsnagCrashReportTests.m */,
8AE1BC941DEFCE8B00D16CEF /* BugsnagConfigurationSpec.m */,
8A4E733E1DC13281001F7CC8 /* BugsnagConfigurationTests.m */,
8AAA64071D24088600A9A123 /* BugsnagSpec.m */,
8A2C8F8D1C6BBFDD00846019 /* BugsnagSinkTests.m */,
Expand Down Expand Up @@ -318,6 +321,7 @@
buildActionMask = 2147483647;
files = (
8A2C8F8F1C6BBFDD00846019 /* BugsnagBreadcrumbsTest.m in Sources */,
8AE1BC951DEFCE8B00D16CEF /* BugsnagConfigurationSpec.m in Sources */,
8A2C8F911C6BBFDD00846019 /* BugsnagSinkTests.m in Sources */,
8AAA64081D24088600A9A123 /* BugsnagSpec.m in Sources */,
8A2C8F901C6BBFDD00846019 /* BugsnagCrashReportTests.m in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions tvOS/Bugsnag.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
8AD9A5031D42EEB0004E1CC5 /* BugsnagNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AB151331D41366400C9B218 /* BugsnagNotifier.m */; };
8AD9A5041D42EEB0004E1CC5 /* BugsnagSink.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AB151351D41366400C9B218 /* BugsnagSink.m */; };
8AD9A5051D42EEE9004E1CC5 /* BSGKSCrashReportWriter.h in Headers */ = {isa = PBXBuildFile; fileRef = 8AB151251D41366400C9B218 /* BSGKSCrashReportWriter.h */; settings = {ATTRIBUTES = (Public, ); }; };
8AD9FA8D1E0863A1002859A7 /* BugsnagConfigurationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8AD9FA8B1E0863A1002859A7 /* BugsnagConfigurationTests.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -69,6 +70,8 @@
8AB151341D41366400C9B218 /* BugsnagSink.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BugsnagSink.h; path = ../Source/BugsnagSink.h; sourceTree = "<group>"; };
8AB151351D41366400C9B218 /* BugsnagSink.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagSink.m; path = ../Source/BugsnagSink.m; sourceTree = "<group>"; };
8AB1513E1D4136F100C9B218 /* KSCrash.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = KSCrash.framework; path = "../../../../Library/Developer/Xcode/DerivedData/tvOS-ebsbvpxsfrmszpejtqlahdcmtszc/Build/Products/Debug-appletvsimulator/KSCrash.framework"; sourceTree = "<group>"; };
8AD9FA8A1E0863A1002859A7 /* BugsnagConfigurationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagConfigurationSpec.m; path = ../Tests/BugsnagConfigurationSpec.m; sourceTree = SOURCE_ROOT; };
8AD9FA8B1E0863A1002859A7 /* BugsnagConfigurationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagConfigurationTests.m; path = ../Tests/BugsnagConfigurationTests.m; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -138,6 +141,8 @@
8AB151131D41356800C9B218 /* Tests */ = {
isa = PBXGroup;
children = (
8AD9FA8A1E0863A1002859A7 /* BugsnagConfigurationSpec.m */,
8AD9FA8B1E0863A1002859A7 /* BugsnagConfigurationTests.m */,
8AB1511D1D41361700C9B218 /* BugsnagBreadcrumbsTest.m */,
8AB1511E1D41361700C9B218 /* BugsnagCrashReportTests.m */,
8AB1511F1D41361700C9B218 /* BugsnagSinkTests.m */,
Expand Down Expand Up @@ -289,6 +294,7 @@
8AB151231D41361700C9B218 /* BugsnagSinkTests.m in Sources */,
8AB151211D41361700C9B218 /* BugsnagBreadcrumbsTest.m in Sources */,
8AB151221D41361700C9B218 /* BugsnagCrashReportTests.m in Sources */,
8AD9FA8D1E0863A1002859A7 /* BugsnagConfigurationTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit dba4487

Please sign in to comment.