Skip to content

Instantly share code, notes, and snippets.

@tovkal
Created August 24, 2017 21:00
Show Gist options
  • Save tovkal/036d5dbae668768d13e5d37161433754 to your computer and use it in GitHub Desktop.
Save tovkal/036d5dbae668768d13e5d37161433754 to your computer and use it in GitHub Desktop.
import Foundation
import Swifter
enum HTTPMethod {
case POST
case GET
}
struct HTTPStubInfo {
let url: String
let jsonFilename: String
let method: HTTPMethod
}
let initialStubs = [
HTTPStubInfo(url: "/api/feed", jsonFilename: "feed", method: .GET),
HTTPStubInfo(url: "/api/createPost", jsonFilename: "createPost", method: .POST),
]
class HTTPDynamicStubs {
var server = HttpServer()
func setUp() {
setupInitialStubs()
try! server.start()
}
func tearDown() {
server.stop()
}
func setupInitialStubs() {
// Setting up all the initial mocks from the array
for stub in initialStubs {
setupStub(url: stub.url, filename: stub.jsonFilename, method: stub.method)
}
}
public func setupStub(url: String, filename: String, method: HTTPMethod = .GET) {
let testBundle = Bundle(for: type(of: self))
let filePath = testBundle.path(forResource: filename, ofType: "json")
let fileUrl = URL(fileURLWithPath: filePath!)
let data = try! Data(contentsOf: fileUrl, options: .uncached)
// Looking for a file and converting it to JSON
let json = dataToJSON(data: data)
// Swifter makes it very easy to create stubbed responses
let response: ((HttpRequest) -> HttpResponse) = { _ in
return HttpResponse.ok(.json(json as AnyObject))
}
switch method {
case .GET : server.GET[url] = response
case .POST: server.POST[url] = response
}
}
func dataToJSON(data: Data) -> Any? {
do {
return try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
} catch let myJSONError {
print(myJSONError)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment