From c4430f05caa5b71c60ff052455af8b35806452dc Mon Sep 17 00:00:00 2001 From: Tomas Camin Date: Tue, 26 Mar 2024 14:19:02 +0100 Subject: [PATCH] Add test --- .../SBTTableViewController.swift | 6 ++ .../DownloadUploadTests.swift | 67 +++++++++++++------ 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Example/SBTUITestTunnel/SBTTableViewController.swift b/Example/SBTUITestTunnel/SBTTableViewController.swift index ad73a671..9ceee7d1 100644 --- a/Example/SBTUITestTunnel/SBTTableViewController.swift +++ b/Example/SBTUITestTunnel/SBTTableViewController.swift @@ -46,6 +46,7 @@ class SBTTableViewController: UITableViewController { private let testList: [BaseTest] = [NetworkTest(testSelector: #selector(executeDataTaskRequest)), NetworkTest(testSelector: #selector(executeDataTaskRequest2)), NetworkTest(testSelector: #selector(executeDataTaskRequest3)), + NetworkTest(testSelector: #selector(executePostDataTaskRequestWithLargeHTTPBody)), NetworkTest(testSelector: #selector(executeUploadDataTaskRequest)), NetworkTest(testSelector: #selector(executeUploadDataTaskRequest2)), NetworkTest(testSelector: #selector(executeBackgroundUploadDataTaskRequest)), @@ -348,6 +349,11 @@ extension SBTTableViewController { @objc func executeDataTaskRequest3() { dataTaskNetwork(urlString: "https://httpbin.org/get?param1=val1¶m2=val2", httpMethod: "GET", httpBody: nil, delay: 0.0, shouldPushResult: false) } + + @objc func executePostDataTaskRequestWithLargeHTTPBody() { + let largeBody = String(repeating: "a", count: 20000) + dataTaskNetwork(urlString: "https://httpbin.org/post", httpMethod: "POST", httpBody: largeBody) + } @objc func executeUploadDataTaskRequest() { let data = "This is a test".data(using: .utf8) diff --git a/Example/SBTUITestTunnel_Tests/DownloadUploadTests.swift b/Example/SBTUITestTunnel_Tests/DownloadUploadTests.swift index af428839..50c9c2bd 100644 --- a/Example/SBTUITestTunnel_Tests/DownloadUploadTests.swift +++ b/Example/SBTUITestTunnel_Tests/DownloadUploadTests.swift @@ -21,62 +21,91 @@ import XCTest class DownloadUploadTests: XCTestCase { override func setUp() { super.setUp() - + app.launchTunnel(withOptions: [SBTUITunneledApplicationLaunchOptionResetFilesystem]) - + expectation(for: NSPredicate(format: "count > 0"), evaluatedWith: app.tables) waitForExpectations(timeout: 15.0, handler: nil) - + Thread.sleep(forTimeInterval: 1.0) } - + func testSingleDownload() { let randomString = ProcessInfo.processInfo.globallyUniqueString - + let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let testFilePath = paths.first!.appending("/test_file_a.txt") - + if FileManager.default.fileExists(atPath: testFilePath) { try! FileManager.default.removeItem(atPath: testFilePath) } - + try! (randomString.data(using: .utf8))?.write(to: URL(fileURLWithPath: testFilePath)) - + app.uploadItem(atPath: testFilePath, toPath: "test_file_b.txt", relativeTo: .documentDirectory) - + let uploadData = app.downloadItems(fromPath: "test_file_b.txt", relativeTo: .documentDirectory)?.first! - + let uploadedString = String(data: uploadData!, encoding: .utf8) - + XCTAssertTrue(randomString == uploadedString) } - + func testMultipleDownload() { let randomString = ProcessInfo.processInfo.globallyUniqueString - + let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let testFilePath = paths.first!.appending("/test_file_a.txt") - + if FileManager.default.fileExists(atPath: testFilePath) { try! FileManager.default.removeItem(atPath: testFilePath) } - + try! (randomString.data(using: .utf8))?.write(to: URL(fileURLWithPath: testFilePath)) - + app.uploadItem(atPath: testFilePath, toPath: "test_file_1.txt", relativeTo: .documentDirectory) app.uploadItem(atPath: testFilePath, toPath: "test_file_2.txt", relativeTo: .documentDirectory) app.uploadItem(atPath: testFilePath, toPath: "test_file_3.txt", relativeTo: .documentDirectory) - + if let uploadDatas = app.downloadItems(fromPath: "test_file_*.txt", relativeTo: .documentDirectory) { XCTAssertEqual(uploadDatas.count, 3) - + for uploadData in uploadDatas { let uploadedString = String(data: uploadData, encoding: .utf8) - + XCTAssertTrue(randomString == uploadedString) } } else { XCTFail("No upload data received") } } + + func testMonitorPostRequestWithHTTPLargeBodyInAppProcess() { + let largeBody = String(repeating: "a", count: 20000) + let matchingRequest = SBTRequestMatch(url: "httpbin.org", method: "POST") + app.monitorRequests(matching: matchingRequest) + + XCTAssertTrue(app.tables.firstMatch.staticTexts["executePostDataTaskRequestWithLargeHTTPBody"].waitForExistence(timeout: 5)) + app.tables.firstMatch.staticTexts["executePostDataTaskRequestWithLargeHTTPBody"].tap() + + XCTAssertTrue(app.waitForMonitoredRequests(matching: matchingRequest, timeout: 10)) + let requests = app.monitoredRequestsFlushAll() + XCTAssertEqual(requests.count, 1) + + for request in requests { + guard let httpBody = request.request?.httpBody else { + XCTFail("Missing http body") + continue + } + + XCTAssertEqual(String(data: httpBody, encoding: .utf8), largeBody) + + XCTAssert((request.responseString()!).contains("httpbin.org")) + XCTAssert(request.timestamp > 0.0) + XCTAssert(request.requestTime > 0.0) + } + + XCTAssert(app.stubRequestsRemoveAll()) + XCTAssert(app.monitorRequestRemoveAll()) + } }