Skip to content

Instantly share code, notes, and snippets.

@younata
Created February 22, 2016 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save younata/332fe5f7ae055b557738 to your computer and use it in GitHub Desktop.
Save younata/332fe5f7ae055b557738 to your computer and use it in GitHub Desktop.
What Carthage/NetworkSpec.swift could have been
// I spent 2 hours working on this before I gave up. This is what I had when I declared it not worth the effort.
import Quick
import Nimble
import ReactiveCocoa
import CarthageKit
import OHHTTPStubs
class NetworkSpec: QuickSpec {
override func spec() {
describe("createURLRequest") {
let url = NSURL(string: "https://example.com")!
it("creates an NSURLRequest with the given url") {
let urlRequest = createURLRequest(url)
expect(urlRequest.URL) == url
expect(urlRequest.allHTTPHeaderFields) == [:]
}
it("uses the given headers if specified") {
let urlRequest = createURLRequest(url, ["exampleHeader": "exampleValue"])
expect(urlRequest.allHTTPHeaderFields) == ["exampleHeader": "exampleValue"]
}
}
describe("Making HTTP Requests") {
var receivedRequest: NSURLRequest?
func makeRequest(request: NSURLRequest, returning response: OHHTTPStubsResponse, request requestBlock: NSURLRequest -> Void) {
let expectation = self.expectationWithDescription("Receiving request")
stub(isHost(request.URL!.host!) && isPath(request.URL!.path!)) { request in
receivedRequest = request
expectation.fulfill()
return response
}
requestBlock(request)
self.waitForExpectationsWithTimeout(1) { _ in }
}
beforeEach {
OHHTTPStubs.removeAllStubs()
receivedRequest = nil
}
fdescribe("executeDataRequest") {
var producer: SignalProducer<NSData, CarthageError>!
var events: [Event<NSData, CarthageError>] = []
let requestBlock: NSURLRequest -> Void = {
events = []
let urlSession = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
producer = executeDataRequest($0, URLSession: urlSession)
producer.on(event: { event in
events.append(event)
}).start()
}
it("makes a data request to an NSURLSession") {
let urlRequest = createURLRequest(NSURL(string: "https://example.com")!)
makeRequest(urlRequest, returning: OHHTTPStubsResponse(JSONObject: [], statusCode: 200, headers: nil), request: requestBlock)
expect(receivedRequest) == urlRequest
}
context("when the download succeeds") {
it("returns the data for the file downloaded") {
let data = "Example response".dataUsingEncoding(NSUTF8StringEncoding)!
let response = OHHTTPStubsResponse(data: data, statusCode: 200, headers: nil)
let urlRequest = createURLRequest(NSURL(string: "https://example.com/download")!)
makeRequest(urlRequest, returning: response, request: requestBlock)
expect(events.count) == 2
expect(events[0].error).to(beNil())
expect(events[0].value) == data
expect(events[1] == Event.Completed) == true
}
}
context("when the download fails") {
it("returns a network error") {
let error = NSError(domain: "Some domain", code: 666, userInfo: nil)
let response = OHHTTPStubsResponse(error: error)
let urlRequest = createURLRequest(NSURL(string: "https://example.com/error")!)
makeRequest(urlRequest, returning: response, request: requestBlock)
expect(events.count) == 1
expect(events[0].value).to(beNil())
expect(events[0].error) == CarthageError.NetworkError(error)
}
}
}
xdescribe("executeDownloadRequest") {
var producer: SignalProducer<NSURL, CarthageError>!
var events: [Event<NSURL, CarthageError>] = []
let urlRequest = createURLRequest(NSURL(string: "https://example.com")!)
beforeEach {
events = []
producer = executeDownloadRequest(urlRequest)
producer.on(event: { event in
events.append(event)
}).start()
}
it("makes a download request to an NSURLSession") {
fail("Mock out the network so I can be tested!")
}
context("when the download succeeds") {
it("returns a local URL for the file downloaded") {
expect(events.count) == 2
expect(events[0].error).to(beNil())
expect(events[0].value?.fileURL) == true
expect(events[1] == Event.Completed) == true
}
}
context("when the download fails") {
it("returns a network error") {
let error = NSError(domain: "Some domain", code: 666, userInfo: nil)
expect(events.count) == 1
expect(events[0].value).to(beNil())
expect(events[0].error) == CarthageError.NetworkError(error)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment