CombineTests.swift 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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 canImport(Combine)
  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<HTTPBinResponse, AFError>?
  36. // When
  37. store {
  38. AF.request(URLRequest.makeHTTPBinRequest())
  39. .publishDecodable(type: HTTPBinResponse.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<HTTPBinResponse, AFError>?
  54. // When
  55. store {
  56. session.request(URLRequest.makeHTTPBinRequest())
  57. .publishDecodable(type: HTTPBinResponse.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(URLRequest.makeHTTPBinRequest())
  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(URLRequest.makeHTTPBinRequest())
  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(URLRequest.makeHTTPBinRequest())
  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<HTTPBinResponse, AFError>?
  125. var publishedResponse: DataResponse<HTTPBinResponse, AFError>?
  126. // When
  127. store {
  128. AF.request(URLRequest.makeHTTPBinRequest())
  129. .responseDecodable(of: HTTPBinResponse.self) { handlerResponse = $0; handlerResponseReceived.fulfill() }
  130. .publishDecodable(type: HTTPBinResponse.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<HTTPBinResponse, AFError>?
  145. // When
  146. store {
  147. AF.request(URLRequest.makeHTTPBinRequest())
  148. .publishDecodable(type: HTTPBinResponse.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: HTTPBinResponse?
  163. // When
  164. store {
  165. AF.request(URLRequest.makeHTTPBinRequest())
  166. .publishDecodable(type: HTTPBinResponse.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(URLRequest.makeHTTPBinRequest(path: "delay/1", timeout: 0.1))
  183. .publishDecodable(type: HTTPBinResponse.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<HTTPBinResponse, AFError>?
  206. // When
  207. let request = AF.request(URLRequest.makeHTTPBinRequest())
  208. let publisher = request.publishDecodable(type: HTTPBinResponse.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<HTTPBinResponse, AFError>?
  229. // When
  230. store {
  231. AF.request(URLRequest.makeHTTPBinRequest())
  232. .publishDecodable(type: HTTPBinResponse.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<HTTPBinResponse, AFError>?
  253. // When
  254. store {
  255. AF.request(URLRequest.makeHTTPBinRequest())
  256. .publishDecodable(type: HTTPBinResponse.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<HTTPBinResponse, AFError>?
  279. // When
  280. store {
  281. AF.request(URLRequest.makeHTTPBinRequest())
  282. .publishDecodable(type: HTTPBinResponse.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() {
  298. // Given
  299. let responseReceived = expectation(description: "response should be received")
  300. let completionReceived = expectation(description: "stream should complete")
  301. var response: DataResponse<HTTPBinResponse, AFError>?
  302. // When
  303. let request = AF.request(URLRequest.makeHTTPBinRequest())
  304. var token: AnyCancellable? = request
  305. .publishDecodable(type: HTTPBinResponse.self)
  306. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  307. receiveValue: { response = $0; responseReceived.fulfill() })
  308. token = nil
  309. waitForExpectations(timeout: timeout)
  310. // Then
  311. XCTAssertTrue(response?.result.isFailure == true)
  312. XCTAssertTrue(response?.error?.isExplicitlyCancelledError == true,
  313. "error is not explicitly cancelled but \(response?.error?.localizedDescription ?? "None")")
  314. XCTAssertTrue(request.isCancelled)
  315. XCTAssertNil(token)
  316. }
  317. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  318. func testThatPublishedDataRequestCanBeCancelledManually() {
  319. // Given
  320. let responseReceived = expectation(description: "response should be received")
  321. let completionReceived = expectation(description: "stream should complete")
  322. var response: DataResponse<HTTPBinResponse, AFError>?
  323. // When
  324. let request = AF.request(URLRequest.makeHTTPBinRequest())
  325. store {
  326. request
  327. .publishDecodable(type: HTTPBinResponse.self)
  328. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  329. receiveValue: { response = $0; responseReceived.fulfill() })
  330. }
  331. request.cancel()
  332. waitForExpectations(timeout: timeout)
  333. // Then
  334. XCTAssertTrue(response?.result.isFailure == true)
  335. XCTAssertTrue(response?.error?.isExplicitlyCancelledError == true,
  336. "error is not explicitly cancelled but \(response?.error?.localizedDescription ?? "None")")
  337. XCTAssertTrue(request.isCancelled)
  338. }
  339. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  340. func testThatMultipleDataRequestPublishersCanBeCombined() {
  341. // Given
  342. let responseReceived = expectation(description: "combined response should be received")
  343. let completionReceived = expectation(description: "combined stream should complete")
  344. var firstResponse: DataResponse<HTTPBinResponse, AFError>?
  345. var secondResponse: DataResponse<HTTPBinResponse, AFError>?
  346. // When
  347. let first = AF.request(URLRequest.makeHTTPBinRequest())
  348. .publishDecodable(type: HTTPBinResponse.self)
  349. let second = AF.request(URLRequest.makeHTTPBinRequest())
  350. .publishDecodable(type: HTTPBinResponse.self)
  351. store {
  352. Publishers.CombineLatest(first, second)
  353. .sink(receiveCompletion: { _ in completionReceived.fulfill() }) { first, second in
  354. firstResponse = first
  355. secondResponse = second
  356. responseReceived.fulfill()
  357. }
  358. }
  359. waitForExpectations(timeout: timeout)
  360. // Then
  361. XCTAssertTrue(firstResponse?.result.isSuccess == true)
  362. XCTAssertTrue(secondResponse?.result.isSuccess == true)
  363. }
  364. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  365. func testThatMultipleDataRequestPublishersCanBeChained() {
  366. // Given
  367. let responseReceived = expectation(description: "combined response should be received")
  368. let completionReceived = expectation(description: "combined stream should complete")
  369. let customValue = "CustomValue"
  370. var firstResponse: DataResponse<HTTPBinResponse, AFError>?
  371. var secondResponse: DataResponse<HTTPBinResponse, AFError>?
  372. // When
  373. store {
  374. AF.request(URLRequest.makeHTTPBinRequest())
  375. .publishDecodable(type: HTTPBinResponse.self)
  376. .flatMap { response -> DataResponsePublisher<HTTPBinResponse> in
  377. firstResponse = response
  378. let request = URLRequest.makeHTTPBinRequest(headers: ["X-Custom": customValue])
  379. return AF.request(request)
  380. .publishDecodable(type: HTTPBinResponse.self)
  381. }
  382. .sink(receiveCompletion: { _ in completionReceived.fulfill() }) { response in
  383. secondResponse = response
  384. responseReceived.fulfill()
  385. }
  386. }
  387. waitForExpectations(timeout: timeout)
  388. // Then
  389. XCTAssertTrue(firstResponse?.result.isSuccess == true)
  390. XCTAssertTrue(secondResponse?.result.isSuccess == true)
  391. XCTAssertEqual(secondResponse?.value?.headers["X-Custom"], customValue)
  392. }
  393. }
  394. // MARK: - DataStreamRequest
  395. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  396. final class DataStreamRequestCombineTests: CombineTestCase {
  397. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  398. func testThatDataStreamRequestCanBePublished() {
  399. // Given
  400. let responseReceived = expectation(description: "response should be received")
  401. let completionReceived = expectation(description: "stream should complete")
  402. var result: Result<HTTPBinResponse, AFError>?
  403. // When
  404. store {
  405. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  406. .publishDecodable(type: HTTPBinResponse.self)
  407. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  408. receiveValue: { stream in
  409. switch stream.event {
  410. case let .stream(value):
  411. result = value
  412. case .complete:
  413. responseReceived.fulfill()
  414. }
  415. })
  416. }
  417. waitForExpectations(timeout: timeout)
  418. // Then
  419. XCTAssertNotNil(result?.success)
  420. }
  421. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  422. func testThatNonAutomaticDataStreamRequestCanBePublished() {
  423. // Given
  424. let responseReceived = expectation(description: "response should be received")
  425. let completionReceived = expectation(description: "stream should complete")
  426. let session = Session(startRequestsImmediately: false)
  427. var result: Result<HTTPBinResponse, AFError>?
  428. // When
  429. store {
  430. session.streamRequest(URLRequest.makeHTTPBinRequest())
  431. .publishDecodable(type: HTTPBinResponse.self)
  432. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  433. receiveValue: { stream in
  434. switch stream.event {
  435. case let .stream(value):
  436. result = value
  437. case .complete:
  438. responseReceived.fulfill()
  439. }
  440. })
  441. }
  442. waitForExpectations(timeout: timeout)
  443. // Then
  444. XCTAssertNotNil(result?.success)
  445. }
  446. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  447. func testThatDataStreamRequestCanPublishData() {
  448. // Given
  449. let responseReceived = expectation(description: "response should be received")
  450. let completionReceived = expectation(description: "stream should complete")
  451. var result: Result<Data, AFError>?
  452. // When
  453. store {
  454. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  455. .publishData()
  456. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  457. receiveValue: { stream in
  458. switch stream.event {
  459. case let .stream(value):
  460. result = value
  461. case .complete:
  462. responseReceived.fulfill()
  463. }
  464. })
  465. }
  466. waitForExpectations(timeout: timeout)
  467. // Then
  468. XCTAssertNotNil(result?.success)
  469. }
  470. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  471. func testThatDataStreamRequestCanPublishString() {
  472. // Given
  473. let responseReceived = expectation(description: "response should be received")
  474. let completionReceived = expectation(description: "stream should complete")
  475. var result: Result<String, AFError>?
  476. // When
  477. store {
  478. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  479. .publishString()
  480. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  481. receiveValue: { stream in
  482. switch stream.event {
  483. case let .stream(value):
  484. result = value
  485. case .complete:
  486. responseReceived.fulfill()
  487. }
  488. })
  489. }
  490. waitForExpectations(timeout: timeout)
  491. // Then
  492. XCTAssertNotNil(result?.success)
  493. }
  494. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  495. func testThatDataStreamRequestCanBePublishedWithMultipleHandlers() {
  496. // Given
  497. let handlerResponseReceived = expectation(description: "handler response should be received")
  498. let publishedResponseReceived = expectation(description: "response should be received")
  499. let completionReceived = expectation(description: "stream should complete")
  500. var handlerResult: Result<HTTPBinResponse, AFError>?
  501. var publishedResult: Result<HTTPBinResponse, AFError>?
  502. // When
  503. store {
  504. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  505. .responseStreamDecodable(of: HTTPBinResponse.self) { stream in
  506. switch stream.event {
  507. case let .stream(value):
  508. handlerResult = value
  509. case .complete:
  510. handlerResponseReceived.fulfill()
  511. }
  512. }
  513. .publishDecodable(type: HTTPBinResponse.self)
  514. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  515. receiveValue: { stream in
  516. switch stream.event {
  517. case let .stream(value):
  518. publishedResult = value
  519. case .complete:
  520. publishedResponseReceived.fulfill()
  521. }
  522. })
  523. }
  524. waitForExpectations(timeout: timeout)
  525. // Then
  526. XCTAssertNotNil(handlerResult?.success)
  527. XCTAssertNotNil(publishedResult?.success)
  528. }
  529. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  530. func testThatDataStreamRequestCanPublishResult() {
  531. // Given
  532. let responseReceived = expectation(description: "response should be received")
  533. let completionReceived = expectation(description: "stream should complete")
  534. var result: Result<HTTPBinResponse, AFError>?
  535. // When
  536. store {
  537. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  538. .publishDecodable(type: HTTPBinResponse.self)
  539. .result()
  540. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  541. receiveValue: { received in
  542. result = received
  543. responseReceived.fulfill()
  544. })
  545. }
  546. waitForExpectations(timeout: timeout)
  547. // Then
  548. XCTAssertNotNil(result?.success)
  549. }
  550. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  551. func testThatDataStreamRequestCanPublishResultWithResponseFailure() {
  552. // Given
  553. let responseReceived = expectation(description: "response should be received")
  554. let completionReceived = expectation(description: "stream should complete")
  555. var result: Result<HTTPBinResponse, AFError>?
  556. // When
  557. store {
  558. AF.streamRequest(URLRequest.makeHTTPBinRequest(path: "delay/1", timeout: 0.1))
  559. .publishDecodable(type: HTTPBinResponse.self)
  560. .result()
  561. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  562. receiveValue: { received in
  563. result = received
  564. responseReceived.fulfill()
  565. })
  566. }
  567. waitForExpectations(timeout: timeout)
  568. // Then
  569. XCTAssertNil(result?.success)
  570. XCTAssertEqual((result?.failure?.underlyingError as? URLError)?.code, .timedOut)
  571. }
  572. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  573. func testThatDataStreamRequestCanPublishValue() {
  574. // Given
  575. let responseReceived = expectation(description: "response should be received")
  576. let completionReceived = expectation(description: "stream should complete")
  577. var response: HTTPBinResponse?
  578. // When
  579. store {
  580. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  581. .publishDecodable(type: HTTPBinResponse.self)
  582. .value()
  583. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  584. receiveValue: { received in
  585. response = received
  586. responseReceived.fulfill()
  587. })
  588. }
  589. waitForExpectations(timeout: timeout)
  590. // Then
  591. XCTAssertNotNil(response)
  592. }
  593. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  594. func testThatDataStreamRequestCanPublishValueWithFailure() {
  595. // Given
  596. let completionReceived = expectation(description: "stream should complete")
  597. var error: AFError?
  598. // When
  599. store {
  600. AF.streamRequest(URLRequest.makeHTTPBinRequest(path: "delay/1", timeout: 0.1))
  601. .publishDecodable(type: HTTPBinResponse.self)
  602. .value()
  603. .sink(receiveCompletion: { completion in
  604. switch completion {
  605. case let .failure(err):
  606. error = err
  607. case .finished:
  608. error = nil
  609. }
  610. completionReceived.fulfill()
  611. }, receiveValue: { _ in })
  612. }
  613. waitForExpectations(timeout: timeout)
  614. // Then
  615. XCTAssertNotNil(error)
  616. XCTAssertEqual((error?.underlyingError as? URLError)?.code, .timedOut)
  617. }
  618. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  619. func testThatPublishedDataStreamRequestIsNotResumedUnlessSubscribed() {
  620. // Given
  621. let responseReceived = expectation(description: "response should be received")
  622. let completionReceived = expectation(description: "stream should complete")
  623. var result: Result<HTTPBinResponse, AFError>?
  624. // When
  625. let request = AF.streamRequest(URLRequest.makeHTTPBinRequest())
  626. let publisher = request.publishDecodable(type: HTTPBinResponse.self)
  627. let stateAfterPublisher = request.state
  628. store {
  629. publisher.sink(receiveCompletion: { _ in completionReceived.fulfill() },
  630. receiveValue: { stream in
  631. switch stream.event {
  632. case let .stream(value):
  633. result = value
  634. case .complete:
  635. responseReceived.fulfill()
  636. }
  637. })
  638. }
  639. let stateAfterSubscription = request.state
  640. waitForExpectations(timeout: timeout)
  641. // Then
  642. XCTAssertTrue(result?.isSuccess == true)
  643. XCTAssertEqual(stateAfterPublisher, .initialized)
  644. XCTAssertEqual(stateAfterSubscription, .resumed)
  645. }
  646. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  647. func testThatDataStreamRequestCanSubscribedFromNonMainQueueButPublishedOnMainQueue() {
  648. // Given
  649. let responseReceived = expectation(description: "response should be received")
  650. let completionReceived = expectation(description: "stream should complete")
  651. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  652. var receivedOnMain = false
  653. var result: Result<HTTPBinResponse, AFError>?
  654. // When
  655. store {
  656. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  657. .publishDecodable(type: HTTPBinResponse.self)
  658. .subscribe(on: queue)
  659. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  660. receiveValue: { stream in
  661. receivedOnMain = Thread.isMainThread
  662. switch stream.event {
  663. case let .stream(value):
  664. result = value
  665. case .complete:
  666. responseReceived.fulfill()
  667. }
  668. })
  669. }
  670. waitForExpectations(timeout: timeout)
  671. // Then
  672. XCTAssertNotNil(result?.success)
  673. XCTAssertTrue(receivedOnMain)
  674. }
  675. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  676. func testThatDataStreamRequestPublishedOnSeparateQueueIsReceivedOnThatQueue() {
  677. // Given
  678. let responseReceived = expectation(description: "response should be received")
  679. let completionReceived = expectation(description: "stream should complete")
  680. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  681. var result: Result<HTTPBinResponse, AFError>?
  682. // When
  683. store {
  684. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  685. .publishDecodable(type: HTTPBinResponse.self, queue: queue)
  686. .sink(receiveCompletion: { _ in
  687. dispatchPrecondition(condition: .onQueue(queue))
  688. completionReceived.fulfill()
  689. },
  690. receiveValue: { stream in
  691. dispatchPrecondition(condition: .onQueue(queue))
  692. switch stream.event {
  693. case let .stream(value):
  694. result = value
  695. case .complete:
  696. responseReceived.fulfill()
  697. }
  698. })
  699. }
  700. waitForExpectations(timeout: timeout)
  701. // Then
  702. XCTAssertNotNil(result?.success)
  703. }
  704. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  705. func testThatDataStreamRequestPublishedOnSeparateQueueCanBeReceivedOntoMainQueue() {
  706. // Given
  707. let responseReceived = expectation(description: "response should be received")
  708. let completionReceived = expectation(description: "stream should complete")
  709. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  710. var receivedOnMain = false
  711. var result: Result<HTTPBinResponse, AFError>?
  712. // When
  713. store {
  714. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  715. .publishDecodable(type: HTTPBinResponse.self, queue: queue)
  716. .receive(on: DispatchQueue.main)
  717. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  718. receiveValue: { stream in
  719. receivedOnMain = Thread.isMainThread
  720. switch stream.event {
  721. case let .stream(value):
  722. result = value
  723. case .complete:
  724. responseReceived.fulfill()
  725. }
  726. })
  727. }
  728. waitForExpectations(timeout: timeout)
  729. // Then
  730. XCTAssertNotNil(result?.success)
  731. XCTAssertTrue(receivedOnMain)
  732. }
  733. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  734. func testThatPublishedDataStreamRequestCanBeCancelledAutomatically() {
  735. // Given
  736. let responseReceived = expectation(description: "response should be received")
  737. let completionReceived = expectation(description: "stream should complete")
  738. var error: AFError?
  739. // When
  740. let request = AF.streamRequest(URLRequest.makeHTTPBinRequest())
  741. var token: AnyCancellable? = request
  742. .publishDecodable(type: HTTPBinResponse.self)
  743. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  744. receiveValue: { error = $0.completion?.error; responseReceived.fulfill() })
  745. token = nil
  746. waitForExpectations(timeout: timeout)
  747. // Then
  748. XCTAssertNotNil(error)
  749. XCTAssertTrue(error?.isExplicitlyCancelledError == true,
  750. "error is not explicitly cancelled but \(error?.localizedDescription ?? "None")")
  751. XCTAssertTrue(request.isCancelled)
  752. XCTAssertNil(token)
  753. }
  754. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  755. func testThatPublishedDataStreamRequestCanBeCancelledManually() {
  756. // Given
  757. let responseReceived = expectation(description: "response should be received")
  758. let completionReceived = expectation(description: "stream should complete")
  759. var error: AFError?
  760. // When
  761. let request = AF.streamRequest(URLRequest.makeHTTPBinRequest())
  762. store {
  763. request
  764. .publishDecodable(type: HTTPBinResponse.self)
  765. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  766. receiveValue: { error = $0.completion?.error; responseReceived.fulfill() })
  767. }
  768. request.cancel()
  769. waitForExpectations(timeout: timeout)
  770. // Then
  771. XCTAssertNotNil(error)
  772. XCTAssertTrue(error?.isExplicitlyCancelledError == true,
  773. "error is not explicitly cancelled but \(error?.localizedDescription ?? "None")")
  774. XCTAssertTrue(request.isCancelled)
  775. }
  776. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  777. func testThatMultipleDataStreamPublishersCanBeCombined() {
  778. // Given
  779. let responseReceived = expectation(description: "combined response should be received")
  780. let completionReceived = expectation(description: "combined stream should complete")
  781. var firstCompletion: DataStreamRequest.Completion?
  782. var secondCompletion: DataStreamRequest.Completion?
  783. // When
  784. let first = AF.streamRequest(URLRequest.makeHTTPBinRequest())
  785. .publishDecodable(type: HTTPBinResponse.self)
  786. let second = AF.streamRequest(URLRequest.makeHTTPBinRequest())
  787. .publishDecodable(type: HTTPBinResponse.self)
  788. store {
  789. Publishers.CombineLatest(first.compactMap { $0.completion }, second.compactMap { $0.completion })
  790. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  791. receiveValue: { first, second in
  792. firstCompletion = first
  793. secondCompletion = second
  794. responseReceived.fulfill()
  795. })
  796. }
  797. waitForExpectations(timeout: timeout)
  798. // Then
  799. XCTAssertNotNil(firstCompletion)
  800. XCTAssertNotNil(secondCompletion)
  801. }
  802. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  803. func testThatMultipleDataStreamRequestPublishersCanBeChained() {
  804. // Given
  805. let responseReceived = expectation(description: "combined response should be received")
  806. let completionReceived = expectation(description: "combined stream should complete")
  807. var firstCompletion: DataStreamRequest.Completion?
  808. var secondCompletion: DataStreamRequest.Completion?
  809. // When
  810. store {
  811. AF.streamRequest(URLRequest.makeHTTPBinRequest())
  812. .publishDecodable(type: HTTPBinResponse.self)
  813. .compactMap { $0.completion }
  814. .flatMap { completion -> DataStreamPublisher<HTTPBinResponse> in
  815. firstCompletion = completion
  816. return AF.streamRequest(URLRequest.makeHTTPBinRequest())
  817. .publishDecodable(type: HTTPBinResponse.self)
  818. }
  819. .compactMap { $0.completion }
  820. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  821. receiveValue: { secondCompletion = $0; responseReceived.fulfill() })
  822. }
  823. waitForExpectations(timeout: timeout)
  824. // Then
  825. XCTAssertNotNil(firstCompletion)
  826. XCTAssertNotNil(secondCompletion)
  827. }
  828. }
  829. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  830. final class DownloadRequestCombineTests: CombineTestCase {
  831. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  832. func testThatDownloadRequestCanBePublished() {
  833. // Given
  834. let responseReceived = expectation(description: "response should be received")
  835. let completionReceived = expectation(description: "publisher should complete")
  836. var response: DownloadResponse<HTTPBinResponse, AFError>?
  837. // When
  838. store {
  839. AF.download(URLRequest.makeHTTPBinRequest())
  840. .publishDecodable(type: HTTPBinResponse.self)
  841. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  842. receiveValue: { response = $0; responseReceived.fulfill() })
  843. }
  844. waitForExpectations(timeout: timeout)
  845. // Then
  846. XCTAssertTrue(response?.result.isSuccess == true)
  847. }
  848. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  849. func testThatNonAutomaticDownloadRequestCanBePublished() {
  850. // Given
  851. let responseReceived = expectation(description: "response should be received")
  852. let completionReceived = expectation(description: "publisher should complete")
  853. let session = Session(startRequestsImmediately: false)
  854. var response: DownloadResponse<HTTPBinResponse, AFError>?
  855. // When
  856. store {
  857. session.download(URLRequest.makeHTTPBinRequest())
  858. .publishDecodable(type: HTTPBinResponse.self)
  859. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  860. receiveValue: { response = $0; responseReceived.fulfill() })
  861. }
  862. waitForExpectations(timeout: timeout)
  863. // Then
  864. XCTAssertTrue(response?.result.isSuccess == true)
  865. }
  866. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  867. func testThatDownloadRequestCanPublishData() {
  868. // Given
  869. let responseReceived = expectation(description: "response should be received")
  870. let completionReceived = expectation(description: "publisher should complete")
  871. var response: DownloadResponse<Data, AFError>?
  872. // When
  873. store {
  874. AF.download(URLRequest.makeHTTPBinRequest())
  875. .publishData()
  876. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  877. receiveValue: { response = $0; responseReceived.fulfill() })
  878. }
  879. waitForExpectations(timeout: timeout)
  880. // Then
  881. XCTAssertTrue(response?.result.isSuccess == true)
  882. }
  883. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  884. func testThatDownloadRequestCanPublishString() {
  885. // Given
  886. let responseReceived = expectation(description: "response should be received")
  887. let completionReceived = expectation(description: "publisher should complete")
  888. var response: DownloadResponse<String, AFError>?
  889. // When
  890. store {
  891. AF.download(URLRequest.makeHTTPBinRequest())
  892. .publishString()
  893. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  894. receiveValue: { response = $0; responseReceived.fulfill() })
  895. }
  896. waitForExpectations(timeout: timeout)
  897. // Then
  898. XCTAssertTrue(response?.result.isSuccess == true)
  899. }
  900. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  901. func testThatDownloadRequestCanPublishUnserialized() {
  902. // Given
  903. let responseReceived = expectation(description: "response should be received")
  904. let completionReceived = expectation(description: "publisher should complete")
  905. var response: DownloadResponse<URL?, AFError>?
  906. // When
  907. store {
  908. AF.download(URLRequest.makeHTTPBinRequest())
  909. .publishUnserialized()
  910. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  911. receiveValue: { response = $0; responseReceived.fulfill() })
  912. }
  913. waitForExpectations(timeout: timeout)
  914. // Then
  915. XCTAssertTrue(response?.result.isSuccess == true)
  916. }
  917. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  918. func testThatDownloadRequestCanPublishWithMultipleHandlers() {
  919. // Given
  920. let handlerResponseReceived = expectation(description: "handler response should be received")
  921. let publishedResponseReceived = expectation(description: "published response should be received")
  922. let completionReceived = expectation(description: "stream should complete")
  923. var handlerResponse: DownloadResponse<HTTPBinResponse, AFError>?
  924. var publishedResponse: DownloadResponse<HTTPBinResponse, AFError>?
  925. // When
  926. store {
  927. AF.download(URLRequest.makeHTTPBinRequest())
  928. .responseDecodable(of: HTTPBinResponse.self) { handlerResponse = $0; handlerResponseReceived.fulfill() }
  929. .publishDecodable(type: HTTPBinResponse.self)
  930. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  931. receiveValue: { publishedResponse = $0; publishedResponseReceived.fulfill() })
  932. }
  933. waitForExpectations(timeout: timeout)
  934. // Then
  935. XCTAssertTrue(handlerResponse?.result.isSuccess == true)
  936. XCTAssertTrue(publishedResponse?.result.isSuccess == true)
  937. }
  938. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  939. func testThatDownloadRequestCanPublishResult() {
  940. // Given
  941. let responseReceived = expectation(description: "response should be received")
  942. let completionReceived = expectation(description: "publisher should complete")
  943. var result: Result<HTTPBinResponse, AFError>?
  944. // When
  945. store {
  946. AF.download(URLRequest.makeHTTPBinRequest())
  947. .publishDecodable(type: HTTPBinResponse.self)
  948. .result()
  949. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  950. receiveValue: { result = $0; responseReceived.fulfill() })
  951. }
  952. waitForExpectations(timeout: timeout)
  953. // Then
  954. XCTAssertTrue(result?.isSuccess == true)
  955. }
  956. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  957. func testThatDownloadRequestCanPublishValueWithFailure() {
  958. // Given
  959. let completionReceived = expectation(description: "stream should complete")
  960. var error: AFError?
  961. // When
  962. store {
  963. AF.download(URLRequest.makeHTTPBinRequest(path: "delay/1", timeout: 0.1))
  964. .publishDecodable(type: HTTPBinResponse.self)
  965. .value()
  966. .sink(receiveCompletion: { completion in
  967. switch completion {
  968. case let .failure(err):
  969. error = err
  970. case .finished:
  971. error = nil
  972. }
  973. completionReceived.fulfill()
  974. }, receiveValue: { _ in })
  975. }
  976. waitForExpectations(timeout: timeout)
  977. // Then
  978. XCTAssertNotNil(error)
  979. XCTAssertEqual((error?.underlyingError as? URLError)?.code, .timedOut)
  980. }
  981. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  982. func testThatPublishedDownloadRequestIsNotResumedUnlessSubscribed() {
  983. // Given
  984. let responseReceived = expectation(description: "response should be received")
  985. let completionReceived = expectation(description: "stream should complete")
  986. var response: DownloadResponse<HTTPBinResponse, AFError>?
  987. // When
  988. let request = AF.download(URLRequest.makeHTTPBinRequest())
  989. let publisher = request.publishDecodable(type: HTTPBinResponse.self)
  990. let stateAfterPublisher = request.state
  991. store {
  992. publisher.sink(receiveCompletion: { _ in completionReceived.fulfill() },
  993. receiveValue: { response = $0; responseReceived.fulfill() })
  994. }
  995. let stateAfterSubscription = request.state
  996. waitForExpectations(timeout: timeout)
  997. // Then
  998. XCTAssertTrue(response?.result.isSuccess == true)
  999. XCTAssertEqual(stateAfterPublisher, .initialized)
  1000. XCTAssertEqual(stateAfterSubscription, .resumed)
  1001. }
  1002. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1003. func testThatDownloadRequestCanSubscribedFromNonMainQueueButPublishedOnMainQueue() {
  1004. // Given
  1005. let responseReceived = expectation(description: "response should be received")
  1006. let completionReceived = expectation(description: "stream should complete")
  1007. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  1008. var receivedOnMain = false
  1009. var response: DownloadResponse<HTTPBinResponse, AFError>?
  1010. // When
  1011. store {
  1012. AF.download(URLRequest.makeHTTPBinRequest())
  1013. .publishDecodable(type: HTTPBinResponse.self)
  1014. .subscribe(on: queue)
  1015. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1016. receiveValue: {
  1017. receivedOnMain = Thread.isMainThread
  1018. response = $0
  1019. responseReceived.fulfill()
  1020. })
  1021. }
  1022. waitForExpectations(timeout: timeout)
  1023. // Then
  1024. XCTAssertTrue(response?.result.isSuccess == true)
  1025. XCTAssertTrue(receivedOnMain)
  1026. }
  1027. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1028. func testThatDownloadRequestPublishedOnSeparateQueueIsReceivedOnThatQueue() {
  1029. // Given
  1030. let responseReceived = expectation(description: "response should be received")
  1031. let completionReceived = expectation(description: "stream should complete")
  1032. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  1033. var response: DownloadResponse<HTTPBinResponse, AFError>?
  1034. // When
  1035. store {
  1036. AF.download(URLRequest.makeHTTPBinRequest())
  1037. .publishDecodable(type: HTTPBinResponse.self, queue: queue)
  1038. .sink(receiveCompletion: { _ in
  1039. dispatchPrecondition(condition: .onQueue(queue))
  1040. completionReceived.fulfill()
  1041. },
  1042. receiveValue: {
  1043. dispatchPrecondition(condition: .onQueue(queue))
  1044. response = $0
  1045. responseReceived.fulfill()
  1046. })
  1047. }
  1048. waitForExpectations(timeout: timeout)
  1049. // Then
  1050. XCTAssertTrue(response?.result.isSuccess == true)
  1051. }
  1052. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1053. func testThatDownloadRequestPublishedOnSeparateQueueCanBeReceivedOntoMainQueue() {
  1054. // Given
  1055. let responseReceived = expectation(description: "response should be received")
  1056. let completionReceived = expectation(description: "stream should complete")
  1057. let queue = DispatchQueue(label: "org.alamofire.tests.combineEventQueue")
  1058. var receivedOnMain = false
  1059. var response: DownloadResponse<HTTPBinResponse, AFError>?
  1060. // When
  1061. store {
  1062. AF.download(URLRequest.makeHTTPBinRequest())
  1063. .publishDecodable(type: HTTPBinResponse.self, queue: queue)
  1064. .receive(on: DispatchQueue.main)
  1065. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1066. receiveValue: {
  1067. receivedOnMain = Thread.isMainThread
  1068. response = $0
  1069. responseReceived.fulfill()
  1070. })
  1071. }
  1072. waitForExpectations(timeout: timeout)
  1073. // Then
  1074. XCTAssertTrue(response?.result.isSuccess == true)
  1075. XCTAssertTrue(receivedOnMain)
  1076. }
  1077. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1078. func testThatPublishedDownloadRequestCanBeCancelledAutomatically() {
  1079. // Given
  1080. let responseReceived = expectation(description: "response should be received")
  1081. let completionReceived = expectation(description: "stream should complete")
  1082. var response: DownloadResponse<HTTPBinResponse, AFError>?
  1083. // When
  1084. let request = AF.download(URLRequest.makeHTTPBinRequest())
  1085. var token: AnyCancellable? = request
  1086. .publishDecodable(type: HTTPBinResponse.self)
  1087. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1088. receiveValue: { response = $0; responseReceived.fulfill() })
  1089. token = nil
  1090. waitForExpectations(timeout: timeout)
  1091. // Then
  1092. XCTAssertTrue(response?.result.isFailure == true)
  1093. XCTAssertTrue(response?.error?.isExplicitlyCancelledError == true,
  1094. "error is not explicitly cancelled but \(response?.error?.localizedDescription ?? "None")")
  1095. XCTAssertTrue(request.isCancelled)
  1096. XCTAssertNil(token)
  1097. }
  1098. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1099. func testThatPublishedDownloadRequestCanBeCancelledManually() {
  1100. // Given
  1101. let responseReceived = expectation(description: "response should be received")
  1102. let completionReceived = expectation(description: "stream should complete")
  1103. var response: DownloadResponse<HTTPBinResponse, AFError>?
  1104. // When
  1105. let request = AF.download(URLRequest.makeHTTPBinRequest())
  1106. store {
  1107. request
  1108. .publishDecodable(type: HTTPBinResponse.self)
  1109. .sink(receiveCompletion: { _ in completionReceived.fulfill() },
  1110. receiveValue: { response = $0; responseReceived.fulfill() })
  1111. }
  1112. request.cancel()
  1113. waitForExpectations(timeout: timeout)
  1114. // Then
  1115. XCTAssertTrue(response?.result.isFailure == true)
  1116. XCTAssertTrue(response?.error?.isExplicitlyCancelledError == true,
  1117. "error is not explicitly cancelled but \(response?.error?.localizedDescription ?? "None")")
  1118. XCTAssertTrue(request.isCancelled)
  1119. }
  1120. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1121. func testThatMultipleDownloadRequestPublishersCanBeCombined() {
  1122. // Given
  1123. let responseReceived = expectation(description: "combined response should be received")
  1124. let completionReceived = expectation(description: "combined stream should complete")
  1125. var firstResponse: DownloadResponse<HTTPBinResponse, AFError>?
  1126. var secondResponse: DownloadResponse<HTTPBinResponse, AFError>?
  1127. // When
  1128. let first = AF.download(URLRequest.makeHTTPBinRequest())
  1129. .publishDecodable(type: HTTPBinResponse.self)
  1130. let second = AF.download(URLRequest.makeHTTPBinRequest())
  1131. .publishDecodable(type: HTTPBinResponse.self)
  1132. store {
  1133. Publishers.CombineLatest(first, second)
  1134. .sink(receiveCompletion: { _ in completionReceived.fulfill() }) { first, second in
  1135. firstResponse = first
  1136. secondResponse = second
  1137. responseReceived.fulfill()
  1138. }
  1139. }
  1140. waitForExpectations(timeout: timeout)
  1141. // Then
  1142. XCTAssertTrue(firstResponse?.result.isSuccess == true)
  1143. XCTAssertTrue(secondResponse?.result.isSuccess == true)
  1144. }
  1145. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1146. func testThatMultipleDownloadRequestPublishersCanBeChained() {
  1147. // Given
  1148. let responseReceived = expectation(description: "combined response should be received")
  1149. let completionReceived = expectation(description: "combined stream should complete")
  1150. let customValue = "CustomValue"
  1151. var firstResponse: DownloadResponse<HTTPBinResponse, AFError>?
  1152. var secondResponse: DownloadResponse<HTTPBinResponse, AFError>?
  1153. // When
  1154. store {
  1155. AF.download(URLRequest.makeHTTPBinRequest())
  1156. .publishDecodable(type: HTTPBinResponse.self)
  1157. .flatMap { response -> DownloadResponsePublisher<HTTPBinResponse> in
  1158. firstResponse = response
  1159. let request = URLRequest.makeHTTPBinRequest(headers: ["X-Custom": customValue])
  1160. return AF.download(request)
  1161. .publishDecodable(type: HTTPBinResponse.self)
  1162. }
  1163. .sink(receiveCompletion: { _ in completionReceived.fulfill() }) { response in
  1164. secondResponse = response
  1165. responseReceived.fulfill()
  1166. }
  1167. }
  1168. waitForExpectations(timeout: timeout)
  1169. // Then
  1170. XCTAssertTrue(firstResponse?.result.isSuccess == true)
  1171. XCTAssertTrue(secondResponse?.result.isSuccess == true)
  1172. XCTAssertEqual(secondResponse?.value?.headers["X-Custom"], customValue)
  1173. }
  1174. }
  1175. @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
  1176. class CombineTestCase: BaseTestCase {
  1177. var storage: Set<AnyCancellable> = []
  1178. override func tearDown() {
  1179. storage = []
  1180. super.tearDown()
  1181. }
  1182. func store(_ toStore: () -> AnyCancellable) {
  1183. storage.insert(toStore())
  1184. }
  1185. }
  1186. #endif