Skip to content

Commit

Permalink
Merge pull request #6 from depoon/update-LocalhostServer
Browse files Browse the repository at this point in the history
Update LocalhostServer
  • Loading branch information
depoon authored Feb 6, 2019
2 parents 3601b20 + 583ee38 commit 72fa9e7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
43 changes: 42 additions & 1 deletion Source/Localhost/LocalhostServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,51 @@ public class LocalhostServer {
crResponse.send(data)
}
}

public func route(method: LocalhostRequestMethod,
path: String,
responseData: Data,
statusCode: Int = 200,
responseHeaderFields: [String: String]? = nil) {
let routeBlock = self.routeBlock(path: path,
responseData: responseData,
statusCode: statusCode,
responseHeaderFields: responseHeaderFields)
switch method {
case .GET:
self.get(path, routeBlock: routeBlock)
case .POST:
self.post(path, routeBlock: routeBlock)
case .PUT:
self.put(path, routeBlock: routeBlock)
case .DELETE:
self.delete(path, routeBlock: routeBlock)
case .HEAD:
self.head(path, routeBlock: routeBlock)
case .OPTIONS:
self.options(path, routeBlock: routeBlock)
}
}

fileprivate func routeBlock(path: String,
responseData: Data,
statusCode: Int = 200,
responseHeaderFields: [String: String]? = nil) -> ((URLRequest) -> LocalhostServerResponse?) {
let block: ((URLRequest) -> LocalhostServerResponse?) = { _ in
let serverPort = self.portNumber
let requestURL: URL = URL(string: "http://localhost:\(serverPort)\(path)")!
let httpUrlResponse = HTTPURLResponse(url: requestURL,
statusCode: statusCode,
httpVersion: nil,
headerFields: responseHeaderFields)!
return LocalhostServerResponse(httpUrlResponse: httpUrlResponse, data: responseData)
}
return block
}
}

public enum LocalhostRequestMethod {
case get, post, put, delete, patch
case GET, POST, PUT, DELETE, HEAD, OPTIONS
}
public struct LocalhostRequest {

Expand Down
2 changes: 1 addition & 1 deletion SwiftLocalhost.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "SwiftLocalhost"
s.version = "0.0.6"
s.version = "0.0.7"
s.swift_version = '4.2'
s.summary = "Swift Localhost Server for your testing needs"
s.description = <<-DESC
Expand Down

0 comments on commit 72fa9e7

Please sign in to comment.