Skip to content

Commit

Permalink
Add configuration to allow recording empty tracks (#88)
Browse files Browse the repository at this point in the history
* add configuration to allow recording empty tracks

* always write into file without checking for recorded tracks

* fixed tests

* adjust expectation

* do not enable recording when recording path is nil
  • Loading branch information
ilyapuchka authored and mluisbrown committed Oct 31, 2018
1 parent 89e1d1b commit 30dafc0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
15 changes: 4 additions & 11 deletions Vinyl/Recorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import Foundation
final class Recorder {
var wax: Wax
let recordingPath: String?
var somethingRecorded = false


init(wax: Wax, recordingPath: String?) {
self.wax = wax
self.recordingPath = recordingPath
Expand All @@ -22,7 +21,6 @@ final class Recorder {
extension Recorder {
func saveTrack(with request: Request, response: Response) {
wax.add(track: Track(request: request, response: response))
somethingRecorded = true
}

func saveTrack(with request: Request, urlResponse: HTTPURLResponse?, body: Data? = nil, error: Error? = nil) {
Expand All @@ -34,14 +32,10 @@ extension Recorder {
extension Recorder {

func persist() throws {
guard let recordingPath = recordingPath, somethingRecorded else {
if somethingRecorded {
throw TurntableError.noRecordingPath
} else {
throw TurntableError.nothingToRecord
}
guard let recordingPath = recordingPath else {
throw TurntableError.noRecordingPath
}

let fileManager = FileManager.default
guard fileManager.createFile(atPath: recordingPath, contents: nil, attributes: nil) == true,
let file = FileHandle(forWritingAtPath: recordingPath) else {
Expand All @@ -57,6 +51,5 @@ extension Recorder {
file.synchronizeFile()

print("Vinyl recorded to: \(recordingPath)")
somethingRecorded = false
}
}
8 changes: 2 additions & 6 deletions Vinyl/Turntable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ enum TurntableError: Error {

case trackNotFound
case noRecordingPath
case nothingToRecord
}

public typealias Plastic = [[String: Any]]
Expand All @@ -39,9 +38,9 @@ public final class Turntable: URLSession {

if configuration.recodingEnabled {
recorder = Recorder(wax: Wax(tracks: []), recordingPath: configuration.recordingPath)
recordingSession = urlSession ?? URLSession.shared
}

recordingSession = urlSession ?? URLSession.shared

super.init()
}

Expand Down Expand Up @@ -81,9 +80,6 @@ public final class Turntable: URLSession {
do {
try recorder.persist()
}
catch TurntableError.nothingToRecord {
print("Nothing to record.")
}
catch TurntableError.noRecordingPath {
fatalError("💣 no path was configured for saving the recording.")
}
Expand Down
13 changes: 8 additions & 5 deletions Vinyl/TurntableConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct TurntableConfiguration {

public let matchingStrategy: MatchingStrategy
public let recordingMode: RecordingMode

var playTracksUniquely: Bool {
get {
switch matchingStrategy {
Expand All @@ -36,10 +36,10 @@ public struct TurntableConfiguration {
var recodingEnabled: Bool {
get {
switch recordingMode {
case .none:
return false
case let .missingTracks(recordingPath), let .missingVinyl(recordingPath):
return recordingPath != nil
default:
return true
return false
}
}
}
Expand All @@ -57,7 +57,10 @@ public struct TurntableConfiguration {
}
}

public init(matchingStrategy: MatchingStrategy = .requestAttributes(types: [.method, .url], playTracksUniquely: true), recordingMode: RecordingMode = .missingVinyl(recordingPath: nil)) {
public init(
matchingStrategy: MatchingStrategy = .requestAttributes(types: [.method, .url], playTracksUniquely: true),
recordingMode: RecordingMode = .missingVinyl(recordingPath: nil)
) {
self.matchingStrategy = matchingStrategy
self.recordingMode = recordingMode
}
Expand Down
65 changes: 41 additions & 24 deletions VinylTests/TurntableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ class TurntableTests: XCTestCase {

XCTAssertEqual(httpResponse.url!.absoluteString, urlString)
XCTAssertEqual(httpResponse.statusCode, 200)
XCTAssertTrue(data! == body)
XCTAssertTrue(data == body)
XCTAssertNotNil(httpResponse.allHeaderFields)

expectation.fulfill()
}) .resume()
}).resume()
}

fileprivate func multipleCallTest(_ turntable: Turntable) {
Expand Down Expand Up @@ -358,7 +358,7 @@ class TurntableTests: XCTestCase {

XCTAssertFalse(Thread.isMainThread)
expectation.fulfill()
}) .resume()
}).resume()
}

func test_Vinyl_mainQueue() {
Expand All @@ -374,59 +374,59 @@ class TurntableTests: XCTestCase {

XCTAssertTrue(Thread.isMainThread)
expectation.fulfill()
}) .resume()
}).resume()
}

func test_Vinyl_Delegate() {

let turntable = Turntable(vinylName: "vinyl_single", delegateQueue: OperationQueue.main)
XCTAssertNil(turntable.delegate)
}

func test_Vinyl_recording_missingVinyl_vinylMissing() {
let expectation = self.expectation(description: "Expected callback to be called on background thread")
defer { self.waitForExpectations(timeout: 4, handler: nil) }

let recordingVinylName = "vinyl_recording"
let path = prepPathForRecording(recordingVinylName)

let dogFood = Turntable(vinylName: "vinyl_single")
let turntable = Turntable(vinylName: recordingVinylName,
turntableConfiguration: TurntableConfiguration(recordingMode: .missingVinyl(recordingPath: nil)),
urlSession: dogFood)

let urlString = "http://api.test.com"

turntable.dataTask(with: URL(string: urlString)!, completionHandler: { (data, response, anError) in
turntable.stopRecording()

XCTAssertTrue(FileManager.default.fileExists(atPath: path))

expectation.fulfill()
}) .resume()
}).resume()
}

func test_Vinyl_recording_missingVinyl_vinylPresent() {
let expectation = self.expectation(description: "Expected callback to be called on background thread")
defer { self.waitForExpectations(timeout: 4, handler: nil) }

let recordingVinylName = "vinyl_recording"
let path = prepPathForRecording(recordingVinylName)

let dogFood = Turntable(vinylName: "vinyl_single")
let turntable = Turntable(vinylName: "vinyl_single",
turntableConfiguration: TurntableConfiguration(recordingMode: .missingVinyl(recordingPath: nil)),
urlSession: dogFood)

let urlString = "http://api.test.com"

turntable.dataTask(with: URL(string: urlString)!, completionHandler: { (data, response, anError) in
turntable.stopRecording()

XCTAssertFalse(FileManager.default.fileExists(atPath: path))

expectation.fulfill()
}) .resume()
}).resume()
}

func test_Vinyl_recording_missingTracks_missingTrack() {
Expand All @@ -449,7 +449,7 @@ class TurntableTests: XCTestCase {
XCTAssertTrue(FileManager.default.fileExists(atPath: path))

expectation.fulfill()
}) .resume()
}).resume()
}

