CombineTests.swift 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. //
  2. // CombineTests.swift
  3. //
  4. // Copyright (c) 2020 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. #if !((os(iOS) && (arch(i386) || arch(arm))) || os(Windows) || os(Linux))
  25. import Alamofire
  26. import Combine
  27. import XCTest
  28. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  29. final class DataRequestCombineTests: CombineTestCase {
  30. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  31. func testThatDataRequestCanBePublished() {
  32. // Given
  33. let responseReceived = expectation(description: "response should be received")
  34. let completionReceived = expectation(description: "stream should complete")
  35. var response: DataResponse<TestResponse, AFError>?
  36. // When
  37. store {
  38. AF.request(.default)
  39. .publishDecodable(type: TestResponse.self)
  40. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  41. receiveValue: { response = $0; responseReceived.fulfill() })
  42. }
  43. waitForExpectations(timeout: timeout)
  44. // Then
  45. XCTAssertTrue(response?.result.isSuccess == true)
  46. }
  47. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  48. func testThatNonAutomaticDataRequestCanBePublished() {
  49. // Given
  50. let responseReceived = expectation(description: "response should be received")
  51. let completionReceived = expectation(description: "stream should complete")
  52. let session = Session(startRequestsImmediately: false)
  53. var response: DataResponse<TestResponse, AFError>?
  54. // When
  55. store {
  56. session.request(.default)
  57. .publishDecodable(type: TestResponse.self)
  58. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  59. receiveValue: { response = $0; responseReceived.fulfill() })
  60. }
  61. waitForExpectations(timeout: timeout)
  62. // Then
  63. XCTAssertTrue(response?.result.isSuccess == true)
  64. }
  65. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  66. func testThatDataRequestCanPublishData() {
  67. // Given
  68. let responseReceived = expectation(description: "response should be received")
  69. let completionReceived = expectation(description: "stream should complete")
  70. let session = Session(startRequestsImmediately: false)
  71. var response: DataResponse<Data, AFError>?
  72. // When
  73. store {
  74. session.request(.default)
  75. .publishData()
  76. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  77. receiveValue: { response = $0; responseReceived.fulfill() })
  78. }
  79. waitForExpectations(timeout: timeout)
  80. // Then
  81. XCTAssertTrue(response?.result.isSuccess == true)
  82. }
  83. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  84. func testThatDataRequestCanPublishString() {
  85. // Given
  86. let responseReceived = expectation(description: "response should be received")
  87. let completionReceived = expectation(description: "stream should complete")
  88. let session = Session(startRequestsImmediately: false)
  89. var response: DataResponse<String, AFError>?
  90. // When
  91. store {
  92. session.request(.default)
  93. .publishString()
  94. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  95. receiveValue: { response = $0; responseReceived.fulfill() })
  96. }
  97. waitForExpectations(timeout: timeout)
  98. // Then
  99. XCTAssertTrue(response?.result.isSuccess == true)
  100. }
  101. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  102. func testThatDataRequestCanBePublishedUnserialized() {
  103. // Given
  104. let responseReceived = expectation(description: "response should be received")
  105. let completionReceived = expectation(description: "stream should complete")
  106. var response: DataResponse<Data?, AFError>?
  107. // When
  108. store {
  109. AF.request(.default)
  110. .publishUnserialized()
  111. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  112. receiveValue: { response = $0; responseReceived.fulfill() })
  113. }
  114. waitForExpectations(timeout: timeout)
  115. // Then
  116. XCTAssertTrue(response?.result.isSuccess == true)
  117. }
  118. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  119. func testThatDataRequestCanBePublishedWithMultipleHandlers() {
  120. // Given
  121. let handlerResponseReceived = expectation(description: "handler response should be received")
  122. let publishedResponseReceived = expectation(description: "published response should be received")
  123. let completionReceived = expectation(description: "stream should complete")
  124. var handlerResponse: DataResponse<TestResponse, AFError>?
  125. var publishedResponse: DataResponse<TestResponse, AFError>?
  126. // When
  127. store {
  128. AF.request(.default)
  129. .responseDecodable(of: TestResponse.self) { handlerResponse = $0; handlerResponseReceived.fulfill() }
  130. .publishDecodable(type: TestResponse.self)
  131. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  132. receiveValue: { publishedResponse = $0; publishedResponseReceived.fulfill() })
  133. }
  134. waitForExpectations(timeout: timeout)
  135. // Then
  136. XCTAssertTrue(handlerResponse?.result.isSuccess == true)
  137. XCTAssertTrue(publishedResponse?.result.isSuccess == true)
  138. }
  139. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  140. func testThatDataRequestCanPublishResult() {
  141. // Given
  142. let responseReceived = expectation(description: "response should be received")
  143. let completionReceived = expectation(description: "stream should complete")
  144. var result: Result<TestResponse, AFError>?
  145. // When
  146. store {
  147. AF.request(.default)
  148. .publishDecodable(type: TestResponse.self)
  149. .result()
  150. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  151. receiveValue: { result = $0; responseReceived.fulfill() })
  152. }
  153. waitForExpectations(timeout: timeout)
  154. // Then
  155. XCTAssertTrue(result?.isSuccess == true)
  156. }
  157. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  158. func testThatDataRequestCanPublishValue() {
  159. // Given
  160. let responseReceived = expectation(description: "response should be received")
  161. let completionReceived = expectation(description: "stream should complete")
  162. var value: TestResponse?
  163. // When
  164. store {
  165. AF.request(.default)
  166. .publishDecodable(type: TestResponse.self)
  167. .value()
  168. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  169. receiveValue: { value = $0; responseReceived.fulfill() })
  170. }
  171. waitForExpectations(timeout: timeout)
  172. // Then
  173. XCTAssertNotNil(value)
  174. }
  175. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  176. func testThatDataRequestCanPublishValueWithFailure() {
  177. // Given
  178. let completionReceived = expectation(description: "stream should complete")
  179. var error: AFError?
  180. // When
  181. store {
  182. AF.request(Endpoint(path: .delay(interval: 1), timeout: 0.01))
  183. .publishDecodable(type: TestResponse.self)
  184. .value()
  185. .sink(receiveCompletion: { completion in
  186. switch completion {
  187. case let .failure(err):
  188. error = err
  189. case .finished:
  190. error = nil
  191. }
  192. completionReceived.fulfill()
  193. }, receiveValue: { _ in })
  194. }
  195. waitForExpectations(timeout: timeout)
  196. // Then
  197. XCTAssertNotNil(error)
  198. XCTAssertEqual((error?.underlyingError as? URLError)?.code, .timedOut)
  199. }
  200. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  201. func testThatPublishedDataRequestIsNotResumedUnlessSubscribed() {
  202. // Given
  203. let responseReceived = expectation(description: "response should be received")
  204. let completionReceived = expectation(description: "stream should complete")
  205. var response: DataResponse<TestResponse, AFError>?
  206. // When
  207. let request = AF.request(.default)
  208. let publisher = request.publishDecodable(type: TestResponse.self)
  209. let stateAfterPublisher = request.state
  210. store {
  211. publisher.sink(receiveCompletion: { _ in completionReceived.fulfill() },
  212. receiveValue: { response = $0; responseReceived.fulfill() })
  213. }
  214. let stateAfterSubscription = request.state
  215. waitForExpectations(timeout: timeout)
  216. // Then
  217. XCTAssertTrue(response?.result.isSuccess == true)
  218. XCTAssertEqual(stateAfterPublisher, .initialized)
  219. XCTAssertEqual(stateAfterSubscription, .resumed)
  220. }
  221. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  222. func testThatDataRequestCanSubscribedFromNonMainQueueButPublishedOnMainQueue() {
  223. // Given
  224. let responseReceived = expectation(description: "response should be received")
  225. let completionReceived = expectation(description: "stream should complete")
  226. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  227. var receivedOnMain = false
  228. var response: DataResponse<TestResponse, AFError>?
  229. // When
  230. store {
  231. AF.request(.default)
  232. .publishDecodable(type: TestResponse.self)
  233. .subscribe(on: queue)
  234. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  235. receiveValue: {
  236. receivedOnMain = Thread.isMainThread
  237. response = $0
  238. responseReceived.fulfill()
  239. })
  240. }
  241. waitForExpectations(timeout: timeout)
  242. // Then
  243. XCTAssertTrue(response?.result.isSuccess == true)
  244. XCTAssertTrue(receivedOnMain)
  245. }
  246. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  247. func testThatDataRequestPublishedOnSeparateQueueIsReceivedOnThatQueue() {
  248. // Given
  249. let responseReceived = expectation(description: "response should be received")
  250. let completionReceived = expectation(description: "stream should complete")
  251. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  252. var response: DataResponse<TestResponse, AFError>?
  253. // When
  254. store {
  255. AF.request(.default)
  256. .publishDecodable(type: TestResponse.self, queue: queue)
  257. .sink(receiveCompletion: { _ in
  258. dispatchPrecondition(condition: .onQueue(queue))
  259. completionReceived.fulfill()
  260. },
  261. receiveValue: {
  262. dispatchPrecondition(condition: .onQueue(queue))
  263. response = $0
  264. responseReceived.fulfill()
  265. })
  266. }
  267. waitForExpectations(timeout: timeout)
  268. // Then
  269. XCTAssertTrue(response?.result.isSuccess == true)
  270. }
  271. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  272. func testThatDataRequestPublishedOnSeparateQueueCanBeReceivedOntoMainQueue() {
  273. // Given
  274. let responseReceived = expectation(description: "response should be received")
  275. let completionReceived = expectation(description: "stream should complete")
  276. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  277. var receivedOnMain = false
  278. var response: DataResponse<TestResponse, AFError>?
  279. // When
  280. store {
  281. AF.request(.default)
  282. .publishDecodable(type: TestResponse.self, queue: queue)
  283. .receive(on: DispatchQueue.main)
  284. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  285. receiveValue: {
  286. receivedOnMain = Thread.isMainThread
  287. response = $0
  288. responseReceived.fulfill()
  289. })
  290. }
  291. waitForExpectations(timeout: timeout)
  292. // Then
  293. XCTAssertTrue(response?.result.isSuccess == true)
  294. XCTAssertTrue(receivedOnMain)
  295. }
  296. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  297. func testThatPublishedDataRequestCanBeCancelledAutomatically() throws {
  298. if #available(macOS 11, iOS 14, watchOS 7, tvOS 14, *) {
  299. throw XCTSkip("Skip on 2020 OS versions, as Combine cancellation no longer emits a value.")
  300. }
  301. // Given
  302. let responseReceived = expectation(description: "response should be received")
  303. let completionReceived = expectation(description: "stream should complete")
  304. var response: DataResponse<TestResponse, AFError>?
  305. // When
  306. let request = AF.request(.default)
  307. var token: AnyCancellable? = request
  308. .publishDecodable(type: TestResponse.self)
  309. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  310. receiveValue: { response = $0; responseReceived.fulfill() })
  311. token = nil
  312. waitForExpectations(timeout: timeout)
  313. // Then
  314. XCTAssertTrue(response?.result.isFailure == true)
  315. XCTAssertTrue(response?.error?.isExplicitlyCancelledError == true,
  316. "error is not explicitly cancelled but \(response?.error?.localizedDescription ?? "None")")
  317. XCTAssertTrue(request.isCancelled)
  318. XCTAssertNil(token)
  319. }
  320. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  321. func testThatPublishedDataRequestCanBeCancelledManually() {
  322. // Given
  323. let responseReceived = expectation(description: "response should be received")
  324. let completionReceived = expectation(description: "stream should complete")
  325. var response: DataResponse<TestResponse, AFError>?
  326. // When
  327. let request = AF.request(.default)
  328. store {
  329. request
  330. .publishDecodable(type: TestResponse.self)
  331. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  332. receiveValue: { response = $0; responseReceived.fulfill() })
  333. }
  334. request.cancel()
  335. waitForExpectations(timeout: timeout)
  336. // Then
  337. XCTAssertTrue(response?.result.isFailure == true)
  338. XCTAssertTrue(response?.error?.isExplicitlyCancelledError == true,
  339. "error is not explicitly cancelled but \(response?.error?.localizedDescription ?? "None")")
  340. XCTAssertTrue(request.isCancelled)
  341. }
  342. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  343. func testThatMultipleDataRequestPublishersCanBeCombined() {
  344. // Given
  345. let responseReceived = expectation(description: "combined response should be received")
  346. let completionReceived = expectation(description: "combined stream should complete")
  347. var firstResponse: DataResponse<TestResponse, AFError>?
  348. var secondResponse: DataResponse<TestResponse, AFError>?
  349. // When
  350. let first = AF.request(.default)
  351. .publishDecodable(type: TestResponse.self)
  352. let second = AF.request(.default)
  353. .publishDecodable(type: TestResponse.self)
  354. store {
  355. Publishers.CombineLatest(first, second)
  356. .sink(receiveCompletion: { _ in completionReceived.fulfill() }) { first, second in
  357. firstResponse = first
  358. secondResponse = second
  359. responseReceived.fulfill()
  360. }
  361. }
  362. waitForExpectations(timeout: timeout)
  363. // Then
  364. XCTAssertTrue(firstResponse?.result.isSuccess == true)
  365. XCTAssertTrue(secondResponse?.result.isSuccess == true)
  366. }
  367. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  368. func testThatMultipleDataRequestPublishersCanBeChained() {
  369. // Given
  370. let responseReceived = expectation(description: "combined response should be received")
  371. let completionReceived = expectation(description: "combined stream should complete")
  372. let customValue = "CustomValue"
  373. var firstResponse: DataResponse<TestResponse, AFError>?
  374. var secondResponse: DataResponse<TestResponse, AFError>?
  375. // When
  376. store {
  377. AF.request(.default)
  378. .publishDecodable(type: TestResponse.self)
  379. .flatMap { response -> DataResponsePublisher<TestResponse> in
  380. firstResponse = response
  381. let request = Endpoint(headers: ["X-Custom": customValue])
  382. return AF.request(request)
  383. .publishDecodable(type: TestResponse.self)
  384. }
  385. .sink(receiveCompletion: { _ in completionReceived.fulfill() }) { response in
  386. secondResponse = response
  387. responseReceived.fulfill()
  388. }
  389. }
  390. waitForExpectations(timeout: timeout)
  391. // Then
  392. XCTAssertTrue(firstResponse?.result.isSuccess == true)
  393. XCTAssertTrue(secondResponse?.result.isSuccess == true)
  394. XCTAssertEqual(secondResponse?.value?.headers["X-Custom"], customValue)
  395. }
  396. }
  397. // MARK: - DataStreamRequest
  398. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  399. final class DataStreamRequestCombineTests: CombineTestCase {
  400. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  401. func testThatDataStreamRequestCanBePublished() {
  402. // Given
  403. let responseReceived = expectation(description: "response should be received")
  404. let completionReceived = expectation(description: "stream should complete")
  405. var result: Result<TestResponse, AFError>?
  406. // When
  407. store {
  408. AF.streamRequest(.default)
  409. .publishDecodable(type: TestResponse.self)
  410. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  411. receiveValue: { stream in
  412. switch stream.event {
  413. case let .stream(value):
  414. result = value
  415. case .complete:
  416. responseReceived.fulfill()
  417. }
  418. })
  419. }
  420. waitForExpectations(timeout: timeout)
  421. // Then
  422. XCTAssertNotNil(result?.success)
  423. }
  424. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  425. func testThatNonAutomaticDataStreamRequestCanBePublished() {
  426. // Given
  427. let responseReceived = expectation(description: "response should be received")
  428. let completionReceived = expectation(description: "stream should complete")
  429. let session = Session(startRequestsImmediately: false)
  430. var result: Result<TestResponse, AFError>?
  431. // When
  432. store {
  433. session.streamRequest(.default)
  434. .publishDecodable(type: TestResponse.self)
  435. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  436. receiveValue: { stream in
  437. switch stream.event {
  438. case let .stream(value):
  439. result = value
  440. case .complete:
  441. responseReceived.fulfill()
  442. }
  443. })
  444. }
  445. waitForExpectations(timeout: timeout)
  446. // Then
  447. XCTAssertNotNil(result?.success)
  448. }
  449. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  450. func testThatDataStreamRequestCanPublishData() {
  451. // Given
  452. let responseReceived = expectation(description: "response should be received")
  453. let completionReceived = expectation(description: "stream should complete")
  454. var result: Result<Data, AFError>?
  455. // When
  456. store {
  457. AF.streamRequest(.default)
  458. .publishData()
  459. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  460. receiveValue: { stream in
  461. switch stream.event {
  462. case let .stream(value):
  463. result = value
  464. case .complete:
  465. responseReceived.fulfill()
  466. }
  467. })
  468. }
  469. waitForExpectations(timeout: timeout)
  470. // Then
  471. XCTAssertNotNil(result?.success)
  472. }
  473. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  474. func testThatDataStreamRequestCanPublishString() {
  475. // Given
  476. let responseReceived = expectation(description: "response should be received")
  477. let completionReceived = expectation(description: "stream should complete")
  478. var result: Result<String, AFError>?
  479. // When
  480. store {
  481. AF.streamRequest(.default)
  482. .publishString()
  483. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  484. receiveValue: { stream in
  485. switch stream.event {
  486. case let .stream(value):
  487. result = value
  488. case .complete:
  489. responseReceived.fulfill()
  490. }
  491. })
  492. }
  493. waitForExpectations(timeout: timeout)
  494. // Then
  495. XCTAssertNotNil(result?.success)
  496. }
  497. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  498. func testThatDataStreamRequestCanBePublishedWithMultipleHandlers() {
  499. // Given
  500. let handlerResponseReceived = expectation(description: "handler response should be received")
  501. let publishedResponseReceived = expectation(description: "response should be received")
  502. let completionReceived = expectation(description: "stream should complete")
  503. var handlerResult: Result<TestResponse, AFError>?
  504. var publishedResult: Result<TestResponse, AFError>?
  505. // When
  506. store {
  507. AF.streamRequest(.default)
  508. .responseStreamDecodable(of: TestResponse.self) { stream in
  509. switch stream.event {
  510. case let .stream(value):
  511. handlerResult = value
  512. case .complete:
  513. handlerResponseReceived.fulfill()
  514. }
  515. }
  516. .publishDecodable(type: TestResponse.self)
  517. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  518. receiveValue: { stream in
  519. switch stream.event {
  520. case let .stream(value):
  521. publishedResult = value
  522. case .complete:
  523. publishedResponseReceived.fulfill()
  524. }
  525. })
  526. }
  527. waitForExpectations(timeout: timeout)
  528. // Then
  529. XCTAssertNotNil(handlerResult?.success)
  530. XCTAssertNotNil(publishedResult?.success)
  531. }
  532. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  533. func testThatDataStreamRequestCanPublishResult() {
  534. // Given
  535. let responseReceived = expectation(description: "response should be received")
  536. let completionReceived = expectation(description: "stream should complete")
  537. var result: Result<TestResponse, AFError>?
  538. // When
  539. store {
  540. AF.streamRequest(.default)
  541. .publishDecodable(type: TestResponse.self)
  542. .result()
  543. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  544. receiveValue: { received in
  545. result = received
  546. responseReceived.fulfill()
  547. })
  548. }
  549. waitForExpectations(timeout: timeout)
  550. // Then
  551. XCTAssertNotNil(result?.success)
  552. }
  553. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  554. func testThatDataStreamRequestCanPublishResultWithResponseFailure() {
  555. // Given
  556. let responseReceived = expectation(description: "response should be received")
  557. let completionReceived = expectation(description: "stream should complete")
  558. var result: Result<TestResponse, AFError>?
  559. // When
  560. store {
  561. AF.streamRequest(Endpoint.delay(1).modifying(\.timeout, to: 0.1))
  562. .publishDecodable(type: TestResponse.self)
  563. .result()
  564. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  565. receiveValue: { received in
  566. result = received
  567. responseReceived.fulfill()
  568. })
  569. }
  570. waitForExpectations(timeout: timeout)
  571. // Then
  572. XCTAssertNil(result?.success)
  573. XCTAssertEqual((result?.failure?.underlyingError as? URLError)?.code, .timedOut)
  574. }
  575. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  576. func testThatDataStreamRequestCanPublishValue() {
  577. // Given
  578. let responseReceived = expectation(description: "response should be received")
  579. let completionReceived = expectation(description: "stream should complete")
  580. var response: TestResponse?
  581. // When
  582. store {
  583. AF.streamRequest(.default)
  584. .publishDecodable(type: TestResponse.self)
  585. .value()
  586. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  587. receiveValue: { received in
  588. response = received
  589. responseReceived.fulfill()
  590. })
  591. }
  592. waitForExpectations(timeout: timeout)
  593. // Then
  594. XCTAssertNotNil(response)
  595. }
  596. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  597. func testThatDataStreamRequestCanPublishValueWithFailure() {
  598. // Given
  599. let completionReceived = expectation(description: "stream should complete")
  600. var error: AFError?
  601. // When
  602. store {
  603. AF.streamRequest(Endpoint.delay(1).modifying(\.timeout, to: 0.1))
  604. .publishDecodable(type: TestResponse.self)
  605. .value()
  606. .sink(receiveCompletion: { completion in
  607. switch completion {
  608. case let .failure(err):
  609. error = err
  610. case .finished:
  611. error = nil
  612. }
  613. completionReceived.fulfill()
  614. }, receiveValue: { _ in })
  615. }
  616. waitForExpectations(timeout: timeout)
  617. // Then
  618. XCTAssertNotNil(error)
  619. XCTAssertEqual((error?.underlyingError as? URLError)?.code, .timedOut)
  620. }
  621. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  622. func testThatPublishedDataStreamRequestIsNotResumedUnlessSubscribed() {
  623. // Given
  624. let responseReceived = expectation(description: "response should be received")
  625. let completionReceived = expectation(description: "stream should complete")
  626. var result: Result<TestResponse, AFError>?
  627. // When
  628. let request = AF.streamRequest(.default)
  629. let publisher = request.publishDecodable(type: TestResponse.self)
  630. let stateAfterPublisher = request.state
  631. store {
  632. publisher.sink(receiveCompletion: { _ in completionReceived.fulfill() },
  633. receiveValue: { stream in
  634. switch stream.event {
  635. case let .stream(value):
  636. result = value
  637. case .complete:
  638. responseReceived.fulfill()
  639. }
  640. })
  641. }
  642. let stateAfterSubscription = request.state
  643. waitForExpectations(timeout: timeout)
  644. // Then
  645. XCTAssertTrue(result?.isSuccess == true)
  646. XCTAssertEqual(stateAfterPublisher, .initialized)
  647. XCTAssertEqual(stateAfterSubscription, .resumed)
  648. }
  649. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  650. func testThatDataStreamRequestCanSubscribedFromNonMainQueueButPublishedOnMainQueue() {
  651. // Given
  652. let responseReceived = expectation(description: "response should be received")
  653. let completionReceived = expectation(description: "stream should complete")
  654. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  655. var receivedOnMain = false
  656. var result: Result<TestResponse, AFError>?
  657. // When
  658. store {
  659. AF.streamRequest(.default)
  660. .publishDecodable(type: TestResponse.self)
  661. .subscribe(on: queue)
  662. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  663. receiveValue: { stream in
  664. receivedOnMain = Thread.isMainThread
  665. switch stream.event {
  666. case let .stream(value):
  667. result = value
  668. case .complete:
  669. responseReceived.fulfill()
  670. }
  671. })
  672. }
  673. waitForExpectations(timeout: timeout)
  674. // Then
  675. XCTAssertNotNil(result?.success)
  676. XCTAssertTrue(receivedOnMain)
  677. }
  678. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  679. func testThatDataStreamRequestPublishedOnSeparateQueueIsReceivedOnThatQueue() {
  680. // Given
  681. let responseReceived = expectation(description: "response should be received")
  682. let completionReceived = expectation(description: "stream should complete")
  683. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  684. var result: Result<TestResponse, AFError>?
  685. // When
  686. store {
  687. AF.streamRequest(.default)
  688. .publishDecodable(type: TestResponse.self, queue: queue)
  689. .sink(receiveCompletion: { _ in
  690. dispatchPrecondition(condition: .onQueue(queue))
  691. completionReceived.fulfill()
  692. },
  693. receiveValue: { stream in
  694. dispatchPrecondition(condition: .onQueue(queue))
  695. switch stream.event {
  696. case let .stream(value):
  697. result = value
  698. case .complete:
  699. responseReceived.fulfill()
  700. }
  701. })
  702. }
  703. waitForExpectations(timeout: timeout)
  704. // Then
  705. XCTAssertNotNil(result?.success)
  706. }
  707. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  708. func testThatDataStreamRequestPublishedOnSeparateQueueCanBeReceivedOntoMainQueue() {
  709. // Given
  710. let responseReceived = expectation(description: "response should be received")
  711. let completionReceived = expectation(description: "stream should complete")
  712. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  713. var receivedOnMain = false
  714. var result: Result<TestResponse, AFError>?
  715. // When
  716. store {
  717. AF.streamRequest(.default)
  718. .publishDecodable(type: TestResponse.self, queue: queue)
  719. .receive(on: DispatchQueue.main)
  720. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  721. receiveValue: { stream in
  722. receivedOnMain = Thread.isMainThread
  723. switch stream.event {
  724. case let .stream(value):
  725. result = value
  726. case .complete:
  727. responseReceived.fulfill()
  728. }
  729. })
  730. }
  731. waitForExpectations(timeout: timeout)
  732. // Then
  733. XCTAssertNotNil(result?.success)
  734. XCTAssertTrue(receivedOnMain)
  735. }
  736. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  737. func testThatPublishedDataStreamRequestCanBeCancelledAutomatically() throws {
  738. if #available(macOS 11, iOS 14, watchOS 7, tvOS 14, *) {
  739. throw XCTSkip("Skip on 2020 OS versions, as Combine cancellation no longer emits a value.")
  740. }
  741. // Given
  742. let responseReceived = expectation(description: "response should be received")
  743. let completionReceived = expectation(description: "stream should complete")
  744. var error: AFError?
  745. // When
  746. let request = AF.streamRequest(.default)
  747. var token: AnyCancellable? = request
  748. .publishDecodable(type: TestResponse.self)
  749. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  750. receiveValue: { error = $0.completion?.error; responseReceived.fulfill() })
  751. token = nil
  752. waitForExpectations(timeout: timeout)
  753. // Then
  754. XCTAssertNotNil(error)
  755. XCTAssertTrue(error?.isExplicitlyCancelledError == true,
  756. "error is not explicitly cancelled but \(error?.localizedDescription ?? "None")")
  757. XCTAssertTrue(request.isCancelled)
  758. XCTAssertNil(token)
  759. }
  760. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  761. func testThatPublishedDataStreamRequestCanBeCancelledManually() {
  762. // Given
  763. let responseReceived = expectation(description: "response should be received")
  764. let completionReceived = expectation(description: "stream should complete")
  765. var error: AFError?
  766. // When
  767. let request = AF.streamRequest(.default)
  768. store {
  769. request
  770. .publishDecodable(type: TestResponse.self)
  771. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  772. receiveValue: { error = $0.completion?.error; responseReceived.fulfill() })
  773. }
  774. request.cancel()
  775. waitForExpectations(timeout: timeout)
  776. // Then
  777. XCTAssertNotNil(error)
  778. XCTAssertTrue(error?.isExplicitlyCancelledError == true,
  779. "error is not explicitly cancelled but \(error?.localizedDescription ?? "None")")
  780. XCTAssertTrue(request.isCancelled)
  781. }
  782. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  783. func testThatMultipleDataStreamPublishersCanBeCombined() {
  784. // Given
  785. let responseReceived = expectation(description: "combined response should be received")
  786. let completionReceived = expectation(description: "combined stream should complete")
  787. var firstCompletion: DataStreamRequest.Completion?
  788. var secondCompletion: DataStreamRequest.Completion?
  789. // When
  790. let first = AF.streamRequest(.default)
  791. .publishDecodable(type: TestResponse.self)
  792. let second = AF.streamRequest(.default)
  793. .publishDecodable(type: TestResponse.self)
  794. store {
  795. Publishers.CombineLatest(first.compactMap(\.completion), second.compactMap(\.completion))
  796. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  797. receiveValue: { first, second in
  798. firstCompletion = first
  799. secondCompletion = second
  800. responseReceived.fulfill()
  801. })
  802. }
  803. waitForExpectations(timeout: timeout)
  804. // Then
  805. XCTAssertNotNil(firstCompletion)
  806. XCTAssertNotNil(secondCompletion)
  807. }
  808. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  809. func testThatMultipleDataStreamRequestPublishersCanBeChained() {
  810. // Given
  811. let responseReceived = expectation(description: "combined response should be received")
  812. let completionReceived = expectation(description: "combined stream should complete")
  813. var firstCompletion: DataStreamRequest.Completion?
  814. var secondCompletion: DataStreamRequest.Completion?
  815. // When
  816. store {
  817. AF.streamRequest(.default)
  818. .publishDecodable(type: TestResponse.self)
  819. .compactMap(\.completion)
  820. .flatMap { completion -> DataStreamPublisher<TestResponse> in
  821. firstCompletion = completion
  822. return AF.streamRequest(.default)
  823. .publishDecodable(type: TestResponse.self)
  824. }
  825. .compactMap(\.completion)
  826. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  827. receiveValue: { secondCompletion = $0; responseReceived.fulfill() })
  828. }
  829. waitForExpectations(timeout: timeout)
  830. // Then
  831. XCTAssertNotNil(firstCompletion)
  832. XCTAssertNotNil(secondCompletion)
  833. }
  834. }
  835. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  836. final class DownloadRequestCombineTests: CombineTestCase {
  837. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  838. func testThatDownloadRequestCanBePublished() {
  839. // Given
  840. let responseReceived = expectation(description: "response should be received")
  841. let completionReceived = expectation(description: "publisher should complete")
  842. var response: DownloadResponse<TestResponse, AFError>?
  843. // When
  844. store {
  845. AF.download(.default)
  846. .publishDecodable(type: TestResponse.self)
  847. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  848. receiveValue: { response = $0; responseReceived.fulfill() })
  849. }
  850. waitForExpectations(timeout: timeout)
  851. // Then
  852. XCTAssertTrue(response?.result.isSuccess == true)
  853. }
  854. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  855. func testThatNonAutomaticDownloadRequestCanBePublished() {
  856. // Given
  857. let responseReceived = expectation(description: "response should be received")
  858. let completionReceived = expectation(description: "publisher should complete")
  859. let session = Session(startRequestsImmediately: false)
  860. var response: DownloadResponse<TestResponse, AFError>?
  861. // When
  862. store {
  863. session.download(.default)
  864. .publishDecodable(type: TestResponse.self)
  865. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  866. receiveValue: { response = $0; responseReceived.fulfill() })
  867. }
  868. waitForExpectations(timeout: timeout)
  869. // Then
  870. XCTAssertTrue(response?.result.isSuccess == true)
  871. }
  872. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  873. func testThatDownloadRequestCanPublishData() {
  874. // Given
  875. let responseReceived = expectation(description: "response should be received")
  876. let completionReceived = expectation(description: "publisher should complete")
  877. var response: DownloadResponse<Data, AFError>?
  878. // When
  879. store {
  880. AF.download(.default)
  881. .publishData()
  882. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  883. receiveValue: { response = $0; responseReceived.fulfill() })
  884. }
  885. waitForExpectations(timeout: timeout)
  886. // Then
  887. XCTAssertTrue(response?.result.isSuccess == true)
  888. }
  889. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  890. func testThatDownloadRequestCanPublishString() {
  891. // Given
  892. let responseReceived = expectation(description: "response should be received")
  893. let completionReceived = expectation(description: "publisher should complete")
  894. var response: DownloadResponse<String, AFError>?
  895. // When
  896. store {
  897. AF.download(.default)
  898. .publishString()
  899. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  900. receiveValue: { response = $0; responseReceived.fulfill() })
  901. }
  902. waitForExpectations(timeout: timeout)
  903. // Then
  904. XCTAssertTrue(response?.result.isSuccess == true)
  905. }
  906. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  907. func testThatDownloadRequestCanPublishUnserialized() {
  908. // Given
  909. let responseReceived = expectation(description: "response should be received")
  910. let completionReceived = expectation(description: "publisher should complete")
  911. var response: DownloadResponse<URL?, AFError>?
  912. // When
  913. store {
  914. AF.download(.default)
  915. .publishUnserialized()
  916. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  917. receiveValue: { response = $0; responseReceived.fulfill() })
  918. }
  919. waitForExpectations(timeout: timeout)
  920. // Then
  921. XCTAssertTrue(response?.result.isSuccess == true)
  922. }
  923. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  924. func testThatDownloadRequestCanPublishURL() {
  925. // Given
  926. let responseReceived = expectation(description: "response should be received")
  927. let completionReceived = expectation(description: "publisher should complete")
  928. var response: DownloadResponse<URL, AFError>?
  929. // When
  930. store {
  931. AF.download(.default)
  932. .publishURL()
  933. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  934. receiveValue: { response = $0; responseReceived.fulfill() })
  935. }
  936. waitForExpectations(timeout: timeout)
  937. // Then
  938. XCTAssertTrue(response?.result.isSuccess == true)
  939. }
  940. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  941. func testThatDownloadRequestCanPublishWithMultipleHandlers() {
  942. // Given
  943. let handlerResponseReceived = expectation(description: "handler response should be received")
  944. let publishedResponseReceived = expectation(description: "published response should be received")
  945. let completionReceived = expectation(description: "stream should complete")
  946. var handlerResponse: DownloadResponse<TestResponse, AFError>?
  947. var publishedResponse: DownloadResponse<TestResponse, AFError>?
  948. // When
  949. store {
  950. AF.download(.default)
  951. .responseDecodable(of: TestResponse.self) { handlerResponse = $0; handlerResponseReceived.fulfill() }
  952. .publishDecodable(type: TestResponse.self)
  953. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  954. receiveValue: { publishedResponse = $0; publishedResponseReceived.fulfill() })
  955. }
  956. waitForExpectations(timeout: timeout)
  957. // Then
  958. XCTAssertTrue(handlerResponse?.result.isSuccess == true)
  959. XCTAssertTrue(publishedResponse?.result.isSuccess == true)
  960. }
  961. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  962. func testThatDownloadRequestCanPublishResult() {
  963. // Given
  964. let responseReceived = expectation(description: "response should be received")
  965. let completionReceived = expectation(description: "publisher should complete")
  966. var result: Result<TestResponse, AFError>?
  967. // When
  968. store {
  969. AF.download(.default)
  970. .publishDecodable(type: TestResponse.self)
  971. .result()
  972. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  973. receiveValue: { result = $0; responseReceived.fulfill() })
  974. }
  975. waitForExpectations(timeout: timeout)
  976. // Then
  977. XCTAssertTrue(result?.isSuccess == true)
  978. }
  979. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  980. func testThatDownloadRequestCanPublishValueWithFailure() {
  981. // Given
  982. let completionReceived = expectation(description: "stream should complete")
  983. var error: AFError?
  984. // When
  985. store {
  986. AF.download(Endpoint.delay(1).modifying(\.timeout, to: 0.1))
  987. .publishDecodable(type: TestResponse.self)
  988. .value()
  989. .sink(receiveCompletion: { completion in
  990. switch completion {
  991. case let .failure(err):
  992. error = err
  993. case .finished:
  994. error = nil
  995. }
  996. completionReceived.fulfill()
  997. }, receiveValue: { _ in })
  998. }
  999. waitForExpectations(timeout: timeout)
  1000. // Then
  1001. XCTAssertNotNil(error)
  1002. XCTAssertEqual((error?.underlyingError as? URLError)?.code, .timedOut)
  1003. }
  1004. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1005. func testThatPublishedDownloadRequestIsNotResumedUnlessSubscribed() {
  1006. // Given
  1007. let responseReceived = expectation(description: "response should be received")
  1008. let completionReceived = expectation(description: "stream should complete")
  1009. var response: DownloadResponse<TestResponse, AFError>?
  1010. // When
  1011. let request = AF.download(.default)
  1012. let publisher = request.publishDecodable(type: TestResponse.self)
  1013. let stateAfterPublisher = request.state
  1014. store {
  1015. publisher.sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1016. receiveValue: { response = $0; responseReceived.fulfill() })
  1017. }
  1018. let stateAfterSubscription = request.state
  1019. waitForExpectations(timeout: timeout)
  1020. // Then
  1021. XCTAssertTrue(response?.result.isSuccess == true)
  1022. XCTAssertEqual(stateAfterPublisher, .initialized)
  1023. XCTAssertEqual(stateAfterSubscription, .resumed)
  1024. }
  1025. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1026. func testThatDownloadRequestCanSubscribedFromNonMainQueueButPublishedOnMainQueue() {
  1027. // Given
  1028. let responseReceived = expectation(description: "response should be received")
  1029. let completionReceived = expectation(description: "stream should complete")
  1030. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  1031. var receivedOnMain = false
  1032. var response: DownloadResponse<TestResponse, AFError>?
  1033. // When
  1034. store {
  1035. AF.download(.default)
  1036. .publishDecodable(type: TestResponse.self)
  1037. .subscribe(on: queue)
  1038. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1039. receiveValue: {
  1040. receivedOnMain = Thread.isMainThread
  1041. response = $0
  1042. responseReceived.fulfill()
  1043. })
  1044. }
  1045. waitForExpectations(timeout: timeout)
  1046. // Then
  1047. XCTAssertTrue(response?.result.isSuccess == true)
  1048. XCTAssertTrue(receivedOnMain)
  1049. }
  1050. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1051. func testThatDownloadRequestPublishedOnSeparateQueueIsReceivedOnThatQueue() {
  1052. // Given
  1053. let responseReceived = expectation(description: "response should be received")
  1054. let completionReceived = expectation(description: "stream should complete")
  1055. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  1056. var response: DownloadResponse<TestResponse, AFError>?
  1057. // When
  1058. store {
  1059. AF.download(.default)
  1060. .publishDecodable(type: TestResponse.self, queue: queue)
  1061. .sink(receiveCompletion: { _ in
  1062. dispatchPrecondition(condition: .onQueue(queue))
  1063. completionReceived.fulfill()
  1064. },
  1065. receiveValue: {
  1066. dispatchPrecondition(condition: .onQueue(queue))
  1067. response = $0
  1068. responseReceived.fulfill()
  1069. })
  1070. }
  1071. waitForExpectations(timeout: timeout)
  1072. // Then
  1073. XCTAssertTrue(response?.result.isSuccess == true)
  1074. }
  1075. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1076. func testThatDownloadRequestPublishedOnSeparateQueueCanBeReceivedOntoMainQueue() {
  1077. // Given
  1078. let responseReceived = expectation(description: "response should be received")
  1079. let completionReceived = expectation(description: "stream should complete")
  1080. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  1081. var receivedOnMain = false
  1082. var response: DownloadResponse<TestResponse, AFError>?
  1083. // When
  1084. store {
  1085. AF.download(.default)
  1086. .publishDecodable(type: TestResponse.self, queue: queue)
  1087. .receive(on: DispatchQueue.main)
  1088. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1089. receiveValue: {
  1090. receivedOnMain = Thread.isMainThread
  1091. response = $0
  1092. responseReceived.fulfill()
  1093. })
  1094. }
  1095. waitForExpectations(timeout: timeout)
  1096. // Then
  1097. XCTAssertTrue(response?.result.isSuccess == true)
  1098. XCTAssertTrue(receivedOnMain)
  1099. }
  1100. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1101. func testThatPublishedDownloadRequestCanBeCancelledAutomatically() throws {
  1102. if #available(macOS 11, iOS 14, watchOS 7, tvOS 14, *) {
  1103. throw XCTSkip("Skip on 2020 OS versions, as Combine cancellation no longer emits a value.")
  1104. }
  1105. // Given
  1106. let responseReceived = expectation(description: "response should be received")
  1107. let completionReceived = expectation(description: "stream should complete")
  1108. var response: DownloadResponse<TestResponse, AFError>?
  1109. // When
  1110. let request = AF.download(.default)
  1111. var token: AnyCancellable? = request
  1112. .publishDecodable(type: TestResponse.self)
  1113. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1114. receiveValue: { response = $0; responseReceived.fulfill() })
  1115. token = nil
  1116. waitForExpectations(timeout: timeout)
  1117. // Then
  1118. XCTAssertTrue(response?.result.isFailure == true)
  1119. XCTAssertTrue(response?.error?.isExplicitlyCancelledError == true,
  1120. "error is not explicitly cancelled but \(response?.error?.localizedDescription ?? "None")")
  1121. XCTAssertTrue(request.isCancelled)
  1122. XCTAssertNil(token)
  1123. }
  1124. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1125. func testThatPublishedDownloadRequestCanBeCancelledManually() {
  1126. // Given
  1127. let responseReceived = expectation(description: "response should be received")
  1128. let completionReceived = expectation(description: "stream should complete")
  1129. var response: DownloadResponse<TestResponse, AFError>?
  1130. // When
  1131. let request = AF.download(.default)
  1132. store {
  1133. request
  1134. .publishDecodable(type: TestResponse.self)
  1135. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1136. receiveValue: { response = $0; responseReceived.fulfill() })
  1137. }
  1138. request.cancel()
  1139. waitForExpectations(timeout: timeout)
  1140. // Then
  1141. XCTAssertTrue(response?.result.isFailure == true)
  1142. XCTAssertTrue(response?.error?.isExplicitlyCancelledError == true,
  1143. "error is not explicitly cancelled but \(response?.error?.localizedDescription ?? "None")")
  1144. XCTAssertTrue(request.isCancelled)
  1145. }
  1146. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1147. func testThatMultipleDownloadRequestPublishersCanBeCombined() {
  1148. // Given
  1149. let responseReceived = expectation(description: "combined response should be received")
  1150. let completionReceived = expectation(description: "combined stream should complete")
  1151. var firstResponse: DownloadResponse<TestResponse, AFError>?
  1152. var secondResponse: DownloadResponse<TestResponse, AFError>?
  1153. // When
  1154. let first = AF.download(.default)
  1155. .publishDecodable(type: TestResponse.self)
  1156. let second = AF.download(.default)
  1157. .publishDecodable(type: TestResponse.self)
  1158. store {
  1159. Publishers.CombineLatest(first, second)
  1160. .sink(receiveCompletion: { _ in completionReceived.fulfill() }) { first, second in
  1161. firstResponse = first
  1162. secondResponse = second
  1163. responseReceived.fulfill()
  1164. }
  1165. }
  1166. waitForExpectations(timeout: timeout)
  1167. // Then
  1168. XCTAssertTrue(firstResponse?.result.isSuccess == true)
  1169. XCTAssertTrue(secondResponse?.result.isSuccess == true)
  1170. }
  1171. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1172. func testThatMultipleDownloadRequestPublishersCanBeChained() {
  1173. // Given
  1174. let responseReceived = expectation(description: "combined response should be received")
  1175. let completionReceived = expectation(description: "combined stream should complete")
  1176. let customValue = "CustomValue"
  1177. var firstResponse: DownloadResponse<TestResponse, AFError>?
  1178. var secondResponse: DownloadResponse<TestResponse, AFError>?
  1179. // When
  1180. store {
  1181. AF.download(.default)
  1182. .publishDecodable(type: TestResponse.self)
  1183. .flatMap { response -> DownloadResponsePublisher<TestResponse> in
  1184. firstResponse = response
  1185. let request = Endpoint(headers: ["X-Custom": customValue])
  1186. return AF.download(request)
  1187. .publishDecodable(type: TestResponse.self)
  1188. }
  1189. .sink(receiveCompletion: { _ in completionReceived.fulfill() }) { response in
  1190. secondResponse = response
  1191. responseReceived.fulfill()
  1192. }
  1193. }
  1194. waitForExpectations(timeout: timeout)
  1195. // Then
  1196. XCTAssertTrue(firstResponse?.result.isSuccess == true)
  1197. XCTAssertTrue(secondResponse?.result.isSuccess == true)
  1198. XCTAssertEqual(secondResponse?.value?.headers["X-Custom"], customValue)
  1199. }
  1200. }
  1201. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1202. class CombineTestCase: BaseTestCase {
  1203. private lazy var storage: Set<AnyCancellable> = { Set<AnyCancellable>() }()
  1204. override func tearDown() {
  1205. storage.removeAll()
  1206. super.tearDown()
  1207. }
  1208. func store(_ toStore: () -> AnyCancellable) {
  1209. storage.insert(toStore())
  1210. }
  1211. }
  1212. #endif