Skip to content

Commit

Permalink
Merge pull request #33 from allaboutapps/feature/add-support-for-reso…
Browse files Browse the repository at this point in the history
…urces-in-subdirectory

Added support for loading file from bundle in directory
  • Loading branch information
mpoimer authored Jun 29, 2023
2 parents dd60740 + 9276618 commit d635142
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/Alamofire/Alamofire.git",
"state": {
"branch": null,
"revision": "f82c23a8a7ef8dc1a49a8bfc6a96883e79121864",
"version": "5.5.0"
"revision": "bc268c28fb170f494de9e9927c371b8342979ece",
"version": "5.7.1"
}
}
]
Expand Down
11 changes: 6 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ let package = Package(
.macOS(.v10_12),
.iOS(.v11),
.tvOS(.v11),
.watchOS(.v3)
.watchOS(.v3),
],
products: [
.library(name: "Fetch", targets: ["Fetch"])
.library(name: "Fetch", targets: ["Fetch"]),
],
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.5.0")
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.7.0"),
],
targets: [
.target(name: "Fetch",
dependencies: ["Alamofire"]),
.testTarget(name: "FetchTests",
dependencies: ["Fetch"],
resources: [
.process("modela.json")
])
.process("modela.json"),
.copy("TestFiles")
]),
],
swiftLanguageVersions: [.v5]
)
31 changes: 28 additions & 3 deletions Sources/Fetch/Stub/Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public struct StubResponse: Stub {
/// - delay: Simulated network delay
/// - bundle: The `Bundle` containing the file, default Bundle.main
public init(statusCode: StatusCode, fileName: String, headers: HTTPHeaders = HTTPHeaders(), delay: TimeInterval, bundle: Bundle = Bundle.main) {
let split = fileName.split(separator: ".")
let name = String(split[0])
let fileExtension = String(split[1])
let fileExtension = fileName.fileExtension
let name = fileName.fileName

let path = bundle.path(forResource: name, ofType: fileExtension)!
var headersToUse = headers

Expand All @@ -64,6 +64,21 @@ public struct StubResponse: Stub {
self.init(statusCode: statusCode, data: try! Data(contentsOf: URL(fileURLWithPath: path)), headers: headersToUse, delay: delay)
}

public init(statusCode: StatusCode, fileName: String, directory: String, headers: HTTPHeaders = .init(), delay: TimeInterval, bundle: Bundle = .main) {
let fileExtension = fileName.fileExtension
let name = fileName.fileName

let path = bundle.path(forResource: name, ofType: fileExtension, inDirectory: directory)!

var headersToUse = headers

if fileExtension == "json" {
headersToUse.add(HTTPHeader.contentType("application/json"))
}

self.init(statusCode: statusCode, data: try! Data(contentsOf: URL(fileURLWithPath: path)), headers: headersToUse, delay: delay)
}

/// Initializes a new `StubResponse` using a `Encodable` and a `JSONEncoder`
///
/// - Parameters:
Expand Down Expand Up @@ -93,3 +108,13 @@ public struct StubError: Stub {
self.delay = delay
}
}

private extension String {
var fileName: String {
URL(fileURLWithPath: self).deletingPathExtension().lastPathComponent
}

var fileExtension: String {
URL(fileURLWithPath: self).pathExtension
}
}
29 changes: 29 additions & 0 deletions Tests/FetchTests/StubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ class StubTests: XCTestCase {
waitForExpectations(timeout: 5, handler: nil)
}

func testStubbedModelFromFileInDirectory() {
let expectation = self.expectation(description: "Fetch model")
let resource = Resource<ModelA>(
method: .get,
path: "/test")

APIClient.shared.stubProvider.register(
stub: StubResponse(
statusCode: 200,
fileName: "copyModelA.json",
directory: "TestFiles",
delay: 0.1,
bundle: Bundle.module
),
for: resource
)

resource.request { (result) in
switch result {
case .success(let response):
XCTAssertEqual(response.model.a, "a")
expectation.fulfill()
case .failure:
XCTFail("Request did not return value")
}
}
waitForExpectations(timeout: 5, handler: nil)
}

func testStubbedHTTPHeaders() {
let expectation = self.expectation(description: "Fetch model")
let headers = HTTPHeaders([
Expand Down
3 changes: 3 additions & 0 deletions Tests/FetchTests/TestFiles/copyModelA.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": "a"
}

0 comments on commit d635142

Please sign in to comment.