Skip to content

Commit

Permalink
Merge pull request #63 from marcelklinek/add-delegate
Browse files Browse the repository at this point in the history
Pass a delegate that reacts to a response
  • Loading branch information
JanGorman authored Jul 5, 2021
2 parents e742eab + aaa77cd commit 0ca3959
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Hippolyte.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
s.author = { "Jan Gorman" => "[email protected]" }
s.social_media_url = "http://twitter.com/JanGorman"

s.ios.deployment_target = '12.0'
s.ios.deployment_target = '10.0'
s.osx.deployment_target = '10.13'

s.source = { :git => "https://github.com/JanGorman/Hippolyte.git", :tag => s.version}
Expand Down
8 changes: 8 additions & 0 deletions Hippolyte.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
77A2EA9F1F6561A30051E45A /* HippolyteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A2EA9E1F6561A30051E45A /* HippolyteTests.swift */; };
77B806641F63E33A0077A365 /* HTTPStubURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B806631F63E33A0077A365 /* HTTPStubURLProtocol.swift */; };
77B806661F63E5670077A365 /* HTTPRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B806651F63E5670077A365 /* HTTPRequest.swift */; };
79FBA093268F012C00A2519B /* ResponseDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79FBA092268F012C00A2519B /* ResponseDelegate.swift */; };
79FBA094268F012C00A2519B /* ResponseDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79FBA092268F012C00A2519B /* ResponseDelegate.swift */; };
ABD79AD622CD5F24003C9D8B /* JSONMatcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABD79AD522CD5F24003C9D8B /* JSONMatcherTests.swift */; };
E64B0ED3223ACCF900FB35E4 /* Hippolyte.h in Headers */ = {isa = PBXBuildFile; fileRef = 77A290D11F62EADF001E70FA /* Hippolyte.h */; settings = {ATTRIBUTES = (Public, ); }; };
E64B0ED4223ACD0D00FB35E4 /* Hippolyte.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A290E81F62EDF5001E70FA /* Hippolyte.swift */; };
Expand Down Expand Up @@ -96,6 +98,7 @@
77A2EA9E1F6561A30051E45A /* HippolyteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HippolyteTests.swift; sourceTree = "<group>"; };
77B806631F63E33A0077A365 /* HTTPStubURLProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPStubURLProtocol.swift; sourceTree = "<group>"; };
77B806651F63E5670077A365 /* HTTPRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPRequest.swift; sourceTree = "<group>"; };
79FBA092268F012C00A2519B /* ResponseDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseDelegate.swift; sourceTree = "<group>"; };
ABD79AD522CD5F24003C9D8B /* JSONMatcherTests.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = JSONMatcherTests.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
E64B0ECB223ACB6300FB35E4 /* Hippolyte.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Hippolyte.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E64B0EE3223ACE8400FB35E4 /* HippolyteTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HippolyteTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -174,6 +177,7 @@
7766CF9D1F652D8F00B717B2 /* URLHook.swift */,
F2F8EA8B2312DE5B00D1FBC4 /* Matchers */,
7766CFA11F65311200B717B2 /* URL+HippolyteAdditions.swift */,
79FBA092268F012C00A2519B /* ResponseDelegate.swift */,
);
path = Hippolyte;
sourceTree = "<group>";
Expand Down Expand Up @@ -430,6 +434,7 @@
F2F8EA8D2312DE7400D1FBC4 /* StringMatcher.swift in Sources */,
77A290F11F62F241001E70FA /* URLSessionHook.swift in Sources */,
77A290ED1F62EEC2001E70FA /* StubResponse.swift in Sources */,
79FBA093268F012C00A2519B /* ResponseDelegate.swift in Sources */,
77A290E91F62EDF5001E70FA /* Hippolyte.swift in Sources */,
77A290EB1F62EE40001E70FA /* StubRequest.swift in Sources */,
7766CF9E1F652D8F00B717B2 /* URLHook.swift in Sources */,
Expand Down Expand Up @@ -466,6 +471,7 @@
F2F8EA942312E84400D1FBC4 /* StringMatcher.swift in Sources */,
E64B0ED8223ACD0D00FB35E4 /* URLSessionHook.swift in Sources */,
E64B0ED6223ACD0D00FB35E4 /* StubResponse.swift in Sources */,
79FBA094268F012C00A2519B /* ResponseDelegate.swift in Sources */,
E64B0ED4223ACD0D00FB35E4 /* Hippolyte.swift in Sources */,
E64B0ED5223ACD0D00FB35E4 /* StubRequest.swift in Sources */,
E64B0EDB223ACD0D00FB35E4 /* URLHook.swift in Sources */,
Expand Down Expand Up @@ -644,6 +650,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = Hippolyte/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.schnaub.Hippolyte;
PRODUCT_NAME = Hippolyte;
Expand All @@ -666,6 +673,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = Hippolyte/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.schnaub.Hippolyte;
PRODUCT_NAME = Hippolyte;
Expand Down
1 change: 1 addition & 0 deletions Hippolyte/Hippolyte.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ open class Hippolyte {
guard let response = stubbedRequests.first(where: { $0.matchesRequest(request) })?.response else {
throw HippolyteError.unmatchedRequest
}
response.delegate?.onResponse?()
return response
}

