Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #34 from antoniostrijdom/intermittent-failures
Browse files Browse the repository at this point in the history
Intermittent failures in Shock v6
  • Loading branch information
albertodebortoli committed Jun 28, 2021
2 parents 6122164 + ef03763 commit 2ce5878
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 249 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PODS:
- GRMustache.swift (4.0.1)
- JustLog (3.1.3):
- SwiftyBeaver (~> 1.9.1)
- Shock (6.0.1):
- Shock (6.0.2):
- GRMustache.swift (~> 4.0.1)
- SwiftNIOHTTP1 (~> 2.22.1)
- SwiftNIO (2.22.1):
Expand Down Expand Up @@ -54,7 +54,7 @@ SPEC CHECKSUMS:
CNIOSHA1: fd4d38fb42778ed971a75d87530f842106d4b4f4
GRMustache.swift: a6436504284b22b4b05daf5f46f77bd3fe00a9a2
JustLog: c1b12fe8fc6a7c22cc31dd874fe7776eaa7089d2
Shock: 31b6786d7716dba5c6dff68c9e3949ab96aebf30
Shock: 708aafb186e31cef487afbf87ce7e8cac337fb8e
SwiftNIO: 763188229de166e046cc8975383cca388832e992
SwiftNIOConcurrencyHelpers: 34d2e9d4d2b7886fa765086e845e6b19417be927
SwiftNIOHTTP1: c0a9ce48ba256f349af4f09648a0e0939b11a65c
Expand Down
4 changes: 4 additions & 0 deletions Example/Shock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
0463BE14252DA52700C18221 /* ClosureMiddlewareTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0463BE13252DA52700C18221 /* ClosureMiddlewareTests.swift */; };
0463BE1A252DA54700C18221 /* ShockTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0463BE19252DA54700C18221 /* ShockTestCase.swift */; };
142CB1172680B93100E0B3F4 /* testSimpleRouteWithVariables.txt in Resources */ = {isa = PBXBuildFile; fileRef = 142CB1162680B93100E0B3F4 /* testSimpleRouteWithVariables.txt */; };
14739D0D25262B640031286B /* SocketServerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14739D0C25262B640031286B /* SocketServerTests.swift */; };
14C308E82649781700B85451 /* testCustomRoute2.txt in Resources */ = {isa = PBXBuildFile; fileRef = 14C308E72649781700B85451 /* testCustomRoute2.txt */; };
28CF36BB9C72A116B41A9CCA /* Pods_Shock_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 504237DD0DF4E81BD4C74B63 /* Pods_Shock_Example.framework */; };
Expand Down Expand Up @@ -48,6 +49,7 @@
/* Begin PBXFileReference section */
0463BE13252DA52700C18221 /* ClosureMiddlewareTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClosureMiddlewareTests.swift; sourceTree = "<group>"; };
0463BE19252DA54700C18221 /* ShockTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShockTestCase.swift; sourceTree = "<group>"; };
142CB1162680B93100E0B3F4 /* testSimpleRouteWithVariables.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = testSimpleRouteWithVariables.txt; sourceTree = "<group>"; };
14739D0C25262B640031286B /* SocketServerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketServerTests.swift; sourceTree = "<group>"; };
14C308E72649781700B85451 /* testCustomRoute2.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = testCustomRoute2.txt; sourceTree = "<group>"; };
2AF495106DF32828591B5E10 /* Pods-Shock_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Shock_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Shock_Tests/Pods-Shock_Tests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -211,6 +213,7 @@
children = (
6DF676FB1F8793F700E1783A /* testTemplatedRoute.mustache */,
6D718E361F8777660070B764 /* testSimpleRoute.txt */,
142CB1162680B93100E0B3F4 /* testSimpleRouteWithVariables.txt */,
6D718E381F877AB30070B764 /* testRedirectRoute.txt */,
6D7BDCF51FA72BC300488DB5 /* testCustomRoute.txt */,
14C308E72649781700B85451 /* testCustomRoute2.txt */,
Expand Down Expand Up @@ -336,6 +339,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
142CB1172680B93100E0B3F4 /* testSimpleRouteWithVariables.txt in Resources */,
14C308E82649781700B85451 /* testCustomRoute2.txt in Resources */,
6DF676FC1F8793F700E1783A /* testTemplatedRoute.mustache in Resources */,
6D718E391F877AB30070B764 /* testRedirectRoute.txt in Resources */,
Expand Down
1 change: 1 addition & 0 deletions Example/Tests/Fixtures/testSimpleRouteWithVariables.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testSimpleRoute (with variables) test fixture
158 changes: 58 additions & 100 deletions Example/Tests/RouteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,57 @@ class RouteTests: ShockTestCase {
}

