Skip to content

Commit

Permalink
Merge pull request #204 from Subito-it/hotfix/large_body
Browse files Browse the repository at this point in the history
Replace URLProtocol property setting
  • Loading branch information
tcamin authored Mar 26, 2024
2 parents c687994 + 7db92b2 commit 65d1000
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 62 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())
}
}
38 changes: 34 additions & 4 deletions Example/SBTUITestTunnel_Tests/MonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,41 @@ class MonitorTests: XCTestCase {
XCTAssert(app.stubRequestsRemoveAll())
XCTAssert(app.monitorRequestRemoveAll())
}

func testMonitorPostRequestWithHTTPBody() {
app.monitorRequests(matching: SBTRequestMatch(url: "httpbin.org", method: "POST"))

let smallBody = String(repeating: "a", count: 100)

_ = request.dataTaskNetwork(urlString: "https://httpbin.org/post", httpMethod: "POST", httpBody: smallBody)
let requests = app.monitoredRequestsFlushAll()
XCTAssertEqual(requests.count, 1)
print(requests.map(\.debugDescription))

for request in requests {
guard let httpBody = request.request?.httpBody else {
XCTFail("Missing http body")
continue
}

XCTAssertEqual(String(data: httpBody, encoding: .utf8), smallBody)

XCTAssert((request.responseString()!).contains("httpbin.org"))
XCTAssert(request.timestamp > 0.0)
XCTAssert(request.requestTime > 0.0)
}

XCTAssert(app.stubRequestsRemoveAll())
XCTAssert(app.monitorRequestRemoveAll())
}


func testMonitorPostRequestWithHTTPLargeBody() {
app.monitorRequests(matching: SBTRequestMatch(url: "httpbin.org", method: "POST"))

let largeBody = String(repeating: "a", count: 20000)

_ = request.dataTaskNetwork(urlString: "https://httpbin.org/post", httpMethod: "POST", httpBody: "&param5=val5&param6=val6")
_ = request.dataTaskNetwork(urlString: "https://httpbin.org/post", httpMethod: "POST", httpBody: largeBody)
let requests = app.monitoredRequestsFlushAll()
XCTAssertEqual(requests.count, 1)
print(requests.map(\.debugDescription))
Expand All @@ -220,7 +250,7 @@ class MonitorTests: XCTestCase {
continue
}

XCTAssertEqual(String(data: httpBody, encoding: .utf8), "&param5=val5&param6=val6")
XCTAssertEqual(String(data: httpBody, encoding: .utf8), largeBody)

XCTAssert((request.responseString()!).contains("httpbin.org"))
XCTAssert(request.timestamp > 0.0)
Expand All @@ -235,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
6 changes: 4 additions & 2 deletions Example/SPM/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ targets:
settings:
base:
INFOPLIST_FILE: "../SBTUITestTunnel/SBTUITestTunnel-Info.plist"
CODE_SIGN_ENTITLEMENTS: "../SBTUITestTunnel_Example.entitlements"
CODE_SIGN_ENTITLEMENTS: "../SBTUITestTunnel_Example.entitlements"
UITests:
type: bundle.ui-testing
platform: iOS
Expand All @@ -33,7 +33,7 @@ targets:
INFOPLIST_FILE: "../SBTUITestTunnel_Tests/Info.plist"

schemes:
SBTUITestTunnel_Example:
SBTUITestTunnel:
build:
targets:
App: [all]
Expand All @@ -51,7 +51,9 @@ schemes:
UITests: [all]
run:
executable: App
debugEnabled: false
buildConfiguration: Debug
test:
targets:
- UITests
debugEnabled: false
75 changes: 61 additions & 14 deletions Example/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ fileGroups:
- SBTUITestTunnel_Example.entitlements

targets:

SBTUITestTunnel_Example:
type: application
platform: iOS
Expand All @@ -25,12 +24,6 @@ targets:
- sdk: UIKit.framework
- sdk: Foundation.framework
- sdk: CoreGraphics.framework
scheme:
gatherCoverageData: true
testTargets:
- SBTUITestTunnel_Tests
coverageTargets:
- SBTUITestTunnel_Example

SBTUITestTunnel_Tests:
type: bundle.ui-testing
Expand All @@ -43,22 +36,76 @@ targets:
- SBTUITestTunnel_Tests
dependencies:
- target: SBTUITestTunnel_Example
scheme:
testTargets:
- SBTUITestTunnel_Tests

SBTUITestTunnel_TestsNoSwizzling:
type: bundle.ui-testing
platform: iOS
deploymentTarget: "12.2"
settings:
base:
GCC_PREPROCESSOR_DEFINITIONS: ["DISABLE_UITUNNEL_SWIZZLING=1", "$(inherited)"]
GCC_PREPROCESSOR_DEFINITIONS:
["DISABLE_UITUNNEL_SWIZZLING=1", "$(inherited)"]
INFOPLIST_FILE: "SBTUITestTunnel_TestsNoSwizzling/Info.plist"
sources:
- SBTUITestTunnel_TestsNoSwizzling
dependencies:
- target: SBTUITestTunnel_Example
scheme:
testTargets:
- SBTUITestTunnel_TestsNoSwizzling

schemes:
SBTUITestTunnel:
build:
targets:
SBTUITestTunnel_Example: [run, test, profile, analyze, archive]
config: Debug
profile:
config: Debug
test:
targets:
- SBTUITestTunnel_Tests
config: Debug
gatherCoverageData: true
disableMainThreadChecker: true
language: en
region: EN

SBTUITestTunnel_Tests:
run:
debugEnabled: false
build:
targets:
SBTUITestTunnel_Example: [run, test, profile, analyze, archive]
config: Debug
profile:
config: Debug
test:
targets:
- SBTUITestTunnel_Tests
config: Debug
gatherCoverageData: true
disableMainThreadChecker: true
debugEnabled: false
language: en
region: EN
management:
isShown: false

SBTUITestTunnel_NoSwizzlingTests:
run:
debugEnabled: false
build:
targets:
SBTUITestTunnel_Example: [run, test, profile, analyze, archive]
config: Debug
profile:
config: Debug
test:
targets:
- SBTUITestTunnel_TestsNoSwizzling
config: Debug
gatherCoverageData: true
disableMainThreadChecker: true
debugEnabled: false
language: en
region: EN
management:
isShown: false
4 changes: 2 additions & 2 deletions Scripts/build_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
require "fileutils"

module Build
EXAMPLE_APP_SCHEME = "SBTUITestTunnel_Example"
EXAMPLE_APP_SCHEME = "SBTUITestTunnel"
UITESTS_SCHEME = "SBTUITestTunnel_Tests"
UITESTS_NOSWIZZ_SCHEME = "SBTUITestTunnel_TestsNoSwizzling"
UITESTS_NOSWIZZ_SCHEME = "SBTUITestTunnel_NoSwizzlingTests"

def self.run_build(project_path)
puts "⏳ Building app..."
Expand Down
Loading

0 comments on commit 65d1000

Please sign in to comment.