Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tcamin committed Mar 26, 2024
1 parent 7db02d7 commit 7db92b2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
6 changes: 6 additions & 0 deletions Example/SBTUITestTunnel/SBTTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -348,6 +349,11 @@ extension SBTTableViewController {
@objc func executeDataTaskRequest3() {
dataTaskNetwork(urlString: "https://httpbin.org/get?param1=val1&param2=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)
Expand Down
67 changes: 48 additions & 19 deletions Example/SBTUITestTunnel_Tests/DownloadUploadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
2 changes: 1 addition & 1 deletion Example/SBTUITestTunnel_Tests/MonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class MonitorTests: XCTestCase {
app.monitorRequests(matching: SBTRequestMatch(url: "httpbin.org"))

let start = Date()
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now() + 2.5) { [weak self] in
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now() + 1.0) { [weak self] in
_ = self?.request.dataTaskNetwork(urlString: "https://httpbin.org/get?param1=val1&param2=val2", httpMethod: "GET", httpBody: nil, delay: 0.0)
}

Expand Down

0 comments on commit 7db92b2

Please sign in to comment.