Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Mockingjay/Builders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,26 @@ public func jsonData(_ data: Data, status: Int = 200, headers: [String:String]?
return http(status, headers: headers, download: .content(data))(request)
}
}

public func content(_ body: String, status: Int = 200, headers: [String:String]? = nil) -> (_ request: URLRequest) -> Response {
return { (request: URLRequest) in
var headers = headers ?? [String:String]()
if headers["Content-Type"] == nil {
headers["Content-Type"] = "text/plain; charset=utf-8"
}
let data: Data = body.data(using: String.Encoding.utf8) ?? Data()
return http(status, headers: headers, download: Download.content(data as Data))(request)
}
}

public func file(_ file: String, _ type: String = "json", status: Int = 200, headers: [String:String]? = nil) -> (_ request: URLRequest) -> Response {
return { (request: URLRequest) in
let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!
if let path = Bundle.main.path(forResource: file, ofType: type) {
if let data = NSData(contentsOfFile: path) {
return content(String(data: data as Data, encoding: .utf8) ?? "", status: status, headers: headers)(request)
}
}
return .failure(NSError())
}
}