func testSimpleRouteWithVariables() {
let route: MockHTTPRoute = .simple(method: .get, urlPath: "/simple/:foo", code: 200, filename: "testSimpleRoute.txt")
let route: MockHTTPRoute = .simple(method: .get, urlPath: "/simple/:foo", code: 200, filename: "testSimpleRouteWithVariables.txt")
server.setup(route: route)

let expectation = self.expectation(description: "Expect 200 response with response body")

HTTPClient.get(url: "\(server.hostURL)/simple/1") { code, body, headers, error in
XCTAssertEqual(code, 200)
XCTAssertEqual(body, "testSimpleRoute (with variables) test fixture\n")
expectation.fulfill()
}
self.waitForExpectations(timeout: 2.0, handler: nil)
}

func testSimpleRouteWithAndWithoutVariables() {
let withoutRoute: MockHTTPRoute = .simple(method: .get, urlPath: "/simple/withoutvariables", code: 200, filename: "testSimpleRoute.txt")
let withRoute: MockHTTPRoute = .simple(method: .get, urlPath: "/simple/withvariables/:foo", code: 200, filename: "testSimpleRouteWithVariables.txt")
server.setup(route: .collection(routes: [withRoute, withoutRoute]))

let expectation = self.expectation(description: "Expect 200 response with response body")

HTTPClient.get(url: "\(server.hostURL)/simple/withvariables/1") { code, body, headers, error in
XCTAssertEqual(code, 200)
XCTAssertEqual(body, "testSimpleRoute (with variables) test fixture\n")
expectation.fulfill()
}
self.waitForExpectations(timeout: 2.0, handler: nil)
}

func testSimpleRouteWithEmptyURLPath() {
let withoutRoute: MockHTTPRoute = .simple(method: .get, urlPath: "", code: 200, filename: "testSimpleRoute.txt")
let withRoute: MockHTTPRoute = .simple(method: .get, urlPath: "/simple/withvariables/:foo", code: 200, filename: "testSimpleRouteWithVariables.txt")
server.setup(route: .collection(routes: [withRoute, withoutRoute]))

let expectation = self.expectation(description: "Expect 200 response with response body")

HTTPClient.get(url: "\(server.hostURL)/simple/withvariables/1") { code, body, headers, error in
XCTAssertEqual(code, 200)
XCTAssertEqual(body, "testSimpleRoute (with variables) test fixture\n")
expectation.fulfill()
}
self.waitForExpectations(timeout: 2.0, handler: nil)
}

