RequestTests.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. //
  2. // RequestTests.swift
  3. //
  4. // Copyright (c) 2014-2016 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. class RequestInitializationTestCase: BaseTestCase {
  28. func testRequestClassMethodWithMethodAndURL() {
  29. // Given
  30. let urlString = "https://httpbin.org/"
  31. // When
  32. let request = Alamofire.request(urlString, withMethod: .get)
  33. // Then
  34. XCTAssertNotNil(request.request, "request URL request should not be nil")
  35. XCTAssertEqual(request.request?.httpMethod ?? "", "GET", "request HTTP method should match expected value")
  36. XCTAssertEqual(request.request?.urlString ?? "", urlString, "request URL string should be equal")
  37. XCTAssertNil(request.response, "request response should be nil")
  38. }
  39. func testRequestClassMethodWithMethodAndURLAndParameters() {
  40. // Given
  41. let urlString = "https://httpbin.org/get"
  42. // When
  43. let request = Alamofire.request(urlString, withMethod: .get, parameters: ["foo": "bar"])
  44. // Then
  45. XCTAssertNotNil(request.request, "request URL request should not be nil")
  46. XCTAssertEqual(request.request?.httpMethod ?? "", "GET", "request HTTP method should match expected value")
  47. XCTAssertNotEqual(request.request?.urlString ?? "", urlString, "request URL string should be equal")
  48. XCTAssertEqual(request.request?.url?.query ?? "", "foo=bar", "query is incorrect")
  49. XCTAssertNil(request.response, "request response should be nil")
  50. }
  51. func testRequestClassMethodWithMethodURLParametersAndHeaders() {
  52. // Given
  53. let urlString = "https://httpbin.org/get"
  54. let headers = ["Authorization": "123456"]
  55. // When
  56. let request = Alamofire.request(urlString, withMethod: .get, parameters: ["foo": "bar"], headers: headers)
  57. // Then
  58. XCTAssertNotNil(request.request, "request should not be nil")
  59. XCTAssertEqual(request.request?.httpMethod ?? "", "GET", "request HTTP method should match expected value")
  60. XCTAssertNotEqual(request.request?.urlString ?? "", urlString, "request URL string should be equal")
  61. XCTAssertEqual(request.request?.url?.query ?? "", "foo=bar", "query is incorrect")
  62. let authorizationHeader = request.request?.value(forHTTPHeaderField: "Authorization") ?? ""
  63. XCTAssertEqual(authorizationHeader, "123456", "Authorization header is incorrect")
  64. XCTAssertNil(request.response, "response should be nil")
  65. }
  66. }
  67. // MARK: -
  68. class RequestResponseTestCase: BaseTestCase {
  69. func testRequestResponse() {
  70. // Given
  71. let urlString = "https://httpbin.org/get"
  72. let expectation = self.expectation(description: "GET request should succeed: \(urlString)")
  73. var request: URLRequest?
  74. var response: HTTPURLResponse?
  75. var data: Data?
  76. var error: Error?
  77. // When
  78. Alamofire.request(urlString, withMethod: .get, parameters: ["foo": "bar"])
  79. .response { responseRequest, responseResponse, responseData, responseError in
  80. request = responseRequest
  81. response = responseResponse
  82. data = responseData
  83. error = responseError
  84. expectation.fulfill()
  85. }
  86. waitForExpectations(timeout: timeout, handler: nil)
  87. // Then
  88. XCTAssertNotNil(request, "request should not be nil")
  89. XCTAssertNotNil(response, "response should not be nil")
  90. XCTAssertNotNil(data, "data should not be nil")
  91. XCTAssertNil(error, "error should be nil")
  92. }
  93. func testRequestResponseWithProgress() {
  94. // Given
  95. let randomBytes = 4 * 1024 * 1024
  96. let urlString = "https://httpbin.org/bytes/\(randomBytes)"
  97. let expectation = self.expectation(description: "Bytes download progress should be reported: \(urlString)")
  98. var byteValues: [(bytes: Int64, totalBytes: Int64, totalBytesExpected: Int64)] = []
  99. var progressValues: [(completedUnitCount: Int64, totalUnitCount: Int64)] = []
  100. var responseRequest: URLRequest?
  101. var responseResponse: HTTPURLResponse?
  102. var responseData: Data?
  103. var responseError: Error?
  104. // When
  105. Alamofire.request(urlString, withMethod: .get)
  106. .downloadProgress { progress in
  107. progressValues.append((progress.completedUnitCount, progress.totalUnitCount))
  108. }
  109. .downloadProgress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
  110. let bytes = (bytes: bytesRead, totalBytes: totalBytesRead, totalBytesExpected: totalBytesExpectedToRead)
  111. byteValues.append(bytes)
  112. }
  113. .response { request, response, data, error in
  114. responseRequest = request
  115. responseResponse = response
  116. responseData = data
  117. responseError = error
  118. expectation.fulfill()
  119. }
  120. waitForExpectations(timeout: timeout, handler: nil)
  121. // Then
  122. XCTAssertNotNil(responseRequest)
  123. XCTAssertNotNil(responseResponse)
  124. XCTAssertNotNil(responseData)
  125. XCTAssertNil(responseError)
  126. XCTAssertEqual(byteValues.count, progressValues.count)
  127. if byteValues.count == progressValues.count {
  128. for (byteValue, progressValue) in zip(byteValues, progressValues) {
  129. XCTAssertGreaterThan(byteValue.bytes, 0)
  130. XCTAssertEqual(byteValue.totalBytes, progressValue.completedUnitCount)
  131. XCTAssertEqual(byteValue.totalBytesExpected, progressValue.totalUnitCount)
  132. }
  133. }
  134. if let lastByteValue = byteValues.last, let lastProgressValue = progressValues.last {
  135. let byteValueFractionalCompletion = Double(lastByteValue.totalBytes) / Double(lastByteValue.totalBytesExpected)
  136. let progressValueFractionalCompletion = Double(lastProgressValue.0) / Double(lastProgressValue.1)
  137. XCTAssertEqual(byteValueFractionalCompletion, 1.0)
  138. XCTAssertEqual(progressValueFractionalCompletion, 1.0)
  139. } else {
  140. XCTFail("last item in bytesValues and progressValues should not be nil")
  141. }
  142. }
  143. func testRequestResponseWithStream() {
  144. // Given
  145. let randomBytes = 4 * 1024 * 1024
  146. let urlString = "https://httpbin.org/bytes/\(randomBytes)"
  147. let expectation = self.expectation(description: "Bytes download progress should be reported: \(urlString)")
  148. var byteValues: [(bytes: Int64, totalBytes: Int64, totalBytesExpected: Int64)] = []
  149. var progressValues: [(completedUnitCount: Int64, totalUnitCount: Int64)] = []
  150. var accumulatedData = [Data]()
  151. var responseRequest: URLRequest?
  152. var responseResponse: HTTPURLResponse?
  153. var responseData: Data?
  154. var responseError: Error?
  155. // When
  156. Alamofire.request(urlString, withMethod: .get)
  157. .downloadProgress { progress in
  158. progressValues.append((progress.completedUnitCount, progress.totalUnitCount))
  159. }
  160. .downloadProgress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
  161. let bytes = (bytes: bytesRead, totalBytes: totalBytesRead, totalBytesExpected: totalBytesExpectedToRead)
  162. byteValues.append(bytes)
  163. }
  164. .stream { data in
  165. accumulatedData.append(data)
  166. }
  167. .response { request, response, data, error in
  168. responseRequest = request
  169. responseResponse = response
  170. responseData = data
  171. responseError = error
  172. expectation.fulfill()
  173. }
  174. waitForExpectations(timeout: timeout, handler: nil)
  175. // Then
  176. XCTAssertNotNil(responseRequest)
  177. XCTAssertNotNil(responseResponse)
  178. XCTAssertNil(responseData)
  179. XCTAssertNil(responseError)
  180. XCTAssertGreaterThanOrEqual(accumulatedData.count, 1)
  181. XCTAssertEqual(byteValues.count, progressValues.count)
  182. if byteValues.count == progressValues.count {
  183. for (byteValue, progressValue) in zip(byteValues, progressValues) {
  184. XCTAssertGreaterThan(byteValue.bytes, 0)
  185. XCTAssertEqual(byteValue.totalBytes, progressValue.completedUnitCount)
  186. XCTAssertEqual(byteValue.totalBytesExpected, progressValue.totalUnitCount)
  187. }
  188. }
  189. if let lastByteValue = byteValues.last, let lastProgressValue = progressValues.last {
  190. let byteValueFractionalCompletion = Double(lastByteValue.totalBytes) / Double(lastByteValue.totalBytesExpected)
  191. let progressValueFractionalCompletion = Double(lastProgressValue.0) / Double(lastProgressValue.1)
  192. XCTAssertEqual(byteValueFractionalCompletion, 1.0)
  193. XCTAssertEqual(progressValueFractionalCompletion, 1.0)
  194. XCTAssertEqual(accumulatedData.reduce(Int64(0)) { $0 + $1.count }, lastByteValue.totalBytes)
  195. } else {
  196. XCTFail("last item in bytesValues and progressValues should not be nil")
  197. }
  198. }
  199. func testPOSTRequestWithUnicodeParameters() {
  200. // Given
  201. let urlString = "https://httpbin.org/post"
  202. let parameters = [
  203. "french": "français",
  204. "japanese": "日本語",
  205. "arabic": "العربية",
  206. "emoji": "😃"
  207. ]
  208. let expectation = self.expectation(description: "request should succeed")
  209. var response: Response<Any>?
  210. // When
  211. Alamofire.request(urlString, withMethod: .post, parameters: parameters)
  212. .responseJSON { closureResponse in
  213. response = closureResponse
  214. expectation.fulfill()
  215. }
  216. waitForExpectations(timeout: timeout, handler: nil)
  217. // Then
  218. if let response = response {
  219. XCTAssertNotNil(response.request, "request should not be nil")
  220. XCTAssertNotNil(response.response, "response should not be nil")
  221. XCTAssertNotNil(response.data, "data should not be nil")
  222. if let json = response.result.value as? [String: Any], let form = json["form"] as? [String: String] {
  223. XCTAssertEqual(form["french"], parameters["french"], "french parameter value should match form value")
  224. XCTAssertEqual(form["japanese"], parameters["japanese"], "japanese parameter value should match form value")
  225. XCTAssertEqual(form["arabic"], parameters["arabic"], "arabic parameter value should match form value")
  226. XCTAssertEqual(form["emoji"], parameters["emoji"], "emoji parameter value should match form value")
  227. } else {
  228. XCTFail("form parameter in JSON should not be nil")
  229. }
  230. } else {
  231. XCTFail("response should not be nil")
  232. }
  233. }
  234. func testPOSTRequestWithBase64EncodedImages() {
  235. // Given
  236. let urlString = "https://httpbin.org/post"
  237. let pngBase64EncodedString: String = {
  238. let URL = url(forResource: "unicorn", withExtension: "png")
  239. let data = try! Data(contentsOf: URL)
  240. return data.base64EncodedString(options: .lineLength64Characters)
  241. }()
  242. let jpegBase64EncodedString: String = {
  243. let URL = url(forResource: "rainbow", withExtension: "jpg")
  244. let data = try! Data(contentsOf: URL)
  245. return data.base64EncodedString(options: .lineLength64Characters)
  246. }()
  247. let parameters = [
  248. "email": "user@alamofire.org",
  249. "png_image": pngBase64EncodedString,
  250. "jpeg_image": jpegBase64EncodedString
  251. ]
  252. let expectation = self.expectation(description: "request should succeed")
  253. var response: Response<Any>?
  254. // When
  255. Alamofire.request(urlString, withMethod: .post, parameters: parameters)
  256. .responseJSON { closureResponse in
  257. response = closureResponse
  258. expectation.fulfill()
  259. }
  260. waitForExpectations(timeout: timeout, handler: nil)
  261. // Then
  262. if let response = response {
  263. XCTAssertNotNil(response.request, "request should not be nil")
  264. XCTAssertNotNil(response.response, "response should not be nil")
  265. XCTAssertNotNil(response.data, "data should not be nil")
  266. XCTAssertTrue(response.result.isSuccess, "result should be success")
  267. if let json = response.result.value as? [String: Any], let form = json["form"] as? [String: String] {
  268. XCTAssertEqual(form["email"], parameters["email"], "email parameter value should match form value")
  269. XCTAssertEqual(form["png_image"], parameters["png_image"], "png_image parameter value should match form value")
  270. XCTAssertEqual(form["jpeg_image"], parameters["jpeg_image"], "jpeg_image parameter value should match form value")
  271. } else {
  272. XCTFail("form parameter in JSON should not be nil")
  273. }
  274. } else {
  275. XCTFail("response should not be nil")
  276. }
  277. }
  278. }
  279. // MARK: -
  280. extension Request {
  281. fileprivate func preValidate(operation: @escaping (Void) -> Void) -> Self {
  282. delegate.queue.addOperation {
  283. operation()
  284. }
  285. return self
  286. }
  287. fileprivate func postValidate(operation: @escaping (Void) -> Void) -> Self {
  288. delegate.queue.addOperation {
  289. operation()
  290. }
  291. return self
  292. }
  293. }
  294. // MARK: -
  295. class RequestExtensionTestCase: BaseTestCase {
  296. func testThatRequestExtensionHasAccessToTaskDelegateQueue() {
  297. // Given
  298. let urlString = "https://httpbin.org/get"
  299. let expectation = self.expectation(description: "GET request should succeed: \(urlString)")
  300. var responses: [String] = []
  301. // When
  302. Alamofire.request(urlString, withMethod: .get)
  303. .preValidate {
  304. responses.append("preValidate")
  305. }
  306. .validate()
  307. .postValidate {
  308. responses.append("postValidate")
  309. }
  310. .response { _, _, _, _ in
  311. responses.append("response")
  312. expectation.fulfill()
  313. }
  314. waitForExpectations(timeout: timeout, handler: nil)
  315. // Then
  316. if responses.count == 3 {
  317. XCTAssertEqual(responses[0], "preValidate", "response at index 0 should be preValidate")
  318. XCTAssertEqual(responses[1], "postValidate", "response at index 1 should be postValidate")
  319. XCTAssertEqual(responses[2], "response", "response at index 2 should be response")
  320. } else {
  321. XCTFail("responses count should be equal to 3")
  322. }
  323. }
  324. }
  325. // MARK: -
  326. class RequestDescriptionTestCase: BaseTestCase {
  327. func testRequestDescription() {
  328. // Given
  329. let urlString = "https://httpbin.org/get"
  330. let request = Alamofire.request(urlString, withMethod: .get)
  331. let initialRequestDescription = request.description
  332. let expectation = self.expectation(description: "Request description should update: \(urlString)")
  333. var finalRequestDescription: String?
  334. var response: HTTPURLResponse?
  335. // When
  336. request.response { _, responseResponse, _, _ in
  337. finalRequestDescription = request.description
  338. response = responseResponse
  339. expectation.fulfill()
  340. }
  341. waitForExpectations(timeout: timeout, handler: nil)
  342. // Then
  343. XCTAssertEqual(initialRequestDescription, "GET https://httpbin.org/get", "incorrect request description")
  344. XCTAssertEqual(
  345. finalRequestDescription ?? "",
  346. "GET https://httpbin.org/get (\(response?.statusCode ?? -1))",
  347. "incorrect request description"
  348. )
  349. }
  350. }
  351. // MARK: -
  352. class RequestDebugDescriptionTestCase: BaseTestCase {
  353. // MARK: Properties
  354. let manager: SessionManager = {
  355. let manager = SessionManager(configuration: .default)
  356. manager.startRequestsImmediately = false
  357. return manager
  358. }()
  359. let managerWithAcceptLanguageHeader: SessionManager = {
  360. var headers = SessionManager.default.session.configuration.httpAdditionalHeaders ?? [:]
  361. headers["Accept-Language"] = "en-US"
  362. let configuration = URLSessionConfiguration.default
  363. configuration.httpAdditionalHeaders = headers
  364. let manager = SessionManager(configuration: configuration)
  365. manager.startRequestsImmediately = false
  366. return manager
  367. }()
  368. let managerWithContentTypeHeader: SessionManager = {
  369. var headers = SessionManager.default.session.configuration.httpAdditionalHeaders ?? [:]
  370. headers["Content-Type"] = "application/json"
  371. let configuration = URLSessionConfiguration.default
  372. configuration.httpAdditionalHeaders = headers
  373. let manager = SessionManager(configuration: configuration)
  374. manager.startRequestsImmediately = false
  375. return manager
  376. }()
  377. let managerDisallowingCookies: SessionManager = {
  378. let configuration = URLSessionConfiguration.default
  379. configuration.httpShouldSetCookies = false
  380. let manager = SessionManager(configuration: configuration)
  381. manager.startRequestsImmediately = false
  382. return manager
  383. }()
  384. // MARK: Tests
  385. func testGETRequestDebugDescription() {
  386. // Given
  387. let urlString = "https://httpbin.org/get"
  388. // When
  389. let request = manager.request(urlString, withMethod: .get)
  390. let components = cURLCommandComponents(for: request)
  391. // Then
  392. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
  393. XCTAssertFalse(components.contains("-X"), "command should not contain explicit -X flag")
  394. XCTAssertEqual(components.last ?? "", "\"\(urlString)\"", "URL component should be equal")
  395. }
  396. func testGETRequestWithDuplicateHeadersDebugDescription() {
  397. // Given
  398. let urlString = "https://httpbin.org/get"
  399. // When
  400. let headers = [ "Accept-Language": "en-GB" ]
  401. let request = managerWithAcceptLanguageHeader.request(urlString, withMethod: .get, headers: headers)
  402. let components = cURLCommandComponents(for: request)
  403. // Then
  404. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
  405. XCTAssertFalse(components.contains("-X"), "command should not contain explicit -X flag")
  406. XCTAssertEqual(components.last ?? "", "\"\(urlString)\"", "URL component should be equal")
  407. let tokens = request.debugDescription.components(separatedBy: "Accept-Language:")
  408. XCTAssertTrue(tokens.count == 2, "command should contain a single Accept-Language header")
  409. XCTAssertTrue(
  410. request.debugDescription.range(of: "-H \"Accept-Language: en-GB\"") != nil,
  411. "command should Accept-Language set to 'en-GB'"
  412. )
  413. }
  414. func testPOSTRequestDebugDescription() {
  415. // Given
  416. let urlString = "https://httpbin.org/post"
  417. // When
  418. let request = manager.request(urlString, withMethod: .post)
  419. let components = cURLCommandComponents(for: request)
  420. // Then
  421. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
  422. XCTAssertEqual(components[3..<5], ["-X", "POST"], "command should contain explicit -X flag")
  423. XCTAssertEqual(components.last ?? "", "\"\(urlString)\"", "URL component should be equal")
  424. }
  425. func testPOSTRequestWithJSONParametersDebugDescription() {
  426. // Given
  427. let urlString = "https://httpbin.org/post"
  428. let parameters = [
  429. "foo": "bar",
  430. "fo\"o": "b\"ar",
  431. "f'oo": "ba'r"
  432. ]
  433. // When
  434. let request = manager.request(urlString, withMethod: .post, parameters: parameters, encoding: .json)
  435. let components = cURLCommandComponents(for: request)
  436. // Then
  437. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
  438. XCTAssertEqual(components[3..<5], ["-X", "POST"], "command should contain explicit -X flag")
  439. XCTAssertNotNil(request.debugDescription.range(of: "-H \"Content-Type: application/json\""), "command should contain Content-Type header")
  440. XCTAssertNotNil(request.debugDescription.range(of: "-d \"{"), "command should contain body parameter")
  441. XCTAssertNotNil(request.debugDescription.range(of: "\\\"f'oo\\\":\\\"ba'r\\\""), "command should contain JSON parameters")
  442. XCTAssertNotNil(request.debugDescription.range(of: "\\\"fo\\\\\\\"o\\\":\\\"b\\\\\\\"ar\\\""), "command should contain JSON parameters")
  443. XCTAssertNotNil(request.debugDescription.range(of: "\\\"foo\\\":\\\"bar\\"), "command should contain JSON parameters")
  444. XCTAssertEqual(components.last ?? "", "\"\(urlString)\"", "URL component should be equal")
  445. }
  446. func testPOSTRequestWithCookieDebugDescription() {
  447. // Given
  448. let urlString = "https://httpbin.org/post"
  449. let properties = [
  450. HTTPCookiePropertyKey.domain: "httpbin.org",
  451. HTTPCookiePropertyKey.path: "/post",
  452. HTTPCookiePropertyKey.name: "foo",
  453. HTTPCookiePropertyKey.value: "bar",
  454. ]
  455. let cookie = HTTPCookie(properties: properties)!
  456. manager.session.configuration.httpCookieStorage?.setCookie(cookie)
  457. // When
  458. let request = manager.request(urlString, withMethod: .post)
  459. let components = cURLCommandComponents(for: request)
  460. // Then
  461. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
  462. XCTAssertEqual(components[3..<5], ["-X", "POST"], "command should contain explicit -X flag")
  463. XCTAssertEqual(components.last ?? "", "\"\(urlString)\"", "URL component should be equal")
  464. XCTAssertEqual(components[5..<6], ["-b"], "command should contain -b flag")
  465. }
  466. func testPOSTRequestWithCookiesDisabledDebugDescription() {
  467. // Given
  468. let urlString = "https://httpbin.org/post"
  469. let properties = [
  470. HTTPCookiePropertyKey.domain: "httpbin.org",
  471. HTTPCookiePropertyKey.path: "/post",
  472. HTTPCookiePropertyKey.name: "foo",
  473. HTTPCookiePropertyKey.value: "bar",
  474. ]
  475. let cookie = HTTPCookie(properties: properties)!
  476. managerDisallowingCookies.session.configuration.httpCookieStorage?.setCookie(cookie)
  477. // When
  478. let request = managerDisallowingCookies.request(urlString, withMethod: .post)
  479. let components = cURLCommandComponents(for: request)
  480. // Then
  481. let cookieComponents = components.filter { $0 == "-b" }
  482. XCTAssertTrue(cookieComponents.isEmpty, "command should not contain -b flag")
  483. }
  484. func testMultipartFormDataRequestWithDuplicateHeadersDebugDescription() {
  485. // Given
  486. let urlString = "https://httpbin.org/post"
  487. let japaneseData = "日本語".data(using: String.Encoding.utf8, allowLossyConversion: false)!
  488. let expectation = self.expectation(description: "multipart form data encoding should succeed")
  489. var request: Request?
  490. var components: [String] = []
  491. // When
  492. managerWithContentTypeHeader.upload(
  493. multipartFormData: { multipartFormData in
  494. multipartFormData.append(japaneseData, withName: "japanese")
  495. },
  496. to: urlString,
  497. withMethod: .post,
  498. encodingCompletion: { result in
  499. switch result {
  500. case .success(let upload, _, _):
  501. request = upload
  502. components = self.cURLCommandComponents(for: upload)
  503. expectation.fulfill()
  504. case .failure:
  505. expectation.fulfill()
  506. }
  507. }
  508. )
  509. waitForExpectations(timeout: timeout, handler: nil)
  510. debugPrint(request!)
  511. // Then
  512. XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
  513. XCTAssertTrue(components.contains("-X"), "command should contain explicit -X flag")
  514. XCTAssertEqual(components.last ?? "", "\"\(urlString)\"", "URL component should be equal")
  515. let tokens = request.debugDescription.components(separatedBy: "Content-Type:")
  516. XCTAssertTrue(tokens.count == 2, "command should contain a single Content-Type header")
  517. XCTAssertTrue(
  518. request.debugDescription.range(of: "-H \"Content-Type: multipart/form-data;") != nil,
  519. "command should contain Content-Type header starting with 'multipart/form-data;'"
  520. )
  521. }
  522. func testThatRequestWithInvalidURLDebugDescription() {
  523. // Given
  524. let urlString = "invalid_url"
  525. // When
  526. let request = manager.request(urlString, withMethod: .get)
  527. let debugDescription = request.debugDescription
  528. // Then
  529. XCTAssertNotNil(debugDescription, "debugDescription should not crash")
  530. }
  531. // MARK: Test Helper Methods
  532. private func cURLCommandComponents(for request: Request) -> [String] {
  533. let whitespaceCharacterSet = CharacterSet.whitespacesAndNewlines
  534. return request.debugDescription.components(separatedBy: whitespaceCharacterSet)
  535. .filter { $0 != "" && $0 != "\\" }
  536. }
  537. }