@@ -3,9 +3,8 @@ import XCTest
3
3
4
4
final class LDSwiftEventSourceTests : XCTestCase {
5
5
func testConfigDefaults( ) {
6
- let handler = MockHandler ( )
7
6
let url = URL ( string: " abc " ) !
8
- let config = EventSource . Config ( handler: handler , url: url)
7
+ let config = EventSource . Config ( handler: MockHandler ( ) , url: url)
9
8
XCTAssertEqual ( config. url, url)
10
9
XCTAssertEqual ( config. method, " GET " )
11
10
XCTAssertEqual ( config. body, nil )
@@ -15,12 +14,13 @@ final class LDSwiftEventSourceTests: XCTestCase {
15
14
XCTAssertEqual ( config. maxReconnectTime, 30.0 )
16
15
XCTAssertEqual ( config. backoffResetThreshold, 60.0 )
17
16
XCTAssertEqual ( config. idleTimeout, 300.0 )
17
+ XCTAssertEqual ( config. headerTransform ( [ " abc " : " 123 " ] ) , [ " abc " : " 123 " ] )
18
+ XCTAssertEqual ( config. connectionErrorHandler ( DummyError ( ) ) , . proceed)
18
19
}
19
20
20
21
func testConfigModification( ) {
21
- let handler = MockHandler ( )
22
22
let url = URL ( string: " abc " ) !
23
- var config = EventSource . Config ( handler: handler , url: url)
23
+ var config = EventSource . Config ( handler: MockHandler ( ) , url: url)
24
24
25
25
let testBody = " test data " . data ( using: . utf8)
26
26
let testHeaders = [ " Authorization " : " basic abc " ]
@@ -29,11 +29,12 @@ final class LDSwiftEventSourceTests: XCTestCase {
29
29
config. body = testBody
30
30
config. lastEventId = " eventId "
31
31
config. headers = testHeaders
32
- config. headerTransform = { _ in [ : ] }
33
32
config. reconnectTime = 2.0
34
33
config. maxReconnectTime = 60.0
35
34
config. backoffResetThreshold = 120.0
36
35
config. idleTimeout = 180.0
36
+ config. headerTransform = { _ in [ : ] }
37
+ config. connectionErrorHandler = { _ in . shutdown }
37
38
38
39
XCTAssertEqual ( config. url, url)
39
40
XCTAssertEqual ( config. method, " REPORT " )
@@ -45,22 +46,129 @@ final class LDSwiftEventSourceTests: XCTestCase {
45
46
XCTAssertEqual ( config. maxReconnectTime, 60.0 )
46
47
XCTAssertEqual ( config. backoffResetThreshold, 120.0 )
47
48
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
+ }
48
143
}
49
144
}
50
145
51
146
private class MockHandler : EventHandler {
147
+ var receivedEvents : [ ReceivedEvent ] = [ ]
148
+
52
149
func onOpened( ) {
150
+ receivedEvents. append ( . opened)
53
151
}
54
152
55
153
func onClosed( ) {
154
+ receivedEvents. append ( . closed)
56
155
}
57
156
58
157
func onMessage( eventType: String , messageEvent: MessageEvent ) {
158
+ receivedEvents. append ( . message( eventType, messageEvent) )
59
159
}
60
160
61
161
func onComment( comment: String ) {
162
+ receivedEvents. append ( . comment( comment) )
62
163
}
63
164
64
165
func onError( error: Error ) {
166
+ receivedEvents. append ( . error( error) )
167
+ }
168
+
169
+ func takeEvent( ) -> ReceivedEvent {
170
+ receivedEvents. remove ( at: 0 )
65
171
}
66
172
}
173
+
174
+ private class DummyError : Error { }
0 commit comments