func testSimpleRouteWithEmptyURLPathAlternate() {
let withoutRoute: MockHTTPRoute = .simple(method: .get, urlPath: "/simple/withoutvariables", code: 200, filename: "testSimpleRoute.txt")
let withRoute: MockHTTPRoute = .simple(method: .get, urlPath: "", code: 200, filename: "testSimpleRouteWithVariables.txt")
server.setup(route: .collection(routes: [withRoute, withoutRoute]))

let expectation = self.expectation(description: "Expect 200 response with response body")

HTTPClient.get(url: "\(server.hostURL)/simple/withoutvariables") { code, body, headers, error in
XCTAssertEqual(code, 200)
XCTAssertEqual(body, "testSimpleRoute test fixture\n")
expectation.fulfill()
Expand Down Expand Up @@ -76,6 +121,18 @@ class RouteTests: ShockTestCase {
}

func testSimpleRouteEquivalence() {
var route1 = MockHTTPRoute.simple(method: .get, urlPath: "/restaurants/bypostcode/:postcode", code: 200, filename: nil)
var route2 = MockHTTPRoute.simple(method: .get, urlPath: "/restaurants/bypostcode/:postcode", code: 200, filename: nil)
XCTAssertEqual(route1, route2, "Simple gets should be equal")
route1 = MockHTTPRoute.simple(method: .get, urlPath: "/restaurants/bypostcode/:postcode", code: 200, filename: nil)
route2 = MockHTTPRoute.simple(method: .get, urlPath: "/restaurants/bypostcode/AR511AA", code: 200, filename: nil)
XCTAssertEqual(route1, route2, "Simple gets should be equal")
route1 = MockHTTPRoute.simple(method: .get, urlPath: "/restaurants/bypostcode/:postcode", code: 200, filename: nil)
route2 = MockHTTPRoute.simple(method: .get, urlPath: "restaurants/:id/reviews", code: 200, filename: nil)
XCTAssertNotEqual(route1, route2, "Paths are different, should not be equal")
}

func testSimpleRouteWithVariablesEquivalence() {
var route1 = MockHTTPRoute.simple(method: .get, urlPath: "foo/bar", code: 200, filename: nil)
var route2 = MockHTTPRoute.simple(method: .get, urlPath: "foo/bar", code: 200, filename: nil)
XCTAssertEqual(route1, route2, "Simple gets should be equal")
Expand Down Expand Up @@ -191,103 +248,4 @@ class RouteTests: ShockTestCase {
])
XCTAssertNotEqual(route1, route2, "Paths are different, should not be equal")
}

func testSimpleRouteHash() {
let route1 = MockHTTPRoute.simple(method: .get, urlPath: "foo/bar", code: 200, filename: nil)
let route2 = MockHTTPRoute.simple(method: .get, urlPath: "foo/bar", code: 200, filename: nil)
var dict = [MockHTTPRoute : String]()
dict[route1] = "Foo"
XCTAssertEqual(dict.count, 1, "There should be one element in the dictionary")
dict[route2] = "Bar"
XCTAssertEqual(dict.count, 1, "There should still be one element in the dictionary")
let route3 = MockHTTPRoute.simple(method: .post, urlPath: "foo/bar", code: 200, filename: nil)
dict[route3] = "Foo"
XCTAssertEqual(dict.count, 2, "There should now be two elements in the dictionary")
XCTAssertEqual(dict[route2], "Bar")
XCTAssertEqual(dict[route3], "Foo")
}

func testCustomRouteHash() {
let route1 = MockHTTPRoute.custom(method: .get, urlPath: "foo/bar", query: ["query":"value"],
requestHeaders: ["HTTPHeader":"false"], responseHeaders: ["HTTPHeader":"true"], code: 200, filename: nil)
let route2 = MockHTTPRoute.custom(method: .get, urlPath: "foo/bar", query: ["query":"value"],
requestHeaders: ["HTTPHeader":"false"], responseHeaders: ["HTTPHeader":"true"], code: 200, filename: nil)
var dict = [MockHTTPRoute : String]()
dict[route1] = "Foo"
XCTAssertEqual(dict.count, 1, "There should be one element in the dictionary")
dict[route2] = "Bar"
XCTAssertEqual(dict.count, 1, "There should still be one element in the dictionary")
let route3 = MockHTTPRoute.custom(method: .post, urlPath: "foo/bar", query: ["query":"value"],
requestHeaders: ["HTTPHeader":"false"], responseHeaders: ["HTTPHeader":"true"], code: 200, filename: nil)
dict[route3] = "Foo"
XCTAssertEqual(dict.count, 2, "There should now be two elements in the dictionary")
XCTAssertEqual(dict[route2], "Bar")
XCTAssertEqual(dict[route3], "Foo")
}

func testTemplateRouteHash() {
let route1 = MockHTTPRoute.template(method: .get, urlPath: "foo/bar", code: 200, filename: nil, templateInfo: ["Value" : 1])
let route2 = MockHTTPRoute.template(method: .get, urlPath: "foo/bar", code: 200, filename: nil, templateInfo: ["Value" : 1])
var dict = [MockHTTPRoute : String]()
dict[route1] = "Foo"
XCTAssertEqual(dict.count, 1, "There should be one element in the dictionary")
dict[route2] = "Bar"
XCTAssertEqual(dict.count, 1, "There should still be one element in the dictionary")
let route3 = MockHTTPRoute.template(method: .post, urlPath: "foo/bar", code: 200, filename: nil, templateInfo: ["Value" : 1])
dict[route3] = "Foo"
XCTAssertEqual(dict.count, 2, "There should now be two elements in the dictionary")
XCTAssertEqual(dict[route2], "Bar")
XCTAssertEqual(dict[route3], "Foo")
}

func testRedirectRouteHash() {
let route1 = MockHTTPRoute.redirect(urlPath: "foo/bar", destination: "bar/foo")
let route2 = MockHTTPRoute.redirect(urlPath: "foo/bar", destination: "bar/foo")
var dict = [MockHTTPRoute : String]()
dict[route1] = "Foo"
XCTAssertEqual(dict.count, 1, "There should be one element in the dictionary")
dict[route2] = "Bar"
XCTAssertEqual(dict.count, 1, "There should still be one element in the dictionary")
let route3 = MockHTTPRoute.redirect(urlPath: "bar/foo", destination: "bar/foo")
dict[route3] = "Foo"
XCTAssertEqual(dict.count, 2, "There should now be two elements in the dictionary")
XCTAssertEqual(dict[route2], "Bar")
XCTAssertEqual(dict[route3], "Foo")
}

func testCollectionRouteHash() {
let route1 = MockHTTPRoute.collection(routes: [
.simple(method: .get, urlPath: "foo/bar", code: 200, filename: nil)
])
let route2 = MockHTTPRoute.collection(routes: [
.simple(method: .get, urlPath: "foo/bar", code: 200, filename: nil)
])
var dict = [MockHTTPRoute : String]()
dict[route1] = "Foo"
XCTAssertEqual(dict.count, 1, "There should be one element in the dictionary")
dict[route2] = "Bar"
XCTAssertEqual(dict.count, 1, "There should still be one element in the dictionary")
let route3 = MockHTTPRoute.collection(routes: [
.simple(method: .post, urlPath: "foo/bar", code: 200, filename: nil)
])
dict[route3] = "Foo"
XCTAssertEqual(dict.count, 2, "There should now be two elements in the dictionary")
XCTAssertEqual(dict[route2], "Bar")
XCTAssertEqual(dict[route3], "Foo")
}

func testTimeoutRouteHash() {
let route1 = MockHTTPRoute.timeout(method: .get, urlPath: "foo/bar", timeoutInSeconds: 1)
let route2 = MockHTTPRoute.timeout(method: .get, urlPath: "foo/bar", timeoutInSeconds: 1)
var dict = [MockHTTPRoute : String]()
dict[route1] = "Foo"
XCTAssertEqual(dict.count, 1, "There should be one element in the dictionary")
dict[route2] = "Bar"
XCTAssertEqual(dict.count, 1, "There should still be one element in the dictionary")
let route3 = MockHTTPRoute.timeout(method: .post, urlPath: "foo/bar", timeoutInSeconds: 1)
dict[route3] = "Foo"
XCTAssertEqual(dict.count, 2, "There should now be two elements in the dictionary")
XCTAssertEqual(dict[route2], "Bar")
XCTAssertEqual(dict[route3], "Foo")
}
}
2 changes: 1 addition & 1 deletion Shock.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Shock'
s.version = '6.0.1'
s.version = '6.0.2'
s.summary = 'A HTTP mocking framework written in Swift.'

