diff --git a/XCode/Sources/ImageReponse.swift b/XCode/Sources/ImageReponse.swift index f929fc3..169fd81 100644 --- a/XCode/Sources/ImageReponse.swift +++ b/XCode/Sources/ImageReponse.swift @@ -1,5 +1,4 @@ import Foundation -import Swifter struct ImageReponse { let path: String diff --git a/XCode/Sources/Model/File.swift b/XCode/Sources/Model/File.swift deleted file mode 100644 index e93c58d..0000000 --- a/XCode/Sources/Model/File.swift +++ /dev/null @@ -1,8 +0,0 @@ -// -// File.swift -// -// -// Created by sky on 21/03/22. -// - -import Foundation diff --git a/XCode/Sources/Model/HttpRequest.swift b/XCode/Sources/Model/HttpRequest.swift index 7f3669b..87f60d6 100644 --- a/XCode/Sources/Model/HttpRequest.swift +++ b/XCode/Sources/Model/HttpRequest.swift @@ -1,5 +1,4 @@ import Foundation -import Swifter public protocol HttpRequest { var path: String { get } diff --git a/XCode/Sources/Model/HttpResponse.swift b/XCode/Sources/Model/HttpResponse.swift new file mode 100644 index 0000000..7d22cc9 --- /dev/null +++ b/XCode/Sources/Model/HttpResponse.swift @@ -0,0 +1,31 @@ +import Foundation +import Swifter + +public class HttpResponse { + let body: Data + let statusCode: Int + let headers: [String : String] + + public init(body: Data = Data(), statusCode: Int = 200, headers: [String: String] = [:]) { + self.body = body + self.statusCode = statusCode + self.headers = headers + } +} + +extension HttpResponse { + public static func notFound() -> HttpResponse { + return HttpResponse(statusCode: 404) + } + + public static func badRequest() -> HttpResponse { + return HttpResponse(statusCode: 400) + } + + func toSwifter() -> Swifter.HttpResponse { + return Swifter.HttpResponse.raw(statusCode, "", headers) { (writer) in + try writer.write(self.body) + } + } +} + diff --git a/XCode/Sources/UITests/UITestHttpServerBuilder.swift b/XCode/Sources/UITests/UITestHttpServerBuilder.swift index c9aa6ed..2e0a724 100644 --- a/XCode/Sources/UITests/UITestHttpServerBuilder.swift +++ b/XCode/Sources/UITests/UITestHttpServerBuilder.swift @@ -63,7 +63,7 @@ public class UITestHttpServerBuilder { DispatchQueue.main.sync { asserts(request.httpRequest()) } - return HttpResponse.notFound + return HttpResponse.notFound().toSwifter() } return self } @@ -116,9 +116,7 @@ public class UITestHttpServerBuilder { } else { data = UITestHttpServerBuilder.drawOnImage(text: request.path) } - return HttpResponse.raw(200, "", nil) { (writer) in - try writer.write(data) - } + return HttpResponse(body: data, statusCode: 200).toSwifter() } } } @@ -146,7 +144,7 @@ public class UITestHttpServerBuilder { } } sleep(response.responseTime ?? 0) - return HttpResponse.raw(statusCode: response.statusCode, body: response.body) + return HttpResponse(body: response.body, statusCode: response.statusCode).toSwifter() } } @@ -155,14 +153,14 @@ public class UITestHttpServerBuilder { httpServer.buildRoute(endpointCallBackResponse.endpoint) { request in Logger.info("Handled request:\(request.method) \(request.path) Params:\(request.queryParams)") self.updateEndpointCallCount(endpointCallBackResponse.endpoint) - return endpointCallBackResponse.callBack(request.httpRequest()) + return endpointCallBackResponse.callBack(request.httpRequest()).toSwifter() } } if httpServer.notFoundHandler == nil { httpServer.notFoundHandler = { request in Logger.info("NOT handled: \(request.method) \(request.path) Params:\(request.queryParams)") - return HttpResponse.notFound + return HttpResponse.notFound().toSwifter() } } @@ -191,14 +189,6 @@ public class UITestHttpServerBuilder { } } -public extension HttpResponse { - static func raw(statusCode: Int, body: Data) -> HttpResponse { - return HttpResponse.raw(statusCode, "", nil) { (writer) in - try writer.write(body) - } - } -} - // Hosting application see only type of this framework extension Swifter.HttpRequest { func httpRequest() -> HttpRequest { @@ -226,7 +216,7 @@ private struct ConcreteHttpRequest: HttpRequest { extension Swifter.HttpServer { - func buildRoute(_ endpoint: HttpEndpoint, body: ((Swifter.HttpRequest) -> HttpResponse)?) { + func buildRoute(_ endpoint: HttpEndpoint, body: ((Swifter.HttpRequest) -> Swifter.HttpResponse)?) { switch endpoint.method { case .delete: return self.DELETE[endpoint.path] = body diff --git a/XCode/Sources/UnitTests/SkyUnitTestCase.swift b/XCode/Sources/UnitTests/SkyUnitTestCase.swift index 784e1ba..b1de272 100644 --- a/XCode/Sources/UnitTests/SkyUnitTestCase.swift +++ b/XCode/Sources/UnitTests/SkyUnitTestCase.swift @@ -1,4 +1,3 @@ -import Swifter import XCTest open class SkyUnitTestCase: XCTestCase { @@ -16,8 +15,3 @@ open class SkyUnitTestCase: XCTestCase { } } -extension HttpResponse { - static func ok(_ data: Data) -> HttpResponse { - return HttpResponse.ok(HttpResponseBody.data(data)) - } -} diff --git a/XCode/Sources/UnitTests/UTHttpServerBuilder.swift b/XCode/Sources/UnitTests/UTHttpServerBuilder.swift index 5e690be..aff079b 100644 --- a/XCode/Sources/UnitTests/UTHttpServerBuilder.swift +++ b/XCode/Sources/UnitTests/UTHttpServerBuilder.swift @@ -13,7 +13,7 @@ public class UTHttpServerBuilder { callCount += 1 lock.signal() Logger.info("Handled request:\(request.method) \(request.path) Params:\(request.queryParams) call count:\(callCount)") - return completion(request.httpRequest(), callCount) + return completion(request.httpRequest(), callCount).toSwifter() } return self } @@ -23,7 +23,7 @@ public class UTHttpServerBuilder { DispatchQueue.main.sync { asserts(request.httpRequest()) } - return HttpResponse.badRequest(HttpResponseBody.html("")) + return HttpResponse.badRequest().toSwifter() } return self } @@ -46,7 +46,7 @@ public class UTHttpServerBuilder { lock.wait() callCount += 1 lock.signal() - return completion(request.httpRequest(), callCount) + return completion(request.httpRequest(), callCount).toSwifter() } } diff --git a/XCode/Tests/SkyTestFoundationTests/SkyUnitTestCaseTests.swift b/XCode/Tests/SkyTestFoundationTests/SkyUnitTestCaseTests.swift index 10bf8e1..573a9c4 100644 --- a/XCode/Tests/SkyTestFoundationTests/SkyUnitTestCaseTests.swift +++ b/XCode/Tests/SkyTestFoundationTests/SkyUnitTestCaseTests.swift @@ -1,5 +1,4 @@ import XCTest -import Swifter @testable import SkyTestFoundation class SkyUnitTestCaseTests: SkyUnitTestCase { @@ -13,7 +12,7 @@ class SkyUnitTestCaseTests: SkyUnitTestCase { let exp = expectation(description: "") httpServerBuilder .route("/login") { (_, _) -> (HttpResponse) in - return HttpResponse.ok(HttpResponseBody.data(Data())) + return HttpResponse() } .buildAndStart() let url = URL(string: "http://localhost:8080/login")! @@ -33,10 +32,10 @@ class SkyUnitTestCaseTests: SkyUnitTestCase { httpServerBuilder .route("/endpoint00") { (_, _) -> (HttpResponse) in - return HttpResponse.ok(HttpResponseBody.data(Data())) + return HttpResponse() } .route("/endpoint01", { (_, _) -> (HttpResponse) in - return HttpResponse.ok(HttpResponseBody.data(Data())) + return HttpResponse() }) .buildAndStart() @@ -65,7 +64,7 @@ class SkyUnitTestCaseTests: SkyUnitTestCase { httpServerBuilder .route("/endpoint00") { (_, _) -> (HttpResponse) in - return HttpResponse.ok(HttpResponseBody.data(Data())) + return HttpResponse() } .onUnexpected { _ in exp01.fulfill() // "Unexpected request.path @@ -106,7 +105,7 @@ class SkyUnitTestCaseTests: SkyUnitTestCase { let exp = expectation(description: "..") httpServerBuilder .route("login") { (_, _) -> (HttpResponse) in - return HttpResponse.ok(HttpResponseBody.data(Data())) + return HttpResponse() } .buildAndStart() @@ -129,11 +128,11 @@ class SkyUnitTestCaseTests: SkyUnitTestCase { httpServerBuilder .route("/endpoint/1") { (request, callCount) -> (HttpResponse) in callCount0 = callCount - return HttpResponse.ok(HttpResponseBody.data(Data())) + return HttpResponse() } .route("/endpoint/2") { (request, callCount) -> (HttpResponse) in callCount1 = callCount - return HttpResponse.ok(HttpResponseBody.data(Data())) + return HttpResponse() } .buildAndStart()