Skip to content

Commit

Permalink
feat(SCREO-xxxx): Fix issue swifter (#66)
Browse files Browse the repository at this point in the history
* "import Swifter" no more mandatory in hosting application

* "import Swifter" no more mandatory in hosting application
  • Loading branch information
skychiarottoa authored Mar 21, 2022
1 parent 7320fdc commit da7c81a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 43 deletions.
1 change: 0 additions & 1 deletion XCode/Sources/ImageReponse.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import Swifter

struct ImageReponse {
let path: String
Expand Down
8 changes: 0 additions & 8 deletions XCode/Sources/Model/File.swift

This file was deleted.

1 change: 0 additions & 1 deletion XCode/Sources/Model/HttpRequest.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import Swifter

public protocol HttpRequest {
var path: String { get }
Expand Down
31 changes: 31 additions & 0 deletions XCode/Sources/Model/HttpResponse.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

22 changes: 6 additions & 16 deletions XCode/Sources/UITests/UITestHttpServerBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class UITestHttpServerBuilder {
DispatchQueue.main.sync {
asserts(request.httpRequest())
}
return HttpResponse.notFound
return HttpResponse.notFound().toSwifter()
}
return self
}
Expand Down Expand Up @@ -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()
}
}
}
Expand Down Expand Up @@ -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()
}
}

Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions XCode/Sources/UnitTests/SkyUnitTestCase.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Swifter
import XCTest

open class SkyUnitTestCase: XCTestCase {
Expand All @@ -16,8 +15,3 @@ open class SkyUnitTestCase: XCTestCase {
}
}

extension HttpResponse {
static func ok(_ data: Data) -> HttpResponse {
return HttpResponse.ok(HttpResponseBody.data(data))
}
}
6 changes: 3 additions & 3 deletions XCode/Sources/UnitTests/UTHttpServerBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -23,7 +23,7 @@ public class UTHttpServerBuilder {
DispatchQueue.main.sync {
asserts(request.httpRequest())
}
return HttpResponse.badRequest(HttpResponseBody.html(""))
return HttpResponse.badRequest().toSwifter()
}
return self
}
Expand All @@ -46,7 +46,7 @@ public class UTHttpServerBuilder {
lock.wait()
callCount += 1
lock.signal()
return completion(request.httpRequest(), callCount)
return completion(request.httpRequest(), callCount).toSwifter()
}
}

Expand Down
15 changes: 7 additions & 8 deletions XCode/Tests/SkyTestFoundationTests/SkyUnitTestCaseTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import XCTest
import Swifter
@testable import SkyTestFoundation

class SkyUnitTestCaseTests: SkyUnitTestCase {
Expand All @@ -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")!
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down

0 comments on commit da7c81a

Please sign in to comment.