Skip to content

Commit

Permalink
chore: added eventsApiProxyURL option for proxying to a different url…
Browse files Browse the repository at this point in the history
… than the sdk config (#194)

* chore: added eventsApiProxyURL option for proxying to a different url than the sdk config

* chore: added tests
  • Loading branch information
jsalaber authored Apr 9, 2024
1 parent 7402bbf commit 72afc52
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
7 changes: 7 additions & 0 deletions DevCycle/Models/DevCycleOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DevCycleOptions {
var disableAutomaticEventLogging: Bool = false
var disableCustomEventLogging: Bool = false
var apiProxyURL: String?
var eventsApiProxyURL: String?

public class OptionsBuilder {
var options: DevCycleOptions
Expand Down Expand Up @@ -90,6 +91,12 @@ public class DevCycleOptions {
return self
}

// Allows the SDK to use a proxy to send to for the events API
public func eventsApiProxyURL(_ proxyURL: String) -> OptionsBuilder {
self.options.eventsApiProxyURL = proxyURL
return self
}

public func build() -> DevCycleOptions {
let result = self.options
self.options = DevCycleOptions()
Expand Down
2 changes: 1 addition & 1 deletion DevCycle/Networking/DevCycleService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class DevCycleService: DevCycleServiceProtocol {

switch(type) {
case "event":
if let proxyUrl = self.options?.apiProxyURL {
if let proxyUrl = self.options?.eventsApiProxyURL {
url = proxyUrl
} else {
url = NetworkingConstants.eventsUrl + NetworkingConstants.hostUrl
Expand Down
2 changes: 2 additions & 0 deletions DevCycleTests/Models/DevCycleOptionsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class DevCycleOptionsTest: XCTestCase {
.disableCustomEventLogging(true)
.disableAutomaticEventLogging(true)
.apiProxyURL("localhost:4000")
.eventsApiProxyURL("localhost:4001")
.build()
XCTAssertNotNil(options)
XCTAssert(options.eventFlushIntervalMS == 1000)
Expand All @@ -35,6 +36,7 @@ class DevCycleOptionsTest: XCTestCase {
XCTAssert(options.disableCustomEventLogging)
XCTAssert(options.disableAutomaticEventLogging)
XCTAssert(options.apiProxyURL == "localhost:4000")
XCTAssert(options.eventsApiProxyURL == "localhost:4001")
}

func testBuilderReturnsOptionsAndSomeAreNil() {
Expand Down
14 changes: 11 additions & 3 deletions DevCycleTests/Networking/DevCycleServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ class DevCycleServiceTests: XCTestCase {

func testProxyConfigURL() throws {
let options = DevCycleOptions.builder().apiProxyURL("localhost:4000").build()
let url = getService(options).createConfigRequest(user: getTestUser(), enableEdgeDB: false).url?.absoluteString
let service = getService(options)
let url = service.createConfigRequest(user: getTestUser(), enableEdgeDB: false).url?.absoluteString
let eventsUrl = service.createEventsRequest().url?.absoluteString

XCTAssert(url!.contains("localhost:4000/v1/mobileSDKConfig"))
XCTAssert(url!.contains("sdkKey=my_sdk_key"))
XCTAssert(url!.contains("user_id=my_user"))
XCTAssertFalse(eventsUrl!.contains("localhost:4000"))
}

func testCreateConfigURLRequestWithEdgeDB() throws {
Expand All @@ -38,9 +42,13 @@ class DevCycleServiceTests: XCTestCase {
}

func testProxyEventUrl() throws {
let options = DevCycleOptions.builder().apiProxyURL("localhost:4000").build()
let url = getService(options).createEventsRequest().url?.absoluteString
let options = DevCycleOptions.builder().eventsApiProxyURL("localhost:4000").build()
let service = getService(options)
let url = service.createEventsRequest().url?.absoluteString
let apiUrl = service.createConfigRequest(user: getTestUser(), enableEdgeDB: false).url?.absoluteString

XCTAssert(url!.contains("localhost:4000/v1/events"))
XCTAssertFalse(apiUrl!.contains("localhost:4000"))
XCTAssertFalse(url!.contains("user_id=my_user"))
}

Expand Down

0 comments on commit 72afc52

Please sign in to comment.