-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resource-specific properties set by resource service subclass #67
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Pod::Spec.new do |spec| | ||
spec.name = 'TABResourceLoader' | ||
spec.homepage = 'https://github.com/theappbusiness/TABResourceLoader' | ||
spec.version = '7.2.0' | ||
spec.version = '8.0.0' | ||
spec.license = { :type => 'MIT' } | ||
spec.authors = { 'Luciano Marisi' => '[email protected]' } | ||
spec.summary = 'Framework for loading resources from a network service' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,18 +10,31 @@ import XCTest | |
@testable import TABResourceLoader | ||
|
||
private let commonKey = "common" | ||
private let resourceNameKey = "resourceName" | ||
private let mockResourceName = "Mock" | ||
private let queryKey = "query" | ||
private let mockQuery = "example" | ||
|
||
struct MockHTTPHeaderFieldsNetworkDataResource: NetworkResourceType, DataResourceType { | ||
protocol MockResourceType: NetworkResourceType, DataResourceType { | ||
var resourceName: String { get } | ||
} | ||
|
||
struct MockHTTPHeaderFieldsNetworkDataResource: MockResourceType { | ||
typealias Model = String | ||
let url: URL | ||
let httpHeaderFields: [String: String]? | ||
let resourceName = mockResourceName | ||
|
||
var queryItems: [URLQueryItem]? { | ||
return [URLQueryItem(name: queryKey, value: mockQuery)] | ||
} | ||
|
||
func model(from data: Data) throws -> String { | ||
return "" | ||
} | ||
} | ||
|
||
final class SubclassedNetworkDataResourceService<Resource: NetworkResourceType & DataResourceType>: GenericNetworkDataResourceService<Resource> { | ||
final class SubclassedNetworkDataResourceService<Resource: MockResourceType>: GenericNetworkDataResourceService<Resource> { | ||
|
||
required init() { | ||
super.init() | ||
|
@@ -31,8 +44,15 @@ final class SubclassedNetworkDataResourceService<Resource: NetworkResourceType & | |
super.init(session: session) | ||
} | ||
|
||
override func additionalHeaderFields() -> [String: String] { | ||
return [commonKey: "subclass"] | ||
override func additionalHeaderFields<Resource: MockResourceType>(for resource: Resource) -> [String: String] { | ||
return [ | ||
commonKey: "subclass", | ||
resourceNameKey: mockResourceName, | ||
] | ||
} | ||
|
||
override func additionalQueryParameters<Resource>(for resource: Resource) -> [URLQueryItem] where Resource : DataResourceType, Resource : NetworkResourceType { | ||
return [URLQueryItem(name: resourceNameKey, value: mockResourceName)] | ||
} | ||
} | ||
|
||
|
@@ -54,14 +74,20 @@ class SubclassedNetworkJSONResourceServiceTests: XCTestCase { //swiftlint:disabl | |
let resourceHTTPHeaderFields = [commonKey: "resource"] | ||
mockResource = MockHTTPHeaderFieldsNetworkDataResource(url: mockURL, httpHeaderFields: resourceHTTPHeaderFields) | ||
testService.fetch(resource: mockResource) { _ in } | ||
XCTAssertEqual(mockSession.capturedRequest!.allHTTPHeaderFields!, resourceHTTPHeaderFields) | ||
let expectedHeaderFields = [commonKey: "resource", resourceNameKey: mockResourceName] | ||
XCTAssertEqual(mockSession.capturedRequest!.allHTTPHeaderFields!, expectedHeaderFields) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you wanted to avoid the force unwraps here you could use optional chaining with a default value?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point @KaneCheshire. i just modified what was already there. didn't even notice the force unwraps. |
||
|
||
let expectedQuery1 = "\(queryKey)=\(mockQuery)" | ||
let expectedQuery2 = "\(resourceNameKey)=\(mockResourceName)" | ||
let expectedQuery = "\(expectedQuery1)&\(expectedQuery2)" | ||
XCTAssertEqual(mockSession.capturedRequest?.url?.query, expectedQuery) | ||
} | ||
|
||
func test_finalRequestInclude_subclassHTTPHeaderFields_and_resourceHTTPHeaderFields() { | ||
let resourceHTTPHeaderFields = ["resource_key": "resource"] | ||
mockResource = MockHTTPHeaderFieldsNetworkDataResource(url: mockURL, httpHeaderFields: resourceHTTPHeaderFields) | ||
testService.fetch(resource: mockResource) { _ in } | ||
let expectedHeaderFields = [commonKey: "subclass", "resource_key": "resource"] | ||
let expectedHeaderFields = [commonKey: "subclass", resourceNameKey: mockResourceName, "resource_key": "resource"] | ||
XCTAssertEqual(mockSession.capturedRequest!.allHTTPHeaderFields!, expectedHeaderFields) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also please include
additionalQueryParameters
in the function signature as the parameter name just to go w/ the comment above.