@@ -3,9 +3,8 @@ import XCTest
33
44final class LDSwiftEventSourceTests : XCTestCase {
55 func testConfigDefaults( ) {
6- let handler = MockHandler ( )
76 let url = URL ( string: " abc " ) !
8- let config = EventSource . Config ( handler: handler , url: url)
7+ let config = EventSource . Config ( handler: MockHandler ( ) , url: url)
98 XCTAssertEqual ( config. url, url)
109 XCTAssertEqual ( config. method, " GET " )
1110 XCTAssertEqual ( config. body, nil )
@@ -15,12 +14,13 @@ final class LDSwiftEventSourceTests: XCTestCase {
1514 XCTAssertEqual ( config. maxReconnectTime, 30.0 )
1615 XCTAssertEqual ( config. backoffResetThreshold, 60.0 )
1716 XCTAssertEqual ( config. idleTimeout, 300.0 )
17+ XCTAssertEqual ( config. headerTransform ( [ " abc " : " 123 " ] ) , [ " abc " : " 123 " ] )
18+ XCTAssertEqual ( config. connectionErrorHandler ( DummyError ( ) ) , . proceed)
1819 }
1920
2021 func testConfigModification( ) {
21- let handler = MockHandler ( )
2222 let url = URL ( string: " abc " ) !
23- var config = EventSource . Config ( handler: handler , url: url)
23+ var config = EventSource . Config ( handler: MockHandler ( ) , url: url)
2424
2525 let testBody = " test data " . data ( using: . utf8)
2626 let testHeaders = [ " Authorization " : " basic abc " ]
@@ -29,11 +29,12 @@ final class LDSwiftEventSourceTests: XCTestCase {
2929 config. body = testBody
3030 config. lastEventId = " eventId "
3131 config. headers = testHeaders
32- config. headerTransform = { _ in [ : ] }
3332 config. reconnectTime = 2.0
3433 config. maxReconnectTime = 60.0
3534 config. backoffResetThreshold = 120.0
3635 config. idleTimeout = 180.0
36+ config. headerTransform = { _ in [ : ] }
37+ config. connectionErrorHandler = { _ in . shutdown }
3738
3839 XCTAssertEqual ( config. url, url)
3940 XCTAssertEqual ( config. method, " REPORT " )
@@ -45,22 +46,129 @@ final class LDSwiftEventSourceTests: XCTestCase {
4546 XCTAssertEqual ( config. maxReconnectTime, 60.0 )
4647 XCTAssertEqual ( config. backoffResetThreshold, 120.0 )
4748 XCTAssertEqual ( config. idleTimeout, 180.0 )
49+ XCTAssertEqual ( config. connectionErrorHandler ( DummyError ( ) ) , . shutdown)
50+ }
51+
52+ func testLastEventIdFromConfig( ) {
53+ var config = EventSource . Config ( handler: MockHandler ( ) , url: URL ( string: " abc " ) !)
54+ var es = EventSource ( config: config)
55+ XCTAssertEqual ( es. getLastEventId ( ) , nil )
56+ config. lastEventId = " def "
57+ es = EventSource ( config: config)
58+ XCTAssertEqual ( es. getLastEventId ( ) , " def " )
59+ }
60+
61+ func testCreatedSession( ) {
62+ let config = EventSource . Config ( handler: MockHandler ( ) , url: URL ( string: " abc " ) !)
63+ let session = EventSourceDelegate ( config: config) . createSession ( )
64+ XCTAssertEqual ( session. configuration. timeoutIntervalForRequest, config. idleTimeout)
65+ XCTAssertEqual ( session. configuration. httpAdditionalHeaders ? [ " Accept " ] as? String , " text/event-stream " )
66+ XCTAssertEqual ( session. configuration. httpAdditionalHeaders ? [ " Cache-Control " ] as? String , " no-cache " )
67+ }
68+
69+ func testCreateRequest( ) {
70+ // 192.0.2.1 is assigned as TEST-NET-1 reserved usage.
71+ var config = EventSource . Config ( handler: MockHandler ( ) , url: URL ( string: " http://192.0.2.1 " ) !)
72+ // Testing default configs
73+ var request = EventSourceDelegate ( config: config) . createRequest ( )
74+ XCTAssertEqual ( request. url, config. url)
75+ XCTAssertEqual ( request. httpMethod, config. method)
76+ XCTAssertEqual ( request. httpBody, config. body)
77+ XCTAssertEqual ( request. timeoutInterval, config. idleTimeout)
78+ XCTAssertEqual ( request. allHTTPHeaderFields, config. headers)
79+ // Testing customized configs
80+ let testBody = " test data " . data ( using: . utf8)
81+ let testHeaders = [ " removing " : " a " , " updating " : " b " ]
82+ let overrideHeaders = [ " updating " : " c " , " last-event-id " : " eventId2 " ]
83+ config. method = " REPORT "
84+ config. body = testBody
85+ config. lastEventId = " eventId "
86+ config. headers = testHeaders
87+ config. idleTimeout = 180.0
88+ config. headerTransform = { provided in
89+ XCTAssertEqual ( provided, [ " removing " : " a " , " updating " : " b " , " Last-Event-ID " : " eventId " ] )
90+ return overrideHeaders
91+ }
92+ request = EventSourceDelegate ( config: config) . createRequest ( )
93+ XCTAssertEqual ( request. url, config. url)
94+ XCTAssertEqual ( request. httpMethod, config. method)
95+ XCTAssertEqual ( request. httpBody, config. body)
96+ XCTAssertEqual ( request. timeoutInterval, config. idleTimeout)
97+ XCTAssertEqual ( request. allHTTPHeaderFields, overrideHeaders)
98+ }
99+
100+ func testDispatchError( ) {
101+ let handler = MockHandler ( )
102+ var connectionErrorHandlerCallCount = 0
103+ var connectionErrorAction : ConnectionErrorAction = . proceed
104+ var config = EventSource . Config ( handler: handler, url: URL ( string: " abc " ) !)
105+ config. connectionErrorHandler = { error in
106+ connectionErrorHandlerCallCount += 1
107+ return connectionErrorAction
108+ }
109+ let es = EventSourceDelegate ( config: config)
110+ XCTAssertEqual ( es. dispatchError ( error: DummyError ( ) ) , . proceed)
111+ XCTAssertEqual ( connectionErrorHandlerCallCount, 1 )
112+ guard case . error( let err) = handler. takeEvent ( ) , err is DummyError
113+ else {
114+ XCTFail ( " handler should receive error if EventSource is not shutting down " )
115+ return
116+ }
117+ XCTAssertTrue ( handler. receivedEvents. isEmpty)
118+ connectionErrorAction = . shutdown
119+ XCTAssertEqual ( es. dispatchError ( error: DummyError ( ) ) , . shutdown)
120+ XCTAssertEqual ( connectionErrorHandlerCallCount, 2 )
121+ XCTAssertTrue ( handler. receivedEvents. isEmpty)
122+ }
123+ }
124+
125+ private enum ReceivedEvent : Equatable {
126+ case opened, closed, message( String , MessageEvent ) , comment( String ) , error( Error )
127+
128+ static func == ( lhs: ReceivedEvent , rhs: ReceivedEvent ) -> Bool {
129+ switch ( lhs, rhs) {
130+ case ( . opened, . opened) :
131+ return true
132+ case ( . closed, . closed) :
133+ return true
134+ case let ( . message( typeLhs, eventLhs) , . message( typeRhs, eventRhs) ) :
135+ return typeLhs == typeRhs && eventLhs == eventRhs
136+ case let ( . comment( lhs) , . comment( rhs) ) :
137+ return lhs == rhs
138+ case ( . error, . error) :
139+ return true
140+ default :
141+ return false
142+ }
48143 }
49144}
50145
51146private class MockHandler : EventHandler {
147+ var receivedEvents : [ ReceivedEvent ] = [ ]
148+
52149 func onOpened( ) {
150+ receivedEvents. append ( . opened)
53151 }
54152
55153 func onClosed( ) {
154+ receivedEvents. append ( . closed)
56155 }
57156
58157 func onMessage( eventType: String , messageEvent: MessageEvent ) {
158+ receivedEvents. append ( . message( eventType, messageEvent) )
59159 }
60160
61161 func onComment( comment: String ) {
162+ receivedEvents. append ( . comment( comment) )
62163 }
63164
64165 func onError( error: Error ) {
166+ receivedEvents. append ( . error( error) )
167+ }
168+
169+ func takeEvent( ) -> ReceivedEvent {
170+ receivedEvents. remove ( at: 0 )
65171 }
66172}
173+
174+ private class DummyError : Error { }
0 commit comments