RequestTests.swift 28 KB

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