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

Move endpoint configuration to class #542

Merged
merged 1 commit into from
Apr 20, 2020
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
1 change: 1 addition & 0 deletions Bugsnag.podspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"Source/BugsnagConfiguration.h",
"Source/BugsnagDevice.h",
"Source/BugsnagDeviceWithState.h",
"Source/BugsnagEndpointConfiguration.h",
"Source/BugsnagError.h",
"Source/BugsnagEvent.h",
"Source/BugsnagMetadata.h",
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Bugsnag Notifiers on other platforms.
* Add `redactedKeys` for removing sensitive values from metadata
[#540](https://github.com/bugsnag/bugsnag-cocoa/pull/540)

* Move endpoint configuration to class
[#542](https://github.com/bugsnag/bugsnag-cocoa/pull/542)

* Make all callbacks return boolean values
[#534](https://github.com/bugsnag/bugsnag-cocoa/pull/534)

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 @@ -73,6 +73,8 @@
E762E9FD1F73F80200E82B43 /* BugsnagHandledState.m in Sources */ = {isa = PBXBuildFile; fileRef = E762E9FB1F73F80200E82B43 /* BugsnagHandledState.m */; };
E77526C0242D0E180077A42F /* BugsnagBreadcrumbs.m in Sources */ = {isa = PBXBuildFile; fileRef = E77526BE242D0E180077A42F /* BugsnagBreadcrumbs.m */; };
E77526C1242D0E180077A42F /* BugsnagBreadcrumbs.h in Headers */ = {isa = PBXBuildFile; fileRef = E77526BF242D0E180077A42F /* BugsnagBreadcrumbs.h */; };
E77AFF0F244A18B10082B8BB /* BugsnagEndpointConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = E77AFF0D244A18B10082B8BB /* BugsnagEndpointConfiguration.m */; };
E77AFF10244A18B10082B8BB /* BugsnagEndpointConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = E77AFF0E244A18B10082B8BB /* BugsnagEndpointConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
E790C42324324528006FFB26 /* BugsnagClientMirrorTest.m in Sources */ = {isa = PBXBuildFile; fileRef = E790C41F2432314A006FFB26 /* BugsnagClientMirrorTest.m */; };
E790C427243354FD006FFB26 /* BugsnagClientInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = E790C426243354FD006FFB26 /* BugsnagClientInternal.h */; };
E790C47024349CE2006FFB26 /* BugsnagStackframe.m in Sources */ = {isa = PBXBuildFile; fileRef = E790C46224349CE1006FFB26 /* BugsnagStackframe.m */; };
Expand Down Expand Up @@ -301,6 +303,8 @@
E762E9FB1F73F80200E82B43 /* BugsnagHandledState.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagHandledState.m; path = ../Source/BugsnagHandledState.m; sourceTree = SOURCE_ROOT; };
E77526BE242D0E180077A42F /* BugsnagBreadcrumbs.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagBreadcrumbs.m; path = ../Source/BugsnagBreadcrumbs.m; sourceTree = "<group>"; };
E77526BF242D0E180077A42F /* BugsnagBreadcrumbs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BugsnagBreadcrumbs.h; path = ../Source/BugsnagBreadcrumbs.h; sourceTree = "<group>"; };
E77AFF0D244A18B10082B8BB /* BugsnagEndpointConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagEndpointConfiguration.m; path = ../Source/BugsnagEndpointConfiguration.m; sourceTree = "<group>"; };
E77AFF0E244A18B10082B8BB /* BugsnagEndpointConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BugsnagEndpointConfiguration.h; path = ../Source/BugsnagEndpointConfiguration.h; sourceTree = "<group>"; };
E790C41F2432314A006FFB26 /* BugsnagClientMirrorTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BugsnagClientMirrorTest.m; sourceTree = "<group>"; };
E790C426243354FD006FFB26 /* BugsnagClientInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BugsnagClientInternal.h; path = ../Source/BugsnagClientInternal.h; sourceTree = "<group>"; };
E790C46224349CE1006FFB26 /* BugsnagStackframe.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BugsnagStackframe.m; path = ../Source/BugsnagStackframe.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -521,6 +525,8 @@
8A2C8FC21C6BC2C800846019 /* BugsnagConfiguration.m */,
E79E6AFC1F4E3847002B35F9 /* BugsnagCrashSentry.h */,
E79E6AFD1F4E3847002B35F9 /* BugsnagCrashSentry.m */,
E77AFF0E244A18B10082B8BB /* BugsnagEndpointConfiguration.h */,
E77AFF0D244A18B10082B8BB /* BugsnagEndpointConfiguration.m */,
E762E9FA1F73F80200E82B43 /* BugsnagHandledState.h */,
E762E9FB1F73F80200E82B43 /* BugsnagHandledState.m */,
E794E8041F9F746D00A67EE7 /* BugsnagKeys.h */,
Expand Down Expand Up @@ -881,6 +887,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
E77AFF10244A18B10082B8BB /* BugsnagEndpointConfiguration.h in Headers */,
E790C47C24349CE2006FFB26 /* BugsnagThread.h in Headers */,
E790C47A24349CE2006FFB26 /* BugsnagStackframe.h in Headers */,
E790C47624349CE2006FFB26 /* BugsnagError.h in Headers */,
Expand Down Expand Up @@ -1075,6 +1082,7 @@
8A2C8FDE1C6BC2C800846019 /* BugsnagSink.m in Sources */,
0089B6EB2411682000D5A7F2 /* BugsnagClient.m in Sources */,
E79E6BBE1F4E3850002B35F9 /* BSG_KSMach.c in Sources */,
E77AFF0F244A18B10082B8BB /* BugsnagEndpointConfiguration.m in Sources */,
E79E6B991F4E3850002B35F9 /* BSG_KSCrashType.c in Sources */,
E79E6BC01F4E3850002B35F9 /* BSG_KSMach_Arm.c in Sources */,
E79E6B8F1F4E3850002B35F9 /* BSG_KSCrashDoctor.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Source/Bugsnag.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import "BugsnagAppWithState.h"
#import "BugsnagDevice.h"
#import "BugsnagDeviceWithState.h"
#import "BugsnagEndpointConfiguration.h"
#import "BugsnagError.h"
#import "BugsnagStackframe.h"
#import "BugsnagThread.h"
Expand Down
10 changes: 2 additions & 8 deletions Source/BugsnagConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import "BugsnagMetadataStore.h"

@class BugsnagUser;
@class BugsnagEndpointConfiguration;

/**
* BugsnagConfiguration error constants
Expand Down Expand Up @@ -204,15 +205,8 @@ typedef NS_OPTIONS(NSUInteger, BSGErrorType) {
* Please note that it is recommended that you set both endpoints. If the notify endpoint is
* missing, an assertion will be thrown. If the session endpoint is missing, a warning will be
* logged and sessions will not be sent automatically.
*
* @param notify the notify endpoint
* @param sessions the sessions endpoint
*
* @throws an assertion if the notify endpoint is not a valid URL
*/

- (void)setEndpointsForNotify:(NSString *_Nonnull)notify
sessions:(NSString *_Nonnull)sessions NS_SWIFT_NAME(setEndpoints(notify:sessions:));
@property(nonnull, nonatomic) BugsnagEndpointConfiguration *endpoints;

// =============================================================================
// MARK: - User
Expand Down
12 changes: 7 additions & 5 deletions Source/BugsnagConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#import "BugsnagBreadcrumbs.h"
#import "BugsnagMetadataStore.h"
#import "BSGSerialization.h"
#import "BugsnagEndpointConfiguration.h"

static NSString *const kHeaderApiPayloadVersion = @"Bugsnag-Payload-Version";
static NSString *const kHeaderApiKey = @"Bugsnag-Api-Key";
Expand Down Expand Up @@ -124,8 +125,7 @@ - (nonnull id)copyWithZone:(nullable NSZone *)zone {
[copy setRedactedKeys:self.redactedKeys];
[copy setMaxBreadcrumbs:self.maxBreadcrumbs];
[copy setMetadata: [[BugsnagMetadata alloc] initWithDictionary:[[self.metadata toDictionary] mutableCopy]]];
[copy setEndpointsForNotify:self.notifyURL.absoluteString
sessions:self.sessionURL.absoluteString];
[copy setEndpoints:self.endpoints];
[copy setOnBreadcrumbBlocks:[self.onBreadcrumbBlocks mutableCopy]];
[copy setOnCrashHandler:self.onCrashHandler];
[copy setOnSendBlocks:[self.onSendBlocks mutableCopy]];
Expand Down Expand Up @@ -184,6 +184,7 @@ - (instancetype _Nonnull)initWithApiKey:(NSString *_Nonnull)apiKey
_metadata = [[BugsnagMetadata alloc] init];
_config = [[BugsnagMetadata alloc] init];
_apiKey = apiKey;
_endpoints = [BugsnagEndpointConfiguration new];
_sessionURL = [NSURL URLWithString:@"https://sessions.bugsnag.com"];
_autoDetectErrors = YES;
_notifyURL = [NSURL URLWithString:BSGDefaultNotifyUrl];
Expand Down Expand Up @@ -326,9 +327,10 @@ - (NSDictionary *)sessionApiHeaders {
};
}

- (void)setEndpointsForNotify:(NSString *_Nonnull)notify sessions:(NSString *_Nonnull)sessions {
_notifyURL = [NSURL URLWithString:notify];
_sessionURL = [NSURL URLWithString:sessions];
- (void)setEndpoints:(BugsnagEndpointConfiguration *)endpoints {
_endpoints = endpoints;
_notifyURL = [NSURL URLWithString:endpoints.notify];
_sessionURL = [NSURL URLWithString:endpoints.sessions];

NSAssert([self isValidUrl:_notifyURL], @"Invalid URL supplied for notify endpoint");

Expand Down
34 changes: 34 additions & 0 deletions Source/BugsnagEndpointConfiguration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// BugsnagEndpointConfiguration.h
// Bugsnag
//
// Created by Jamie Lynch on 15/04/2020.
// Copyright © 2020 Bugsnag. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
* Set the endpoints to send data to. By default we'll send error reports to
* https://notify.bugsnag.com, and sessions to https://sessions.bugsnag.com, but you can
* override this if you are using Bugsnag Enterprise to point to your own Bugsnag endpoints.
*/
@interface BugsnagEndpointConfiguration : NSObject

/**
* Configures the endpoint to which events should be sent
*/
@property NSString *notify;

/**
* Configures the endpoint to which sessions should be sent
*/
@property NSString *sessions;

- (instancetype)initWithNotify:(NSString *)notify sessions:(NSString *)sessions;

@end

NS_ASSUME_NONNULL_END
28 changes: 28 additions & 0 deletions Source/BugsnagEndpointConfiguration.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// BugsnagEndpointConfiguration.m
// Bugsnag
//
// Created by Jamie Lynch on 15/04/2020.
// Copyright © 2020 Bugsnag. All rights reserved.
//

#import "BugsnagEndpointConfiguration.h"

@implementation BugsnagEndpointConfiguration

- (instancetype)init {
if (self = [super init]) {
_notify = @"https://notify.bugsnag.com";
_sessions = @"https://sessions.bugsnag.com";
}
return self;
}

- (instancetype)initWithNotify:(NSString *)notify sessions:(NSString *)sessions {
if (self = [super init]) {
_notify = notify;
_sessions = sessions;
}
return self;
}
@end
43 changes: 29 additions & 14 deletions Tests/BugsnagConfigurationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ - (void)testSessionEndpoints {
XCTAssertEqualObjects([NSURL URLWithString:@"https://sessions.bugsnag.com"], config.sessionURL);

// Test overriding the session endpoint (use dummy endpoints to avoid hitting production)
[config setEndpointsForNotify:@"http://localhost:1234" sessions:@"http://localhost:8000"];

config.endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://localhost:1234"
sessions:@"http://localhost:8000"];
XCTAssertEqualObjects([NSURL URLWithString:@"http://localhost:8000"], config.sessionURL);
}

- (void)testSetEmptySessionsEndpoint {
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
[config setEndpointsForNotify:@"http://notify.example.com" sessions:@""];
config.endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://notify.example.com"
sessions:@""];
BugsnagSessionTracker *sessionTracker
= [[BugsnagSessionTracker alloc] initWithConfig:config postRecordCallback:nil];

Expand All @@ -89,7 +92,8 @@ - (void)testSetEmptySessionsEndpoint {

- (void)testSetMalformedSessionsEndpoint {
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
[config setEndpointsForNotify:@"http://notify.example.com" sessions:@"f"];
config.endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://notify.example.com"
sessions:@"f"];
BugsnagSessionTracker *sessionTracker
= [[BugsnagSessionTracker alloc] initWithConfig:config postRecordCallback:nil];

Expand All @@ -106,7 +110,8 @@ - (void)testAddOnSessionBlock {
// Setup
__block XCTestExpectation *expectation = [self expectationWithDescription:@"Remove On Session Block"];
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
[config setEndpointsForNotify:@"http://notreal.bugsnag.com" sessions:@"http://notreal.bugsnag.com"];
config.endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://notreal.bugsnag.com"
sessions:@"http://notreal.bugsnag.com"];
XCTAssertEqual([[config onSessionBlocks] count], 0);
BugsnagOnSessionBlock sessionBlock = ^BOOL(NSMutableDictionary * _Nonnull sessionPayload) {
// We expect the session block to be called
Expand All @@ -131,7 +136,8 @@ - (void)testRemoveOnSessionBlock {
calledExpectation.inverted = YES;

BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
[config setEndpointsForNotify:@"http://notreal.bugsnag.com" sessions:@"http://notreal.bugsnag.com"];
config.endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://notreal.bugsnag.com"
sessions:@"http://notreal.bugsnag.com"];
XCTAssertEqual([[config onSessionBlocks] count], 0);
BugsnagOnSessionBlock sessionBlock = ^BOOL(NSMutableDictionary * _Nonnull sessionPayload) {
[calledExpectation fulfill];
Expand Down Expand Up @@ -165,7 +171,8 @@ - (void)testAddOnSessionBlockThenRemove {
expectation4.inverted = YES;

BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
[config setEndpointsForNotify:@"http://notreal.bugsnag.com" sessions:@"http://notreal.bugsnag.com"];
config.endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://notreal.bugsnag.com"
sessions:@"http://notreal.bugsnag.com"];
XCTAssertEqual([[config onSessionBlocks] count], 0);

BugsnagOnSessionBlock sessionBlock = ^BOOL(NSMutableDictionary * _Nonnull sessionPayload) {
Expand Down Expand Up @@ -306,13 +313,15 @@ - (void)testNotifyEndpoint {
XCTAssertEqualObjects([NSURL URLWithString:@"https://notify.bugsnag.com/"], config.notifyURL);

// Test overriding the notify endpoint (use dummy endpoints to avoid hitting production)
[config setEndpointsForNotify:@"http://localhost:1234" sessions:@"http://localhost:8000"];
config.endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://localhost:1234"
sessions:@"http://localhost:8000"];
XCTAssertEqualObjects([NSURL URLWithString:@"http://localhost:1234"], config.notifyURL);
}

- (void)testSetEndpoints {
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
[config setEndpointsForNotify:@"http://notify.example.com" sessions:@"http://sessions.example.com"];
config.endpoints = [[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://notify.example.com"
sessions:@"http://sessions.example.com"];
XCTAssertEqualObjects([NSURL URLWithString:@"http://notify.example.com"], config.notifyURL);
XCTAssertEqualObjects([NSURL URLWithString:@"http://sessions.example.com"], config.sessionURL);
}
Expand All @@ -323,32 +332,38 @@ - (void)testSetNilNotifyEndpoint {
NSString *notify = @"foo";
notify = nil;
#if DEBUG
XCTAssertThrowsSpecificNamed([config setEndpointsForNotify:notify sessions:@"http://sessions.example.com"],
XCTAssertThrowsSpecificNamed([config setEndpoints:[[BugsnagEndpointConfiguration alloc] initWithNotify:notify
sessions:@"http://sessions.example.com"]],
NSException, NSInternalInconsistencyException);
#else
XCTAssertNoThrow([config setEndpointsForNotify:@"" sessions:@"http://sessions.example.com"]);
XCTAssertNoThrow([config setEndpoints:[[BugsnagEndpointConfiguration alloc] initWithNotify:@""
sessions:@"http://sessions.example.com"]]);
#endif
}

// in debug these throw exceptions though in release are "tolerated"
- (void)testSetEmptyNotifyEndpoint {
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
#if DEBUG
XCTAssertThrowsSpecificNamed([config setEndpointsForNotify:@"" sessions:@"http://sessions.example.com"],
XCTAssertThrowsSpecificNamed([config setEndpoints:[[BugsnagEndpointConfiguration alloc] initWithNotify:@""
sessions:@"http://sessions.example.com"]],
NSException, NSInternalInconsistencyException);
#else
XCTAssertNoThrow([config setEndpointsForNotify:@"" sessions:@"http://sessions.example.com"]);
XCTAssertNoThrow([config setEndpoints:[[BugsnagEndpointConfiguration alloc] initWithNotify:@""
sessions:@"http://sessions.example.com"]]);
#endif
}

// in debug these throw exceptions though in release are "tolerated"
- (void)testSetMalformedNotifyEndpoint {
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
#if DEBUG
XCTAssertThrowsSpecificNamed([config setEndpointsForNotify:@"http://" sessions:@"http://sessions.example.com"],
XCTAssertThrowsSpecificNamed([config setEndpoints:[[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://"
sessions:@"http://sessions.example.com"]],
NSException, NSInternalInconsistencyException);
#else
XCTAssertNoThrow([config setEndpointsForNotify:@"http://" sessions:@"http://sessions.example.com"]);
XCTAssertNoThrow([config setEndpoints:[[BugsnagEndpointConfiguration alloc] initWithNotify:@"http://"
sessions:@"http://sessions.example.com"]]);
#endif
}

Expand Down
Loading