-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathPocketFeedTests.swift
67 lines (54 loc) · 2.52 KB
/
PocketFeedTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import UIKit
import GCDWebServers
import XCTest
@testable import Client
class PocketStoriesTests: XCTestCase {
var pocketAPI: String!
let webServer: GCDWebServer = GCDWebServer()
/// Setup a basic web server that binds to a random port and that has one default handler on /hello
fileprivate func setupWebServer() {
let path = Bundle(for: type(of: self)).path(forResource: "pocketglobalfeed", ofType: "json")
let data = try! Data(contentsOf: URL(fileURLWithPath: path!))
webServer.addHandler(forMethod: "GET", path: "/pocketglobalfeed", request: GCDWebServerRequest.self) { (request) -> GCDWebServerResponse in
return GCDWebServerDataResponse(data: data, contentType: "application/json")
}
if webServer.start(withPort: 0, bonjourName: nil) == false {
XCTFail("Can't start the GCDWebServer")
}
pocketAPI = "http://localhost:\(webServer.port)/pocketglobalfeed"
}
override func setUp() {
super.setUp()
setupWebServer()
}
override func tearDown() {
super.tearDown()
}
func testPocketStoriesCaching() {
let expect = expectation(description: "Pocket")
let PocketFeed = Pocket(endPoint: pocketAPI)
PocketFeed.globalFeed(items: 4).upon { result in
let items = result
XCTAssertEqual(items.count, 2, "We are fetching a static feed. There are only 2 items in it")
self.webServer.stop() // Stop the webserver so we can check caching
// Try again now that the webserver is down
PocketFeed.globalFeed(items: 4).upon { result in
let items = result
XCTAssertEqual(items.count, 2, "We are fetching a static feed. There are only 2 items in it")
let item = items.first
//These are all not optional so they should never be nil.
//But lets check in case someone decides to change something
XCTAssertNotNil(item?.domain, "Why")
XCTAssertNotNil(item?.imageURL, "You")
XCTAssertNotNil(item?.storyDescription, "Do")
XCTAssertNotNil(item?.title, "This")
XCTAssertNotNil(item?.url, "?")
expect.fulfill()
}
}
waitForExpectations(timeout: 10, handler: nil)
}
}