forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchTests.swift
113 lines (91 loc) · 4.37 KB
/
SearchTests.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* 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 Foundation
import UIKit
import XCTest
class SearchTests: XCTestCase {
private let uriFixup = URIFixup()
func testParsing() {
let parser = OpenSearchParser(pluginMode: true)
let file = NSBundle.mainBundle().pathForResource("google", ofType: "xml", inDirectory: "SearchPlugins/en")
let engine: OpenSearchEngine! = parser.parse(file!)
XCTAssertEqual(engine.shortName, "Google")
XCTAssertNil(engine.description)
// Test regular search queries.
XCTAssertEqual(engine.searchURLForQuery("foobar")!.absoluteString, "https://www.google.com/search?q=foobar&ie=utf-8&oe=utf-8")
// Test search suggestion queries.
XCTAssertEqual(engine.suggestURLForQuery("foobar")!.absoluteString, "https://www.google.com/complete/search?client=firefox&q=foobar")
}
func testURIFixup() {
// Check valid URLs. We can load these after some fixup.
checkValidURL("http://www.mozilla.org", afterFixup: "http://www.mozilla.org")
checkValidURL("about:", afterFixup: "about:")
checkValidURL("about:config", afterFixup: "about:config")
checkValidURL("file:///f/o/o", afterFixup: "file:///f/o/o")
checkValidURL("ftp://ftp.mozilla.org", afterFixup: "ftp://ftp.mozilla.org")
checkValidURL("foo.bar", afterFixup: "http://foo.bar")
checkValidURL(" foo.bar ", afterFixup: "http://foo.bar")
checkValidURL("1.2.3", afterFixup: "http://1.2.3")
// Check invalid URLs. These are passed along to the default search engine.
checkInvalidURL("foobar")
checkInvalidURL("foo bar")
checkInvalidURL("mozilla. org")
checkInvalidURL("about: config")
checkInvalidURL("123")
checkInvalidURL("a/b")
}
private func checkValidURL(beforeFixup: String, afterFixup: String) {
XCTAssertEqual(uriFixup.getURL(beforeFixup)!.absoluteString, afterFixup)
}
private func checkInvalidURL(beforeFixup: String) {
XCTAssertNil(uriFixup.getURL(beforeFixup))
}
func testSuggestClient() {
let webServerBase = startMockSuggestServer()
let engine = OpenSearchEngine(shortName: "Mock engine", description: nil, image: nil, searchTemplate: "", suggestTemplate: "\(webServerBase)?q={searchTerms}")
let client = SearchSuggestClient(searchEngine: engine)
let query1 = self.expectationWithDescription("foo query")
client.query("foo", callback: { response, error in
if error != nil {
XCTFail("Error: \(error?.description)")
}
XCTAssertEqual(response![0], "foo")
XCTAssertEqual(response![1], "foo2")
XCTAssertEqual(response![2], "foo you")
query1.fulfill()
})
waitForExpectationsWithTimeout(10, handler: nil)
let query2 = self.expectationWithDescription("foo bar query")
client.query("foo bar", callback: { response, error in
if error != nil {
XCTFail("Error: \(error?.description)")
}
XCTAssertEqual(response![0], "foo bar soap")
XCTAssertEqual(response![1], "foo barstool")
XCTAssertEqual(response![2], "foo bartender")
query2.fulfill()
})
waitForExpectationsWithTimeout(10, handler: nil)
}
private func startMockSuggestServer() -> String {
let webServer: GCDWebServer = GCDWebServer()
webServer.addHandlerForMethod("GET", path: "/", requestClass: GCDWebServerRequest.self) { (request) -> GCDWebServerResponse! in
var suggestions: [String]!
let query = request.query["q"] as! String
switch query {
case "foo":
suggestions = ["foo", "foo2", "foo you"]
case "foo bar":
suggestions = ["foo bar soap", "foo barstool", "foo bartender"]
default:
XCTFail("Unexpected query: \(query)")
}
return GCDWebServerDataResponse(JSONObject: [query, suggestions])
}
if !webServer.startWithPort(0, bonjourName: nil) {
XCTFail("Can't start the GCDWebServer")
}
return "http://localhost:\(webServer.port)"
}
}