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

Fix failing tests #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let package = Package(
),
.testTarget(
name: "MockingjayTests",
dependencies: [ "Mockingjay" ],
dependencies: [ "Mockingjay", "MockingjayXCTest" ],
path: "Tests/MockingjayTests"
)
],
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/MockingjayProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public func ==(lhs:Stub, rhs:Stub) -> Bool {
}

var stubs = [Stub]()
var registered: Bool = false
public var registered: Bool = false

public class MockingjayProtocol: URLProtocol {
// MARK: Stubs
Expand Down
1 change: 1 addition & 0 deletions Sources/XCTest/XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#if canImport(XCTest)
import ObjectiveC
import XCTest
import Mockingjay

let swizzleTearDown: Void = {
let tearDown = class_getInstanceMethod(XCTest.self, #selector(XCTest.tearDown))
Expand Down
66 changes: 66 additions & 0 deletions Tests/MockingjayTests/MockingjayAsyncProtocolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,72 @@ class MockingjayAsyncProtocolTests: XCTestCase, URLSessionDataDelegate {
}
XCTAssertEqual(mutableData as Data, stubData)
}

func testDownloadOfAudioFileInChunks() {
let request = URLRequest(url: URL(string: "https://fuller.li/")!)

let thisSourceFile = URL(fileURLWithPath: #file)
let thisDirectory = thisSourceFile.deletingLastPathComponent()
let resourceURL = thisDirectory.appendingPathComponent("TestAudio.m4a")
let data = try! Data(contentsOf: resourceURL)

let stubResponse = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: "1.1", headerFields: ["Content-Length" : String(data.count)])!

MockingjayProtocol.addStub(matcher: { (requestedRequest) -> (Bool) in
return true
}) { (request) -> (Response) in
return Response.success(stubResponse, Download.streamContent(data: data, inChunksOf: 2000))
}
let urlSession = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.current)
let dataTask = urlSession.dataTask(with: request)
dataTask.resume()

let mutableData = NSMutableData()
while mutableData.length < data.count {
let expectation = self.expectation(description: "testProtocolCanReturnedDataInChunks")
self.didReceiveDataHandler = { (session: Foundation.URLSession, dataTask: URLSessionDataTask, data: Data) in
mutableData.append(data)
expectation.fulfill()
}
waitForExpectations(timeout: 2.0, handler: nil)
}
XCTAssertEqual(mutableData as Data, data)
}

func testByteRanges() {
let length = 100000
var request = URLRequest(url: URL(string: "https://fuller.li/")!)
request.addValue("bytes=50000-149999", forHTTPHeaderField: "Range")

let thisSourceFile = URL(fileURLWithPath: #file)
let thisDirectory = thisSourceFile.deletingLastPathComponent()
let resourceURL = thisDirectory.appendingPathComponent("TestAudio.m4a")
let data = try! Data(contentsOf: resourceURL)

let stubResponse = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: "1.1", headerFields: ["Content-Length" : String(length)])!
MockingjayProtocol.addStub(matcher: { (requestedRequest) -> (Bool) in
return true
}) { (request) -> (Response) in
return Response.success(stubResponse, .streamContent(data: data, inChunksOf: 2000))
}

let urlSession = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.current)
let dataTask = urlSession.dataTask(with: request)
dataTask.resume()

var mutableData = Data()
while mutableData.count < length {
let expectation = self.expectation(description: "testProtocolCanReturnedDataInChunks")
self.didReceiveDataHandler = { (session: Foundation.URLSession, dataTask: URLSessionDataTask, data: Data) in
mutableData.append(data)
expectation.fulfill()
}
waitForExpectations(timeout: 2.0, handler: nil)
}

let correctData = data.subdata(in: 50000 ..< (50000 + length))
XCTAssertEqual(mutableData, correctData)
}

// MARK: NSURLSessionDataDelegate
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
Expand Down
4 changes: 2 additions & 2 deletions Tests/MockingjayTests/MockingjayProtocolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import XCTest

class MockingjayProtocolTests : XCTestCase {

var urlSession:URLSession!
lazy var urlSession = URLSession(configuration: URLSessionConfiguration.default)

override func setUp() {
super.setUp()
urlSession = URLSession(configuration: URLSessionConfiguration.default)
}

override func tearDown() {
Expand Down Expand Up @@ -73,6 +72,7 @@ class MockingjayProtocolTests : XCTestCase {
XCTAssertNil(response)
XCTAssertNil(data)
XCTAssertNotNil(error)
XCTAssertEqual((error as NSError?)?.domain, "MockingjayTests")
}

func testProtocolReturnsResponseWithRegisteredStubError() {
Expand Down
15 changes: 15 additions & 0 deletions Tests/MockingjayTests/MockingjayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class MockingjaySessionTests: XCTestCase {
}

func testEphemeralSessionConfigurationIncludesProtocol() {
ensureMockingjayProtocolRegistration()
let configuration = URLSessionConfiguration.ephemeral
let protocolClasses = (configuration.protocolClasses!).map(toString)
XCTAssertEqual(protocolClasses.first!, "MockingjayProtocol")
}

func testDefaultSessionConfigurationIncludesProtocol() {
ensureMockingjayProtocolRegistration()
let configuration = URLSessionConfiguration.default
let protocolClasses = (configuration.protocolClasses!).map(toString)
XCTAssertEqual(protocolClasses.first!, "MockingjayProtocol")
Expand All @@ -52,4 +54,17 @@ class MockingjaySessionTests: XCTestCase {
XCTAssertNil(error, String(describing: error))
}
}

/// Ensures that `MockingjayProtocol` is added to `URLProtocol` registered classes.
///
/// The registration process happens while stubbing, so for test cases, where `stub(_:_:)` is not called
/// `MockingjayProtocol` will not be registered. Call this method within those test cases
/// if your test relies on `MockingjayProtocol` being registered to pass.
func ensureMockingjayProtocolRegistration() {
if !registered {
URLProtocol.registerClass(MockingjayProtocol.self)
URLSessionConfiguration.mockingjaySwizzleDefaultSessionConfiguration()
registered = true
}
}
}
1 change: 1 addition & 0 deletions Tests/MockingjayTests/MockingjayXCTestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
import MockingjayXCTest
@testable import Mockingjay


Expand Down
Binary file added Tests/MockingjayTests/TestAudio.m4a
Binary file not shown.