Skip to content

Commit

Permalink
Merge pull request #15 from billp/issue-14-Update_TestPinning_test_ca…
Browse files Browse the repository at this point in the history
…ses_to_use_the_new_response_callbacks

Update TestPinning test cases to use the new response callbacks (fixes #14)
  • Loading branch information
billp authored Jan 14, 2021
2 parents 9e49b22 + 25b512f commit 820579e
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 28 deletions.
4 changes: 4 additions & 0 deletions TermiNetwork.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
F3B0F35B250A3121009FF4D0 /* herokuapp.com.cer in Resources */ = {isa = PBXBuildFile; fileRef = F3B0F35A250A3121009FF4D0 /* herokuapp.com.cer */; };
F3D16EC82575299900F7B523 /* TermiNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F319682D243D01E1008725CE /* TermiNetwork.framework */; };
F3D16EC92575299900F7B523 /* TermiNetwork.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = F319682D243D01E1008725CE /* TermiNetwork.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
F3DF6C6025B0438400806623 /* TestPinningDeprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3DF6C5F25B0438400806623 /* TestPinningDeprecated.swift */; };
F3F255E9244C33B800517BF7 /* RouteConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3F255E8244C33B800517BF7 /* RouteConfiguration.swift */; };
F3F48288245EBF6D00656449 /* TestUploadOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3F48287245EBE8000656449 /* TestUploadOperations.swift */; };
F3F4828D245EE58C00656449 /* MultipartFormDataStream.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3F4828C245EE58C00656449 /* MultipartFormDataStream.swift */; };
Expand Down Expand Up @@ -284,6 +285,7 @@
F3AA5146256EF53000D64D31 /* TestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestHelpers.swift; sourceTree = "<group>"; };
F3AF556825987256000E01D8 /* DoNothingInterceptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DoNothingInterceptor.swift; sourceTree = "<group>"; };
F3B0F35A250A3121009FF4D0 /* herokuapp.com.cer */ = {isa = PBXFileReference; lastKnownFileType = file; path = herokuapp.com.cer; sourceTree = "<group>"; };
F3DF6C5F25B0438400806623 /* TestPinningDeprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestPinningDeprecated.swift; sourceTree = "<group>"; };
F3F255E8244C33B800517BF7 /* RouteConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouteConfiguration.swift; sourceTree = "<group>"; };
F3F48287245EBE8000656449 /* TestUploadOperations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUploadOperations.swift; sourceTree = "<group>"; };
F3F4828C245EE58C00656449 /* MultipartFormDataStream.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultipartFormDataStream.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -514,6 +516,7 @@
F3363E7325A639D700699E73 /* Deprecated */ = {
isa = PBXGroup;
children = (
F3DF6C5F25B0438400806623 /* TestPinningDeprecated.swift */,
F398575D25AC94C900DCCAAE /* TestTransformersDeprecated.swift */,
F35B533F24439FAC00C1853A /* TestMockRequestDeprecated.swift */,
F368C9B925A9BFEA0059FECB /* TestUploadOperationsDeprecated.swift */,
Expand Down Expand Up @@ -1040,6 +1043,7 @@
F32BB57B25A8ADCE00E6E096 /* StatusCodeTransformer.swift in Sources */,
F32C3D13243E499E004497BC /* TestPinning.swift in Sources */,
F3AA5147256EF53000D64D31 /* TestHelpers.swift in Sources */,
F3DF6C6025B0438400806623 /* TestPinningDeprecated.swift in Sources */,
F3850C8D243D103700F7CC13 /* TestJSONParams.swift in Sources */,
F3850C91243D103700F7CC13 /* TestRequestDeprecated.swift in Sources */,
F387B0F7257958EB00A8C0C7 /* TestTransformers.swift in Sources */,
Expand Down
58 changes: 30 additions & 28 deletions Tests/TestPinning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,44 @@ class TestPinning: XCTestCase {
}

func testValidCertificate() {
let expectation = XCTestExpectation(description: "testValidCertificate")
var failed = true
let expectation = XCTestExpectation(description: "testValidCertificate")
var failed = true

let request = Request(route: APIRoute.testPinning(certPath: validCertPath))
request.start(responseType: String.self, onSuccess: { _ in
failed = false
expectation.fulfill()
}, onFailure: { _, _ in
failed = true
expectation.fulfill()
})
Request(route: APIRoute.testPinning(certPath: validCertPath))
.success(responseType: String.self) { _ in
failed = false
expectation.fulfill()
}
.failure { _ in
failed = true
expectation.fulfill()
}

wait(for: [expectation], timeout: 100)
wait(for: [expectation], timeout: 100)

XCTAssert(!failed)
}
XCTAssert(!failed)
}

func testInvalidCertificate() {
let expectation = XCTestExpectation(description: "testInvalidCertificate")
var failed = true
func testInvalidCertificate() {
let expectation = XCTestExpectation(description: "testInvalidCertificate")
var failed = true

let request = Request(route: APIRoute.testPinning(certPath: invalidCertPath))
request.start(responseType: String.self, onSuccess: { _ in
Request(route: APIRoute.testPinning(certPath: invalidCertPath))
.success(responseType: String.self) { _ in
failed = true
expectation.fulfill()
}, onFailure: { error, _ in
if case .pinningError = error {
failed = false
} else {
failed = true
}
expectation.fulfill()
})
.failure { error in
if case .pinningError = error {
failed = false
} else {
failed = true
}
expectation.fulfill()
}

wait(for: [expectation], timeout: 100)
wait(for: [expectation], timeout: 100)

XCTAssert(!failed)
}
XCTAssert(!failed)
}
}
93 changes: 93 additions & 0 deletions Tests/TestPinningDeprecated.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// TestPinningDeprecated.swift
//
// Copyright © 2018-2021 Vasilis Panagiotopoulos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
// Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import XCTest
import TermiNetwork

class TestPinningDeprecated: XCTestCase {

var bundle: Bundle = {
return Bundle(for: TestPinning.self)
}()

var invalidCertPath: String {
return bundle.path(forResource: "forums.swift.org",
ofType: "cer",
inDirectory: nil,
forLocalization: nil) ?? ""
}

var validCertPath: String {
return bundle.path(forResource: "herokuapp.com",
ofType: "cer",
inDirectory: nil,
forLocalization: nil) ?? ""
}

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
Environment.set(Env.termiNetworkRemote)
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testValidCertificate() {
let expectation = XCTestExpectation(description: "testValidCertificate")
var failed = true

let request = Request(route: APIRoute.testPinning(certPath: validCertPath))
request.start(responseType: String.self, onSuccess: { _ in
failed = false
expectation.fulfill()
}, onFailure: { _, _ in
failed = true
expectation.fulfill()
})

wait(for: [expectation], timeout: 100)

XCTAssert(!failed)
}

func testInvalidCertificate() {
let expectation = XCTestExpectation(description: "testInvalidCertificate")
var failed = true

let request = Request(route: APIRoute.testPinning(certPath: invalidCertPath))
request.start(responseType: String.self, onSuccess: { _ in
failed = true
expectation.fulfill()
}, onFailure: { error, _ in
if case .pinningError = error {
failed = false
} else {
failed = true
}
expectation.fulfill()
})

wait(for: [expectation], timeout: 100)

XCTAssert(!failed)
}
}

0 comments on commit 820579e

Please sign in to comment.