Expand Down
5 changes: 5 additions & 0 deletions Hippolyte/ResponseDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

@objc public protocol ResponseDelegate {
@objc optional func onResponse()
}
12 changes: 12 additions & 0 deletions Hippolyte/StubResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public struct StubResponse: HTTPStubResponse, Equatable {
response.headers[key] = value
return self
}

@discardableResult
public func addDelegate(_ delegate: ResponseDelegate) -> Builder {
assert(response != nil)
response.delegate = delegate
return self
}

public func build() -> StubResponse {
response
Expand All @@ -62,6 +69,7 @@ public struct StubResponse: HTTPStubResponse, Equatable {
public var body: Data?
public let shouldFail: Bool
public let error: NSError?
public var delegate: ResponseDelegate?

/// Initialize a default response with statusCode 200 and empty body
public init() {
Expand Down Expand Up @@ -89,5 +97,9 @@ public struct StubResponse: HTTPStubResponse, Equatable {
shouldFail = false
error = nil
}

public static func == (lhs: StubResponse, rhs: StubResponse) -> Bool {
lhs.statusCode == rhs.statusCode && lhs.headers == rhs.headers && lhs.body == rhs.body && lhs.shouldFail == rhs.shouldFail
}

}
36 changes: 36 additions & 0 deletions HippolyteTests/HippolyteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ final class HippolyteTests: XCTestCase {
}.resume()
wait(for: [thirdExpectation], timeout: 1)
}

func testDelegateCalledWhenThereIsResponse() {
let url = URL(string: "http://www.apple.com")!
var stub = StubRequest(method: .GET, url: url)
let expectation = self.expectation(description: "Notifies delegate")

let response = StubResponse.Builder()
.stubResponse(withStatusCode: 200)
.addDelegate(TestResponseDelegate {
expectation.fulfill()
})
.build()
stub.response = response

Hippolyte.shared.add(stubbedRequest: stub)

let request = TestRequest(method: .GET, url: url)
_ = try? Hippolyte.shared.response(for: request)

wait(for: [expectation], timeout: 1)
Hippolyte.shared.clearStubs()
}

}

final class BlockRedirectDelegate: NSObject, URLSessionDelegate, URLSessionTaskDelegate {
Expand All @@ -193,3 +216,16 @@ final class BlockRedirectDelegate: NSObject, URLSessionDelegate, URLSessionTaskD
}

}

final class TestResponseDelegate: ResponseDelegate {

private let assertFunction: () -> Void

init(_ assertFunction: @escaping () -> Void) {
self.assertFunction = assertFunction
}

func onResponse() {
assertFunction()
}
}

0 comments on commit 0ca3959

Please sign in to comment.