s.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion Shock/Classes/Middleware/ClosureMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public class ClosureMiddleware: Middleware {
public struct ClosureMiddleware: Middleware {

public typealias Closure = (_ request: MiddlewareRequestContext,
_ response: MiddlewareResponseContext,
Expand Down
36 changes: 21 additions & 15 deletions Shock/Classes/Middleware/Middleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Jack Newcombe on 01/10/2020.
//

import Foundation
import NIO

public typealias HandlerClosure = (MiddlewareRequestContext, MiddlewareResponseContext) -> Void

Expand Down Expand Up @@ -71,26 +71,36 @@ class MiddlewareService {
let next: () -> Void
}

private(set) var middleware: [Middleware] = []

private var context: MiddlewareContext?

var notFoundHandler: HandlerClosure?
private let middleware: [Middleware]
private let notFoundHandler: HandlerClosure?

public init(middleware: [Middleware], notFoundHandler: HandlerClosure?) {
self.middleware = middleware
self.notFoundHandler = notFoundHandler
}

func executeAll(forRequest request: MockNIOHTTPRequest) -> MiddlewareContext? {
executeAll(forRequest: request, middleware: middleware)
func executeAll(forRequest request: MockNIOHTTPRequest) -> EventLoopFuture<MiddlewareContext?> {
let promise = request.eventLoop.makePromise(of: MiddlewareContext?.self)

request.eventLoop.execute {
// _MiddlewareResponseContext is a reference type that is updated across the registered middlewares
let responseContext = _MiddlewareResponseContext()
let context = self.executeAll(forRequest: request, middleware: self.middleware, responseContext: responseContext)
promise.succeed(context)
}

return promise.futureResult
}

private func executeAll(forRequest request: MockNIOHTTPRequest, middleware: [Middleware]) -> MiddlewareContext? {
private func executeAll(forRequest request: MockNIOHTTPRequest, middleware: [Middleware], responseContext: MiddlewareResponseContext) -> MiddlewareContext? {

let requestContext = _MiddlewareRequestContext(request: request)
let responseContext = _MiddlewareResponseContext()

let context = _MiddlewareContext(requestContext: requestContext,
responseContext: responseContext,
notFoundHandler: notFoundHandler) {
if (middleware.count - 1) > 0 {
self.executeAll(forRequest: request, middleware: Array(middleware[1...]))
self.executeAll(forRequest: request, middleware: Array(middleware[1...]), responseContext: responseContext)
}
}

Expand All @@ -99,8 +109,4 @@ class MiddlewareService {
return context
}

func add(middleware mdl: Middleware) {
middleware.append(mdl)
}

}
2 changes: 1 addition & 1 deletion Shock/Classes/Middleware/MockRoutesMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

class MockRoutesMiddleware: Middleware {
struct MockRoutesMiddleware: Middleware {

let router: MockNIOHTTPRouter

Expand Down
Loading

0 comments on commit 2ce5878

Please sign in to comment.