RequestTests.swift 28 KB

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