func test_Vinyl_recording_missingTracks_existingTrack() {
Expand All @@ -469,10 +469,24 @@ class TurntableTests: XCTestCase {
turntable.dataTask(with: URL(string: urlString)!, completionHandler: { (data, response, anError) in
turntable.stopRecording()

XCTAssertFalse(FileManager.default.fileExists(atPath: path))
XCTAssertTrue(FileManager.default.fileExists(atPath: path))

expectation.fulfill()
}) .resume()
}).resume()
}

func test_Vynil_recording_empty() {
let recordingVinylName = "vinyl_recording"
let path = prepPathForRecording(recordingVinylName)

let dogFood = Turntable(vinylName: "vinyl_single")
let turntable = Turntable(vinylName: "vinyl_single",
turntableConfiguration: TurntableConfiguration(recordingMode: .missingTracks(recordingPath: path)),
urlSession: dogFood)

turntable.stopRecording()

XCTAssertTrue(FileManager.default.fileExists(atPath: path))
}

func test_default_URLSession_configuration() {
Expand All @@ -487,12 +501,15 @@ class TurntableTests: XCTestCase {
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = ["X-UniqueHeader": expectedHeaderValue]

let recordingVinylName = "vinyl_recording"
let path = prepPathForRecording(recordingVinylName)

let urlSession = URLSession(configuration: configuration)
let turntable = Turntable(vinylName: "vinyl_single",
turntableConfiguration: TurntableConfiguration(recordingMode: .missingTracks(recordingPath: nil)),
turntableConfiguration: TurntableConfiguration(recordingMode: .missingTracks(recordingPath: path)),
urlSession: urlSession)

let headerValue = turntable.configuration.httpAdditionalHeaders!["X-UniqueHeader"] as! String
let headerValue = turntable.configuration.httpAdditionalHeaders?["X-UniqueHeader"] as? String

// Can't compare `configuration` instances since they return copies
XCTAssertEqual(headerValue, expectedHeaderValue)
Expand Down

0 comments on commit 30dafc0

Please sign in to comment.