Skip to content

Commit

Permalink
Merge pull request #7 from MrMUsmanAli/feature/added_tests
Browse files Browse the repository at this point in the history
added some unit tests
  • Loading branch information
MrMUsmanAli authored Dec 12, 2021
2 parents 215224f + c6d32d1 commit e3ffe56
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Sources/wiretapp/WiretappMockURLPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ internal class WiretappMockURLPlugin: URLProtocol {
}

override class func canInit(with request: URLRequest) -> Bool {
ProcessInfo.processInfo.environment[Wiretapp.testCaseName] != nil
guard let testCaseName = ProcessInfo.processInfo.environment[Wiretapp.testCaseName] else {
return false
}
return !testCaseName.isEmpty
}

override func startLoading() {
Expand Down
118 changes: 116 additions & 2 deletions Tests/wiretappTests/wiretappTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,121 @@
import XCTest
import Wiretapp

final class wiretappTests: XCTestCase {
func testExample() {
XCTAssertTrue(1 == 1)
struct User: Decodable {
let id: Int
let name: String
let username: String
let email: String
let phone: String
let address: Address

struct Address: Decodable {
let street: String
let suite: String
let city: String
let zipcode: String
}
}
struct Post: Decodable {
let userId: Int
let id: Int
let title: String
let body: String
}

func test_noEnvironmentVarsLoadsNormally() {
Wiretapp.registerSharedSession()
setenv(Wiretapp.recordEnabled, "false", 1)
setenv(Wiretapp.testCaseName, "", 1)
let expectation = XCTestExpectation(description: "outbound request made to web")

URLSession.shared.dataTask(
with: URL(string: "https://jsonplaceholder.typicode.com/users")!
) { data, response, error in
if
let data = data,
let decoded = try? JSONDecoder().decode([User].self, from: data),
decoded.count > 1
{
expectation.fulfill()
}
}
.resume()
wait(for: [expectation], timeout: 10.0)
}

func test_usingTestCaseNameInvokesMockResponses() {
Wiretapp.registerSharedSession()
setenv(Wiretapp.recordEnabled, "false", 1)
let packageRootPath = URL(fileURLWithPath: #file)
.pathComponents
.prefix(while: { $0 != "Tests" })
.joined(separator: "/")
.dropFirst()
setenv(Wiretapp.testCaseName,
packageRootPath + "/Example/demo.wiretapp/MockResponses/WireTappUITests/ExampleTestDynamicFolder",
1
)
let expectation = XCTestExpectation(description: "data will load from our test folder instead of web")

URLSession.shared.dataTask(
with: URL(string: "https://jsonplaceholder.typicode.com/users")!
) { data, response, error in
if
let data = data,
let decoded = try? JSONDecoder().decode([User].self, from: data),
decoded.first?.name == "FirstName LastName"
{
expectation.fulfill()
}
}
.resume()
wait(for: [expectation], timeout: 10.0)
}

func test_loadingMultipleMockResponsesForSameURI() {
Wiretapp.registerSharedSession()
setenv(Wiretapp.recordEnabled, "false", 1)
let packageRootPath = URL(fileURLWithPath: #file)
.pathComponents
.prefix(while: { $0 != "Tests" })
.joined(separator: "/")
.dropFirst()
setenv(Wiretapp.testCaseName,
packageRootPath + "/Example/demo.wiretapp/MockResponses/WireTappUITests/ExampleTestMultipleResponses",
1
)
let expectation = XCTestExpectation(description: "responses from same uri have loaded in sequence")

func loadPosts(completion: @escaping ([Post]?) -> ()) {
URLSession.shared.dataTask(
with: URL(string: "https://jsonplaceholder.typicode.com/posts")!
) { data, response, error in
if
let data = data,
let decoded = try? JSONDecoder().decode([Post].self, from: data)
{
completion(decoded)
}
completion(nil)
}
.resume()
}

loadPosts { posts in
if posts?.count == 1 {
loadPosts { posts in
if posts?.count == 2 {
loadPosts { posts in
if posts?.count == 3 {
expectation.fulfill()
}
}
}
}
}
}
wait(for: [expectation], timeout: 10.0)
}
}

0 comments on commit e3ffe56

Please sign in to comment.