RequestTests.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. //
  2. // RequestTests.swift
  3. //
  4. // Copyright (c) 2014-2018 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. import Alamofire
  25. import Foundation
  26. import XCTest
  27. // TODO: Evaulate need for request creation hooks?
  28. // TODO: Are init tests useful? If so, make them real init tests.
  29. //class RequestInitializationTestCase: BaseTestCase {
  30. // func testDataRequestInitializer() {
  31. // // Given
  32. //
  33. // }
  34. //
  35. // func testRequestClassMethodWithMethodAndURL() {
  36. // // Given
  37. // let urlString = "https://httpbin.org/get"
  38. //
  39. // // When
  40. // let request = Alamofire.request(urlString)
  41. //
  42. // // Then
  43. // XCTAssertNotNil(request.request)
  44. // XCTAssertEqual(request.request?.httpMethod, "GET")
  45. // XCTAssertEqual(request.request?.url?.absoluteString, urlString)
  46. // XCTAssertNil(request.response)
  47. // }
  48. //
  49. // func testRequestClassMethodWithMethodAndURLAndParameters() {
  50. // // Given
  51. // let urlString = "https://httpbin.org/get"
  52. //
  53. // // When
  54. // let request = Alamofire.request(urlString, parameters: ["foo": "bar"])
  55. //
  56. // // Then
  57. // XCTAssertNotNil(request.request)
  58. // XCTAssertEqual(request.request?.httpMethod, "GET")
  59. // XCTAssertNotEqual(request.request?.url?.absoluteString, urlString)
  60. // XCTAssertEqual(request.request?.url?.query, "foo=bar")
  61. // XCTAssertNil(request.response)
  62. // }
  63. //
  64. // func testRequestClassMethodWithMethodURLParametersAndHeaders() {
  65. // // Given
  66. // let urlString = "https://httpbin.org/get"
  67. // let headers = ["Authorization": "123456"]
  68. //
  69. // // When
  70. // let request = Alamofire.request(urlString, parameters: ["foo": "bar"], headers: headers)
  71. //
  72. // // Then
  73. // XCTAssertNotNil(request.request)
  74. // XCTAssertEqual(request.request?.httpMethod, "GET")
  75. // XCTAssertNotEqual(request.request?.url?.absoluteString, urlString)
  76. // XCTAssertEqual(request.request?.url?.query, "foo=bar")
  77. // XCTAssertEqual(request.request?.value(forHTTPHeaderField: "Authorization"), "123456")
  78. // XCTAssertNil(request.response)
  79. // }
  80. //}
  81. // MARK: -
  82. //class RequestSubclassRequestPropertyTestCase: BaseTestCase {
  83. // private enum AuthenticationError: Error {
  84. // case expiredAccessToken
  85. // }
  86. //
  87. // private class AuthenticationAdapter: RequestAdapter {
  88. // func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
  89. // throw AuthenticationError.expiredAccessToken
  90. // }
  91. // }
  92. //
  93. // private var sessionManager: SessionManager!
  94. //
  95. // override func setUp() {
  96. // super.setUp()
  97. //
  98. // sessionManager = SessionManager()
  99. // sessionManager.startRequestsImmediately = false
  100. //
  101. // sessionManager.adapter = AuthenticationAdapter()
  102. // }
  103. //
  104. // func testDataRequestHasURLRequest() {
  105. // // Given
  106. // let urlString = "https://httpbin.org/"
  107. //
  108. // // When
  109. // let request = sessionManager.request(urlString)
  110. //
  111. // // Then
  112. // XCTAssertNotNil(request.request)
  113. // XCTAssertEqual(request.request?.httpMethod, "GET")
  114. // XCTAssertEqual(request.request?.url?.absoluteString, urlString)
  115. // XCTAssertNil(request.response)
  116. // }
  117. //
  118. // func testDownloadRequestHasURLRequest() {
  119. // // Given
  120. // let urlString = "https://httpbin.org/"
  121. //
  122. // // When
  123. // let request = sessionManager.download(urlString)
  124. //
  125. // // Then
  126. // XCTAssertNotNil(request.request)
  127. // XCTAssertEqual(request.request?.httpMethod, "GET")
  128. // XCTAssertEqual(request.request?.url?.absoluteString, urlString)
  129. // XCTAssertNil(request.response)
  130. // }
  131. //
  132. // func testUploadDataRequestHasURLRequest() {
  133. // // Given
  134. // let urlString = "https://httpbin.org/"
  135. //
  136. // // When
  137. // let request = sessionManager.upload(Data(), to: urlString)
  138. //
  139. // // Then
  140. // XCTAssertNotNil(request.request)
  141. // XCTAssertEqual(request.request?.httpMethod, "POST")
  142. // XCTAssertEqual(request.request?.url?.absoluteString, urlString)
  143. // XCTAssertNil(request.response)
  144. // }
  145. //
  146. // func testUploadFileRequestHasURLRequest() {
  147. // // Given
  148. // let urlString = "https://httpbin.org/"
  149. // let imageURL = url(forResource: "rainbow", withExtension: "jpg")
  150. //
  151. // // When
  152. // let request = sessionManager.upload(imageURL, to: urlString)
  153. //
  154. // // Then
  155. // XCTAssertNotNil(request.request)
  156. // XCTAssertEqual(request.request?.httpMethod, "POST")
  157. // XCTAssertEqual(request.request?.url?.absoluteString, urlString)
  158. // XCTAssertNil(request.response)
  159. // }
  160. //
  161. // func testUploadStreamRequestHasURLRequest() {
  162. // // Given
  163. // let urlString = "https://httpbin.org/"
  164. // let imageURL = url(forResource: "rainbow", withExtension: "jpg")
  165. // let imageStream = InputStream(url: imageURL)!
  166. //
  167. // // When
  168. // let request = sessionManager.upload(imageStream, to: urlString)
  169. //
  170. // // Then
  171. // XCTAssertNotNil(request.request)
  172. // XCTAssertEqual(request.request?.httpMethod, "POST")
  173. // XCTAssertEqual(request.request?.url?.absoluteString, urlString)
  174. // XCTAssertNil(request.response)
  175. // }
  176. //}
  177. // MARK: -
  178. class RequestResponseTestCase: BaseTestCase {
  179. func testRequestResponse() {
  180. // Given
  181. let urlString = "https://httpbin.org/get"
  182. let expectation = self.expectation(description: "GET request should succeed: \(urlString)")
  183. var response: DataResponse<Data?>?
  184. // When
  185. Alamofire.request(urlString, parameters: ["foo": "bar"])
  186. .response { resp in
  187. response = resp
  188. expectation.fulfill()
  189. }
  190. waitForExpectations(timeout: timeout, handler: nil)
  191. // Then
  192. XCTAssertNotNil(response?.request)
  193. XCTAssertNotNil(response?.response)
  194. XCTAssertNotNil(response?.data)
  195. XCTAssertNil(response?.error)
  196. }
  197. // func testRequestResponseWithProgress() {
  198. // // Given
  199. // let randomBytes = 4 * 1024 * 1024
  200. // let urlString = "https://httpbin.org/bytes/\(randomBytes)"
  201. //
  202. // let expectation = self.expectation(description: "Bytes download progress should be reported: \(urlString)")
  203. //
  204. // var progressValues: [Double] = []
  205. // var response: DataResponse<Data?>?
  206. //
  207. // // When
  208. // Alamofire.request(urlString)
  209. // .downloadProgress { progress in
  210. // progressValues.append(progress.fractionCompleted)
  211. // }
  212. // .response { resp in
  213. // response = resp
  214. // expectation.fulfill()
  215. // }
  216. //
  217. // waitForExpectations(timeout: timeout, handler: nil)
  218. //
  219. // // Then
  220. // XCTAssertNotNil(response?.request)
  221. // XCTAssertNotNil(response?.response)
  222. // XCTAssertNotNil(response?.data)
  223. // XCTAssertNil(response?.error)
  224. //
  225. // var previousProgress: Double = progressValues.first ?? 0.0
  226. //
  227. // for progress in progressValues {
  228. // XCTAssertGreaterThanOrEqual(progress, previousProgress)
  229. // previousProgress = progress
  230. // }
  231. //
  232. // if let lastProgressValue = progressValues.last {
  233. // XCTAssertEqual(lastProgressValue, 1.0)
  234. // } else {
  235. // XCTFail("last item in progressValues should not be nil")
  236. // }
  237. // }
  238. //
  239. // func testRequestResponseWithStream() {
  240. // // Given
  241. // let randomBytes = 4 * 1024 * 1024
  242. // let urlString = "https://httpbin.org/bytes/\(randomBytes)"
  243. //
  244. // let expectation = self.expectation(description: "Bytes download progress should be reported: \(urlString)")
  245. //
  246. // var progressValues: [Double] = []
  247. // var accumulatedData = [Data]()
  248. // var response: DataResponse<Data?>?
  249. //
  250. // // When
  251. // Alamofire.request(urlString)
  252. // .downloadProgress { progress in
  253. // progressValues.append(progress.fractionCompleted)
  254. // }
  255. // .stream { data in
  256. // accumulatedData.append(data)
  257. // }
  258. // .response { resp in
  259. // response = resp
  260. // expectation.fulfill()
  261. // }
  262. //
  263. // waitForExpectations(timeout: timeout, handler: nil)
  264. //
  265. // // Then
  266. // XCTAssertNotNil(response?.request)
  267. // XCTAssertNotNil(response?.response)
  268. // XCTAssertNil(response?.data)
  269. // XCTAssertNil(response?.error)
  270. // XCTAssertGreaterThanOrEqual(accumulatedData.count, 1)
  271. //
  272. // var previousProgress: Double = progressValues.first ?? 0.0
  273. //
  274. // for progress in progressValues {
  275. // XCTAssertGreaterThanOrEqual(progress, previousProgress)
  276. // previousProgress = progress
  277. // }
  278. //
  279. // if let lastProgress = progressValues.last {
  280. // XCTAssertEqual(lastProgress, 1.0)
  281. // } else {
  282. // XCTFail("last item in progressValues should not be nil")
  283. // }
  284. // }
  285. //
  286. func testPOSTRequestWithUnicodeParameters() {
  287. // Given
  288. let urlString = "https://httpbin.org/post"
  289. let parameters = [
  290. "french": "français",
  291. "japanese": "日本語",
  292. "arabic": "العربية",
  293. "emoji": "😃"
  294. ]
  295. let expectation = self.expectation(description: "request should succeed")
  296. var response: DataResponse<Any>?
  297. // When
  298. Alamofire.request(urlString, method: .post, parameters: parameters)
  299. .responseJSON { closureResponse in
  300. response = closureResponse
  301. expectation.fulfill()
  302. }
  303. waitForExpectations(timeout: timeout, handler: nil)
  304. // Then
  305. XCTAssertNotNil(response?.request)
  306. XCTAssertNotNil(response?.response)
  307. XCTAssertNotNil(response?.data)
  308. if let json = response?.result.value as? [String: Any], let form = json["form"] as? [String: String] {
  309. XCTAssertEqual(form["french"], parameters["french"])
  310. XCTAssertEqual(form["japanese"], parameters["japanese"])
  311. XCTAssertEqual(form["arabic"], parameters["arabic"])
  312. XCTAssertEqual(form["emoji"], parameters["emoji"])
  313. } else {
  314. XCTFail("form parameter in JSON should not be nil")
  315. }
  316. }
  317. func testPOSTRequestWithBase64EncodedImages() {
  318. // Given
  319. let urlString = "https://httpbin.org/post"
  320. let pngBase64EncodedString: String = {
  321. let URL = url(forResource: "unicorn", withExtension: "png")
  322. let data = try! Data(contentsOf: URL)
  323. return data.base64EncodedString(options: .lineLength64Characters)
  324. }()
  325. let jpegBase64EncodedString: String = {
  326. let URL = url(forResource: "rainbow", withExtension: "jpg")
  327. let data = try! Data(contentsOf: URL)
  328. return data.base64EncodedString(options: .lineLength64Characters)
  329. }()
  330. let parameters = [
  331. "email": "user@alamofire.org",
  332. "png_image": pngBase64EncodedString,
  333. "jpeg_image": jpegBase64EncodedString
  334. ]
  335. let expectation = self.expectation(description: "request should succeed")
  336. var response: DataResponse<Any>?
  337. // When
  338. Alamofire.request(urlString, method: .post, parameters: parameters)
  339. .responseJSON { closureResponse in
  340. response = closureResponse
  341. expectation.fulfill()
  342. }
  343. waitForExpectations(timeout: timeout, handler: nil)
  344. // Then
  345. XCTAssertNotNil(response?.request)
  346. XCTAssertNotNil(response?.response)
  347. XCTAssertNotNil(response?.data)
  348. XCTAssertEqual(response?.result.isSuccess, true)
  349. if let json = response?.result.value as? [String: Any], let form = json["form"] as? [String: String] {
  350. XCTAssertEqual(form["email"], parameters["email"])
  351. XCTAssertEqual(form["png_image"], parameters["png_image"])
  352. XCTAssertEqual(form["jpeg_image"], parameters["jpeg_image"])
  353. } else {
  354. XCTFail("form parameter in JSON should not be nil")
  355. }
  356. }
  357. }
  358. // MARK: -
  359. extension Request {
  360. fileprivate func preValidate(operation: @escaping () -> Void) -> Self {
  361. internalQueue.addOperation {
  362. operation()
  363. }
  364. return self
  365. }
  366. fileprivate func postValidate(operation: @escaping () -> Void) -> Self {
  367. internalQueue.addOperation {
  368. operation()
  369. }
  370. return self
  371. }
  372. }
  373. // MARK: -
  374. // TODO: Do we still want this API?
  375. class RequestExtensionTestCase: BaseTestCase {
  376. func testThatRequestExtensionHasAccessToTaskDelegateQueue() {
  377. // Given
  378. let urlString = "https://httpbin.org/get"
  379. let expectation = self.expectation(description: "GET request should succeed: \(urlString)")
  380. var responses: [String] = []
  381. // When
  382. Alamofire.request(urlString)
  383. .preValidate {
  384. responses.append("preValidate")
  385. }
  386. .validate()
  387. .postValidate {
  388. responses.append("postValidate")
  389. }
  390. .response { _ in
  391. responses.append("response")
  392. expectation.fulfill()
  393. }
  394. waitForExpectations(timeout: timeout, handler: nil)
  395. // Then
  396. if responses.count == 3 {
  397. XCTAssertEqual(responses[0], "preValidate")
  398. XCTAssertEqual(responses[1], "postValidate")
  399. XCTAssertEqual(responses[2], "response")
  400. } else {
  401. XCTFail("responses count should be equal to 3")
  402. }
  403. }
  404. }
  405. // MARK: -
  406. //class RequestDescriptionTestCase: BaseTestCase {
  407. // func testRequestDescription() {
  408. // // Given
  409. // let urlString = "https://httpbin.org/get"
  410. // let request = Alamofire.request(urlString)
  411. // let initialRequestDescription = request.description
  412. //
  413. // let expectation = self.expectation(description: "Request description should update: \(urlString)")
  414. //
  415. // var finalRequestDescription: String?
  416. // var response: HTTPURLResponse?
  417. //
  418. // // When
  419. // request.response { resp in
  420. // finalRequestDescription = request.description
  421. // response = resp.response
  422. //
  423. // expectation.fulfill()
  424. // }
  425. //
  426. // waitForExpectations(timeout: timeout, handler: nil)
  427. //
  428. // // Then
  429. // XCTAssertEqual(initialRequestDescription, "GET https://httpbin.org/get")
  430. // XCTAssertEqual(finalRequestDescription, "GET https://httpbin.org/get (\(response?.statusCode ?? -1))")
  431. // }
  432. //}
  433. // MARK: -
  434. //class RequestDebugDescriptionTestCase: BaseTestCase {
  435. // // MARK: Properties
  436. //
  437. // let manager: SessionManager = {
  438. // let manager = SessionManager(configuration: .default)
  439. // manager.startRequestsImmediately = false
  440. // return manager
  441. // }()
  442. //
  443. // let managerWithAcceptLanguageHeader: SessionManager = {
  444. // var headers = SessionManager.default.session.configuration.httpAdditionalHeaders ?? [:]
  445. // headers["Accept-Language"] = "en-US"
  446. //
  447. // let configuration = URLSessionConfiguration.default
  448. // configuration.httpAdditionalHeaders = headers
  449. //
  450. // let manager = SessionManager(configuration: configuration)
  451. // manager.startRequestsImmediately = false
  452. //
  453. // return manager
  454. // }()
  455. //
  456. // let managerWithContentTypeHeader: SessionManager = {
  457. // var headers = SessionManager.default.session.configuration.httpAdditionalHeaders ?? [:]
  458. // headers["Content-Type"] = "application/json"
  459. //
  460. // let configuration = URLSessionConfiguration.default
  461. // configuration.httpAdditionalHeaders = headers
  462. //
  463. // let manager = SessionManager(configuration: configuration)
  464. // manager.startRequestsImmediately = false
  465. //
  466. // return manager
  467. // }()
  468. //
  469. // let managerDisallowingCookies: SessionManager = {
  470. // let configuration = URLSessionConfiguration.default
  471. // configuration.httpShouldSetCookies = false
  472. //
  473. // let manager = SessionManager(configuration: configuration)
  474. // manager.startRequestsImmediately = false
  475. //
  476. // return manager
  477. // }()
  478. //
  479. // // MARK: Tests
  480. //
  481. // func testGETRequestDebugDescription() {
  482. // // Given
  483. // let urlString = "https://httpbin.org/get"
  484. //
  485. // // When
  486. // let request = manager.request(urlString)
  487. // let components = cURLCommandComponents(for: request)
  488. //
  489. // // Then
  490. // XCTAssertEqual(components[0..<3], ["$", "curl", "-v"])
  491. // XCTAssertFalse(components.contains("-X"))
  492. // XCTAssertEqual(components.last, "\"\(urlString)\"")
  493. // }
  494. //
  495. // func testGETRequestWithJSONHeaderDebugDescription() {
  496. // // Given
  497. // let urlString = "https://httpbin.org/get"
  498. //
  499. // // When
  500. // let headers: [String: String] = [ "X-Custom-Header": "{\"key\": \"value\"}" ]
  501. // let request = manager.request(urlString, headers: headers)
  502. //
  503. // // Then
  504. // XCTAssertNotNil(request.debugDescription.range(of: "-H \"X-Custom-Header: {\\\"key\\\": \\\"value\\\"}\""))
  505. // }
  506. //
  507. // func testGETRequestWithDuplicateHeadersDebugDescription() {
  508. // // Given
  509. // let urlString = "https://httpbin.org/get"
  510. //
  511. // // When
  512. // let headers = [ "Accept-Language": "en-GB" ]
  513. // let request = managerWithAcceptLanguageHeader.request(urlString, headers: headers)
  514. // let components = cURLCommandComponents(for: request)
  515. //
  516. // // Then
  517. // XCTAssertEqual(components[0..<3], ["$", "curl", "-v"])
  518. // XCTAssertFalse(components.contains("-X"))
  519. // XCTAssertEqual(components.last, "\"\(urlString)\"")
  520. //
  521. // let tokens = request.debugDescription.components(separatedBy: "Accept-Language:")
  522. // XCTAssertTrue(tokens.count == 2, "command should contain a single Accept-Language header")
  523. //
  524. // XCTAssertNotNil(request.debugDescription.range(of: "-H \"Accept-Language: en-GB\""))
  525. // }
  526. //
  527. // func testPOSTRequestDebugDescription() {
  528. // // Given
  529. // let urlString = "https://httpbin.org/post"
  530. //
  531. // // When
  532. // let request = manager.request(urlString, method: .post)
  533. // let components = cURLCommandComponents(for: request)
  534. //
  535. // // Then
  536. // XCTAssertEqual(components[0..<3], ["$", "curl", "-v"])
  537. // XCTAssertEqual(components[3..<5], ["-X", "POST"])
  538. // XCTAssertEqual(components.last, "\"\(urlString)\"")
  539. // }
  540. //
  541. // func testPOSTRequestWithJSONParametersDebugDescription() {
  542. // // Given
  543. // let urlString = "https://httpbin.org/post"
  544. //
  545. // let parameters = [
  546. // "foo": "bar",
  547. // "fo\"o": "b\"ar",
  548. // "f'oo": "ba'r"
  549. // ]
  550. //
  551. // // When
  552. // let request = manager.request(urlString, method: .post, parameters: parameters, encoding: JSONEncoding.default)
  553. // let components = cURLCommandComponents(for: request)
  554. //
  555. // // Then
  556. // XCTAssertEqual(components[0..<3], ["$", "curl", "-v"])
  557. // XCTAssertEqual(components[3..<5], ["-X", "POST"])
  558. //
  559. // XCTAssertNotNil(request.debugDescription.range(of: "-H \"Content-Type: application/json\""))
  560. // XCTAssertNotNil(request.debugDescription.range(of: "-d \"{"))
  561. // XCTAssertNotNil(request.debugDescription.range(of: "\\\"f'oo\\\":\\\"ba'r\\\""))
  562. // XCTAssertNotNil(request.debugDescription.range(of: "\\\"fo\\\\\\\"o\\\":\\\"b\\\\\\\"ar\\\""))
  563. // XCTAssertNotNil(request.debugDescription.range(of: "\\\"foo\\\":\\\"bar\\"))
  564. //
  565. // XCTAssertEqual(components.last, "\"\(urlString)\"")
  566. // }
  567. //
  568. // func testPOSTRequestWithCookieDebugDescription() {
  569. // // Given
  570. // let urlString = "https://httpbin.org/post"
  571. //
  572. // let properties = [
  573. // HTTPCookiePropertyKey.domain: "httpbin.org",
  574. // HTTPCookiePropertyKey.path: "/post",
  575. // HTTPCookiePropertyKey.name: "foo",
  576. // HTTPCookiePropertyKey.value: "bar",
  577. // ]
  578. //
  579. // let cookie = HTTPCookie(properties: properties)!
  580. // manager.session.configuration.httpCookieStorage?.setCookie(cookie)
  581. //
  582. // // When
  583. // let request = manager.request(urlString, method: .post)
  584. // let components = cURLCommandComponents(for: request)
  585. //
  586. // // Then
  587. // XCTAssertEqual(components[0..<3], ["$", "curl", "-v"])
  588. // XCTAssertEqual(components[3..<5], ["-X", "POST"])
  589. // XCTAssertEqual(components.last, "\"\(urlString)\"")
  590. // XCTAssertEqual(components[5..<6], ["-b"])
  591. // }
  592. //
  593. // func testPOSTRequestWithCookiesDisabledDebugDescription() {
  594. // // Given
  595. // let urlString = "https://httpbin.org/post"
  596. //
  597. // let properties = [
  598. // HTTPCookiePropertyKey.domain: "httpbin.org",
  599. // HTTPCookiePropertyKey.path: "/post",
  600. // HTTPCookiePropertyKey.name: "foo",
  601. // HTTPCookiePropertyKey.value: "bar",
  602. // ]
  603. //
  604. // let cookie = HTTPCookie(properties: properties)!
  605. // managerDisallowingCookies.session.configuration.httpCookieStorage?.setCookie(cookie)
  606. //
  607. // // When
  608. // let request = managerDisallowingCookies.request(urlString, method: .post)
  609. // let components = cURLCommandComponents(for: request)
  610. //
  611. // // Then
  612. // let cookieComponents = components.filter { $0 == "-b" }
  613. // XCTAssertTrue(cookieComponents.isEmpty)
  614. // }
  615. //
  616. // func testMultipartFormDataRequestWithDuplicateHeadersDebugDescription() {
  617. // // Given
  618. // let urlString = "https://httpbin.org/post"
  619. // let japaneseData = "日本語".data(using: .utf8, allowLossyConversion: false)!
  620. // let expectation = self.expectation(description: "multipart form data encoding should succeed")
  621. //
  622. // var request: Request?
  623. // var components: [String] = []
  624. //
  625. // // When
  626. // managerWithContentTypeHeader.upload(
  627. // multipartFormData: { multipartFormData in
  628. // multipartFormData.append(japaneseData, withName: "japanese")
  629. // },
  630. // to: urlString,
  631. // encodingCompletion: { result in
  632. // switch result {
  633. // case .success(let upload, _, _):
  634. // request = upload
  635. // components = self.cURLCommandComponents(for: upload)
  636. //
  637. // expectation.fulfill()
  638. // case .failure:
  639. // expectation.fulfill()
  640. // }
  641. // }
  642. // )
  643. //
  644. // waitForExpectations(timeout: timeout, handler: nil)
  645. //
  646. // debugPrint(request!)
  647. //
  648. // // Then
  649. // XCTAssertEqual(components[0..<3], ["$", "curl", "-v"])
  650. // XCTAssertTrue(components.contains("-X"))
  651. // XCTAssertEqual(components.last, "\"\(urlString)\"")
  652. //
  653. // let tokens = request.debugDescription.components(separatedBy: "Content-Type:")
  654. // XCTAssertTrue(tokens.count == 2, "command should contain a single Content-Type header")
  655. //
  656. // XCTAssertNotNil(request.debugDescription.range(of: "-H \"Content-Type: multipart/form-data;"))
  657. // }
  658. //
  659. // func testThatRequestWithInvalidURLDebugDescription() {
  660. // // Given
  661. // let urlString = "invalid_url"
  662. //
  663. // // When
  664. // let request = manager.request(urlString)
  665. // let debugDescription = request.debugDescription
  666. //
  667. // // Then
  668. // XCTAssertNotNil(debugDescription, "debugDescription should not crash")
  669. // }
  670. //
  671. // // MARK: Test Helper Methods
  672. //
  673. // private func cURLCommandComponents(for request: Request) -> [String] {
  674. // let whitespaceCharacterSet = CharacterSet.whitespacesAndNewlines
  675. // return request.debugDescription
  676. // .components(separatedBy: whitespaceCharacterSet)
  677. // .filter { $0 != "" && $0 != "\\" }
  678. // }
  679. //}