@@ -865,4 +865,56 @@ final class Analytics_Tests: XCTestCase {
865865 let d : Double ? = trackEvent? . properties? . value ( forKeyPath: " TestNaN " )
866866 XCTAssertNil ( d)
867867 }
868+
869+ // Linux doesn't know what URLProtocol is and on watchOS it somehow works differently and isn't hit.
870+ #if !os(Linux) && !os(watchOS)
871+ func testFailedSegmentResponse( ) throws {
872+ //register our network blocker (returns 400 response)
873+ guard URLProtocol . registerClass ( FailedNetworkCalls . self) else {
874+ XCTFail ( ) ; return }
875+
876+ let analytics = Analytics ( configuration: Configuration ( writeKey: " networkTest " ) )
877+
878+ waitUntilStarted ( analytics: analytics)
879+
880+ //set the httpClient to use our blocker session
881+ let segment = analytics. find ( pluginType: SegmentDestination . self)
882+ let configuration = URLSessionConfiguration . ephemeral
883+ configuration. allowsCellularAccess = true
884+ configuration. timeoutIntervalForRequest = 30
885+ configuration. timeoutIntervalForRequest = 60
886+ configuration. httpMaximumConnectionsPerHost = 2
887+ configuration. protocolClasses = [ FailedNetworkCalls . self]
888+ configuration. httpAdditionalHeaders = [
889+ " Content-Type " : " application/json; charset=utf-8 " ,
890+ " Authorization " : " Basic test " ,
891+ " User-Agent " : " analytics-ios/ \( Analytics . version ( ) ) "
892+ ]
893+
894+ let blockSession = URLSession ( configuration: configuration, delegate: nil , delegateQueue: nil )
895+
896+ segment? . httpClient? . session = blockSession
897+
898+ analytics. track ( name: " test track " , properties: [ " Malformed Paylod " : " My Failed Prop " ] )
899+
900+ //get fileUrl from track call
901+ let storedEvents : [ URL ] ? = analytics. storage. read ( . events)
902+ let fileURL = storedEvents![ 0 ]
903+
904+
905+ let expectation = XCTestExpectation ( )
906+
907+ analytics. flush {
908+ expectation. fulfill ( )
909+ }
910+
911+ wait ( for: [ expectation] , timeout: 1.0 )
912+
913+ let newStoredEvents : [ URL ] ? = analytics. storage. read ( . events)
914+
915+ XCTAssert ( !( newStoredEvents? . contains ( fileURL) ) !)
916+
917+ XCTAssertFalse ( FileManager . default. fileExists ( atPath: fileURL. path) )
918+ }
919+ #endif
868920}
0 commit comments