Skip to content

Commit

Permalink
Fix LocalhostServer
Browse files Browse the repository at this point in the history
  • Loading branch information
depoon committed Jan 1, 2019
1 parent 6e19c6a commit 245e536
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions Source/Localhost/LocalhostServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,56 @@ extension LocalhostServer: LocalhostRouter {

public func get(_ path: String, routeBlock: @escaping ((URLRequest) -> LocalhostServerResponse?)) {
self.server.get(path, block: { [weak self] (req, res, next) in
self?.handleRoute(routeBlock: routeBlock, crRequest: req, crResponse: res)
self?.handleRoute(httpMethod: "GET",
routeBlock: routeBlock,
crRequest: req,
crResponse: res)
})
}

public func post(_ path: String, routeBlock: @escaping ((URLRequest) -> LocalhostServerResponse?)) {
self.server.post(path, block: { [weak self] (req, res, next) in
self?.handleRoute(routeBlock: routeBlock, crRequest: req, crResponse: res)
self?.handleRoute(httpMethod: "POST",
routeBlock: routeBlock,
crRequest: req,
crResponse: res)
})
}

public func delete(_ path: String, routeBlock: @escaping ((URLRequest) -> LocalhostServerResponse?)) {
self.server.delete(path, block: { [weak self] (req, res, next) in
self?.handleRoute(routeBlock: routeBlock, crRequest: req, crResponse: res)
self?.handleRoute(httpMethod: "DELETE",
routeBlock: routeBlock,
crRequest: req,
crResponse: res)
})

}

public func put(_ path: String, routeBlock: @escaping ((URLRequest) -> LocalhostServerResponse?)) {
self.server.put(path, block: { [weak self] (req, res, next) in
self?.handleRoute(routeBlock: routeBlock, crRequest: req, crResponse: res)
self?.handleRoute(httpMethod: "PUT",
routeBlock: routeBlock,
crRequest: req,
crResponse: res)
})
}

public func head(_ path: String, routeBlock: @escaping ((URLRequest) -> LocalhostServerResponse?)) {
self.server.head(path, block: { [weak self] (req, res, next) in
self?.handleRoute(httpMethod: "HEAD",
routeBlock: routeBlock,
crRequest: req,
crResponse: res)
})
}

public func options(_ path: String, routeBlock: @escaping ((URLRequest) -> LocalhostServerResponse?)) {
self.server.options(path, block: { [weak self] (req, res, next) in
self?.handleRoute(httpMethod: "OPTIONS",
routeBlock: routeBlock,
crRequest: req,
crResponse: res)
})

}

public func startListening(){
Expand All @@ -51,7 +79,7 @@ public class LocalhostServer {
public let portNumber: UInt
let server: CRHTTPServer

var recordedRequests: [URLRequest]
public var recordedRequests: [URLRequest]

public required init(portNumber: UInt){
server = CRHTTPServer()
Expand All @@ -64,10 +92,11 @@ public class LocalhostServer {
return LocalhostServer(portNumber: availablePort)
}

fileprivate func handleRoute(routeBlock: @escaping ((URLRequest) -> LocalhostServerResponse?),
fileprivate func handleRoute(httpMethod: String, routeBlock: @escaping ((URLRequest) -> LocalhostServerResponse?),
crRequest: CRRequest,
crResponse: CRResponse) {
var request = URLRequest(url: crRequest.url)
request.httpMethod = httpMethod
request.allHTTPHeaderFields = crRequest.allHTTPHeaderFields
if let body = crRequest.body as? Data {
request.httpBody = body
Expand Down Expand Up @@ -103,8 +132,7 @@ public struct LocalhostRequest {
public init(method: LocalhostRequestMethod, url: URL) {
self.method = method
self.url = url
}

}
}

protocol LocalhostResponse {
Expand All @@ -120,6 +148,4 @@ struct LocalhostJsonResponse: LocalhostResponse {
self.httpUrlResponse = httpUrlResponse
self.body = body
}


}

0 comments on commit 245e536

Please sign in to comment.