Skip to content

Commit

Permalink
Add debug information to requests when running UITests
Browse files Browse the repository at this point in the history
  • Loading branch information
buggmagnet committed Oct 14, 2024
1 parent 12ccbe3 commit 5c81d53
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class PartnerAPIClient {
description: "Completion handler for the request is invoked"
)

printRequestInformation(method: method, request: request, url: url, jsonObject: jsonObject)
var requestError: Error?

let task = URLSession.shared.dataTask(with: request) { data, response, error in
requestError = error

Expand All @@ -97,17 +97,15 @@ class PartnerAPIClient {
if 200 ... 204 ~= response.statusCode {
print("Request successful")
do {
if data.isEmpty {
// Not all requests return JSON data
jsonResponse = [:]
} else {
if data.isEmpty == false {
jsonResponse = try JSONSerialization.jsonObject(with: data) as? [String: Any] ?? [:]
}
} catch {
XCTFail("Failed to deserialize JSON response")
}
} else {
XCTFail("Request failed with status code \(response.statusCode)")
let errorMessage = (try? XCTUnwrap(String(data: data, encoding: .utf8))) ?? "failed to parse error"
XCTFail("Request failed with error message: \(errorMessage) status code \(response.statusCode)")

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (CustomListsTests)

testAddSingleLocationToCustomList, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (CustomListsTests)

testCreateCustomListPersistAfterAppRestarts, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (CustomListsTests)

testDeleteCustomList, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (CustomListsTests)

testEditCustomListLocations, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (ConnectivityTests)

testAPIReachableWhenBlocked, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (ConnectivityTests)

testAppStillFunctioningWhenAPIDown, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (AccountTests)

testDeleteAccount, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (AccountTests)

testLogin, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (AccountTests)

testLoginToAccountWithTooManyDevices, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (AccountTests)

testLogOut, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403

Check failure on line 108 in ios/MullvadVPNUITests/Networking/PartnerAPIClient.swift

View workflow job for this annotation

GitHub Actions / reuse-e2e-workflow / Run tests (AccountTests)

testTimeLeft, failed - Request failed with error message: {"code":"PERMISSION_DENIED","error":"Invalid API credentials"} status code 403
}

completionHandlerInvokedExpectation.fulfill()
Expand All @@ -120,4 +118,21 @@ class PartnerAPIClient {

return jsonResponse
}

private func printRequestInformation(method: String, request: URLRequest, url: URL, jsonObject: [String: Any]?) {
/// Under no circumstances should the `accessToken` or the account number ever be printed
if #available(iOS 16, *) {
let headers = request.allHTTPHeaderFields?.filter { element in
element.key != "Authorization"
} ?? [:]

let debugMessage = """
Making a \(method) request to \(url) with the following body \(jsonObject ?? [:])
and headers \(headers)
"""
.replacingOccurrences(of: accessToken, with: "[REDACTED]")
.replacing(/[0-9]{16}/, with: "[REDACTED]")
print(debugMessage)
}
}
}

0 comments on commit 5c81d53

Please sign in to comment.