Revision control

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