Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactor #6

Merged
merged 11 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Example/Tests/Succulent/Tests_testIgnoredParametersForTrace.trace
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP-Trace-Version: 1.0
Generator: Succulent/1.0


Method: GET
Protocol-Version: HTTP/1.1
Protocol: http
Host: www.cactuslab.com
File: /query.txt?username=test&toBe=ignored
Response-Header:<<--EOF-2-
HTTP/1.1 200 OK
Content-Type: text/plain

--EOF-2-
Response-Body:<<--EOF-2-
Success for query
--EOF-2-
36 changes: 25 additions & 11 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ class Tests: XCTestCase, SucculentTest {
override func setUp() {
super.setUp()

configureSucculent()

session = URLSession(configuration: .default)
}

func configureSucculent(ignoredParams: Set<String>? = nil, passThroughBaseUrl: URL? = nil) {
if let suc = suc {
suc.stop()
}
let conf = Configuration(port: nil, ignoreParameters: ignoredParams, ignoreVersioningRequests: ["^/ignore_post.txt"])
if let traceURL = self.traceUrl {
suc = Succulent(traceUrl: traceURL, baseUrl: nil, ignoreVersioningRequests: ["^/ignore_post.txt"])
suc = Succulent(replayFrom: traceURL, passThroughBaseUrl: passThroughBaseUrl, configuration: conf)
} else {
suc = Succulent()
suc = Succulent(passThroughBaseUrl: passThroughBaseUrl, configuration: conf)
}

suc.start()

baseURL = URL(string: "http://localhost:\(suc.actualPort)")

session = URLSession(configuration: .default)
self.baseURL = URL(string: "http://localhost:\(suc.actualPort)")
}

/// The name of the trace file for the current test
Expand Down Expand Up @@ -70,7 +77,7 @@ class Tests: XCTestCase, SucculentTest {
}

func testIgnoredParameters() {
suc.ignoreParameters = ["ignoreMe"]
configureSucculent(ignoredParams: ["ignoreMe"])

GET("query.txt?username=test&ignoreMe=1209") { (data, response, error) in
XCTAssertEqual(String(data: data!, encoding: .utf8)!, "Success for query")
Expand All @@ -82,8 +89,15 @@ class Tests: XCTestCase, SucculentTest {
}
}

func testIgnoredParametersForTrace() {
karlvr marked this conversation as resolved.
Show resolved Hide resolved
configureSucculent(ignoredParams: ["toBe"])
GET("query.txt?username=test&toBe=ignored") { (data, response, error) in
XCTAssertEqual(String(data: data!, encoding: .utf8)!, "Success for query")
}
}

func testIgnoreAllParameters() {
suc.ignoreParameters = ["ignore_me"]
configureSucculent(ignoredParams: ["ignore_me"])

GET("query.txt?ignore_me=12345") { (data, response, error) in
XCTAssertEqual(String(data: data!, encoding: .utf8)!, "Success for query")
Expand Down Expand Up @@ -192,7 +206,7 @@ class Tests: XCTestCase, SucculentTest {
}

func testPassThrough() {
suc.baseUrl = URL(string: "http://www.cactuslab.com/")
configureSucculent(ignoredParams: nil, passThroughBaseUrl: URL(string: "http://www.cactuslab.com/"))

GET("index.html") { (data, response, error) in
let string = String(data: data!, encoding: .utf8)!
Expand All @@ -201,7 +215,7 @@ class Tests: XCTestCase, SucculentTest {
}

func testPassThroughURLPreservation() {
suc.baseUrl = URL(string: "http://www.cactuslab.com/api/")
configureSucculent(ignoredParams: nil, passThroughBaseUrl: URL(string: "http://www.cactuslab.com/api/"))

GET("index.html") { (data, response, error) in
XCTAssertTrue(response?.url?.absoluteString == "http://cactuslab.com/api/index.html", "The responseURL was \(response?.url?.absoluteString ?? "nil")")
Expand Down Expand Up @@ -264,7 +278,7 @@ class Tests: XCTestCase, SucculentTest {
}

func testQueryStringOrder() {
suc.ignoreParameters = ["a"]
configureSucculent(ignoredParams: ["a"])

GET("query.txt?username=test&perPage=2&a=1") { (data, response, error) in
XCTAssertEqual(String(data: data!, encoding: .utf8)!, "Success for query")
Expand Down
2 changes: 1 addition & 1 deletion Example/Tests/TraceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TraceTests: XCTestCase, SucculentTest {

recordingURL = self.recordUrl

suc = Succulent(recordUrl: recordingURL, baseUrl: URL(string: "http://cactuslab.com/")!)
suc = Succulent(recordTo: recordingURL, baseUrl: URL(string: "http://cactuslab.com/")!)
suc.start()

baseURL = URL(string: "http://localhost:\(suc.actualPort)")
Expand Down
65 changes: 41 additions & 24 deletions Succulent/Classes/Succulent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,36 @@
import Embassy
import Foundation

public struct Configuration {
public var port: Int?
karlvr marked this conversation as resolved.
Show resolved Hide resolved
public var ignoreParameters: Set<String>?
karlvr marked this conversation as resolved.
Show resolved Hide resolved
public var ignoreVersioningRequests: [String]?
karlvr marked this conversation as resolved.
Show resolved Hide resolved

public init() {

}

public init(port: Int? = nil, ignoreParameters: Set<String>? = nil, ignoreVersioningRequests: [String]?) {
self.port = port
self.ignoreParameters = ignoreParameters
self.ignoreVersioningRequests = ignoreVersioningRequests
}
}

public class Succulent : NSObject, URLSessionTaskDelegate {

public var port: Int?
public var version = 0
public var baseUrl: URL?
public var recordUrl: URL? {
public private(set) var port: Int?
public private(set) var version = 0
karlvr marked this conversation as resolved.
Show resolved Hide resolved
public private(set) var baseUrl: URL?
public private(set) var recordUrl: URL? {
didSet {
if let recordUrl = recordUrl {
//Throw away the previous trace
try? FileManager.default.removeItem(at: recordUrl)
}
}
}
public var ignoreParameters: Set<String>?
public private(set) var ignoreParameters: Set<String>?

public let router = Router()

Expand All @@ -48,27 +64,19 @@ public class Succulent : NSObject, URLSessionTaskDelegate {
private var currentTrace = NSMutableOrderedSet()
private var recordedKeys = Set<String>()
private var ignoreExpressions: [NSRegularExpression] = []

public override init() {
super.init()

createDefaultRouter()
}

public convenience init(traceUrl: URL, baseUrl: URL? = nil, ignoreVersioningRequests: [String] = []) {
self.init()
public convenience init(replayFrom traceUrl: URL? = nil, passThroughBaseUrl baseUrl: URL? = nil, configuration: Configuration? = nil) {
karlvr marked this conversation as resolved.
Show resolved Hide resolved

self.init(configuration: configuration)
self.baseUrl = baseUrl
ignoreExpressions = ignoreVersioningRequests.map { (expression) -> NSRegularExpression in
return try! NSRegularExpression(pattern: expression, options: [])

if let traceUrl = traceUrl {
addTrace(url: traceUrl)
}

addTrace(url: traceUrl, ignoreVersioningRequests: ignoreVersioningRequests)
}

public convenience init(recordUrl: URL, baseUrl: URL) {
self.init()
public convenience init(recordTo recordUrl: URL, baseUrl: URL, configuration: Configuration? = nil) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation:

Initialise Succulent in recording mode, with a URL to record the trace to and the base URL for the upstream server that we're recording.

self.init(configuration: configuration)

defer {
/* Defer so that the didSet runs on recordUrl */
Expand All @@ -77,6 +85,18 @@ public class Succulent : NSObject, URLSessionTaskDelegate {
}
}

private init(configuration: Configuration?) {
super.init()
if let configuration = configuration, let ignoreVersioningRequests = configuration.ignoreVersioningRequests {
karlvr marked this conversation as resolved.
Show resolved Hide resolved
ignoreExpressions = ignoreVersioningRequests.map { (expression) -> NSRegularExpression in
return try! NSRegularExpression(pattern: expression, options: [])
}
ignoreParameters = configuration.ignoreParameters
port = configuration.port
}
createDefaultRouter()
}

private func createDefaultRouter() {
router.add(".*").anyParams().block { (req, resultBlock) in
/* Increment version when we get the first GET after a mutating http method */
Expand Down Expand Up @@ -183,7 +203,7 @@ public class Succulent : NSObject, URLSessionTaskDelegate {
}

/// Load the trace file at the given URL and populate our traces ivar
private func addTrace(url: URL, ignoreVersioningRequests: [String]) {
private func addTrace(url: URL) {
if traces == nil {
traces = [String : Trace]()
}
Expand Down Expand Up @@ -333,9 +353,6 @@ public class Succulent : NSObject, URLSessionTaskDelegate {
self.loop.call {
switch result {
case .response(let res):
if res.containsHeader("Set-Cookie") {
print("Here we are with cooookies")
}
startResponse("\(res.status)", res.headers ?? [])

if let data = res.data {
Expand Down