Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit f8fad69

Browse files
authored
Bug 1521485 - Random port for running tests (#4489)
* Bug 1521485 - Random port for running tests * speed up login test
1 parent e5c1d69 commit f8fad69

17 files changed

+62
-48
lines changed

Client/Application/TestAppDelegate.swift

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ class TestAppDelegate: AppDelegate {
2222
let launchArguments = ProcessInfo.processInfo.arguments
2323

2424
launchArguments.forEach { arg in
25+
if arg.starts(with: LaunchArguments.ServerPort) {
26+
let portString = arg.replacingOccurrences(of: LaunchArguments.ServerPort, with: "")
27+
if let port = Int(portString) {
28+
AppInfo.webserverPort = port
29+
} else {
30+
fatalError("Failed to set web server port override.")
31+
}
32+
}
33+
2534
if arg.starts(with: LaunchArguments.LoadDatabasePrefix) {
2635
if launchArguments.contains(LaunchArguments.ClearProfile) {
2736
fatalError("Clearing profile and loading a test database is not a supported combination.")

Client/Application/WebServer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class WebServer {
3636
@discardableResult func start() throws -> Bool {
3737
if !server.isRunning {
3838
try server.start(options: [
39-
GCDWebServerOption_Port: 6571,
39+
GCDWebServerOption_Port: AppInfo.webserverPort,
4040
GCDWebServerOption_BindToLocalhost: true,
4141
GCDWebServerOption_AutomaticallySuspendInBackground: true,
4242
GCDWebServerOption_AuthenticationMethod: GCDWebServerAuthenticationMethod_Basic,

Client/Frontend/Browser/SessionData.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Shared
99
private func migrate(urls: [URL]) -> [URL] {
1010
return urls.compactMap { url in
1111
var url = url
12-
[("http://localhost:6571/errors/error.html?url=", "\(InternalURL.baseUrl)/\(SessionRestoreHandler.path)?url=")
12+
let port = AppInfo.webserverPort
13+
[("http://localhost:\(port)/errors/error.html?url=", "\(InternalURL.baseUrl)/\(SessionRestoreHandler.path)?url=")
1314
// TODO: handle reader pages ("http://localhost:6571/reader-mode/page?url=", "\(InternalScheme.url)/\(ReaderModeHandler.path)?url=")
1415
].forEach {
1516
oldItem, newItem in

ClientTests/ClientTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ClientTests: XCTestCase {
6060

6161
fileprivate func hostIsValid(_ host: String) -> Bool {
6262
let expectation = self.expectation(description: "Validate host for \(host)")
63-
let request = URLRequest(url: URL(string: "http://\(host):6571/about/license")!)
63+
let request = URLRequest(url: URL(string: "http://\(host):\(AppInfo.webserverPort)/about/license")!)
6464
var response: HTTPURLResponse?
6565
Alamofire.request(request).authenticate(usingCredential: WebServer.sharedInstance.credentials).response { (res) -> Void in
6666
response = res.response

ClientTests/UIImageViewExtensionsTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class UIImageViewExtensionsTests: XCTestCase {
4242
}
4343

4444
let favImageView = UIImageView()
45-
favImageView.setIcon(Favicon(url: "http://localhost:6571/favicon/icon"), forURL: URL(string: "http://localhost:6571"))
45+
favImageView.setIcon(Favicon(url: "http://localhost:\(AppInfo.webserverPort)/favicon/icon"), forURL: URL(string: "http://localhost:\(AppInfo.webserverPort)"))
4646

4747
let expect = expectation(description: "UIImageView async load")
4848
let time = Int64(2 * Double(NSEC_PER_SEC))

Shared/AppInfo.swift

+3
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@ open class AppInfo {
8383
public static var isApplication: Bool {
8484
return Bundle.main.object(forInfoDictionaryKey: "CFBundlePackageType") as! String == "APPL"
8585
}
86+
87+
// The port for the internal webserver, tests can change this
88+
public static var webserverPort = 6571
8689
}

Shared/Extensions/URLExtensions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public struct InternalURL {
366366
private let sessionRestoreHistoryItemBaseUrl = "\(InternalURL.baseUrl)/\(InternalURL.Path.sessionrestore.rawValue)?url="
367367

368368
public static func isValid(url: URL) -> Bool {
369-
let isWebServerUrl = url.absoluteString.hasPrefix("http://localhost:6571/")
369+
let isWebServerUrl = url.absoluteString.hasPrefix("http://localhost:\(AppInfo.webserverPort)/")
370370
if isWebServerUrl, url.path.hasPrefix("/test-fixture/") {
371371
// internal test pages need to be treated as external pages
372372
return false

Shared/LaunchArguments.swift

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public struct LaunchArguments {
1111
public static let ClearProfile = "FIREFOX_CLEAR_PROFILE"
1212
public static let StageServer = "FIREFOX_USE_STAGE_SERVER"
1313
public static let DeviceName = "DEVICE_NAME"
14+
public static let ServerPort = "GCDWEBSERVER_PORT:"
1415

1516
// After the colon, put the name of the file to load from test bundle
1617
public static let LoadDatabasePrefix = "FIREFOX_LOAD_DB_NAMED:"

SharedTests/NSURLExtensionsTests.swift

+28-28
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ class NSURLExtensionsTests: XCTestCase {
198198
]
199199
let badurls = [
200200
"http://google.com",
201-
"http://localhost:6571/sessionrestore.html",
202-
"http://localhost:6571/errors/error.html?url=http%3A//mozilla.com",
203-
"http://localhost:6571/errors/error.html?url=http%3A//mozilla.com/about/home/%23panel%3D1",
201+
"http://localhost:\(AppInfo.webserverPort)/sessionrestore.html",
202+
"http://localhost:\(AppInfo.webserverPort)/errors/error.html?url=http%3A//mozilla.com",
203+
"http://localhost:\(AppInfo.webserverPort)/errors/error.html?url=http%3A//mozilla.com/about/home/%23panel%3D1",
204204
]
205205

206206
checkUrls(goodurls: goodurls, badurls: badurls, checker: { url in
@@ -215,9 +215,9 @@ class NSURLExtensionsTests: XCTestCase {
215215
]
216216
let badurls = [
217217
"http://google.com",
218-
"http://localhost:6571/sessionrestore.html",
219-
"http://localhost:6571/errors/error.html?url=http%3A//mozilla.com",
220-
"http://localhost:6571/errors/error.html?url=http%3A//mozilla.com/about/home/%23panel%3D1",
218+
"http://localhost:\(AppInfo.webserverPort)/sessionrestore.html",
219+
"http://localhost:\(AppInfo.webserverPort)/errors/error.html?url=http%3A//mozilla.com",
220+
"http://localhost:\(AppInfo.webserverPort)/errors/error.html?url=http%3A//mozilla.com/about/home/%23panel%3D1",
221221
]
222222

223223
checkUrls(goodurls: goodurls, badurls: badurls, checker: { url in
@@ -231,7 +231,7 @@ class NSURLExtensionsTests: XCTestCase {
231231
]
232232
let badurls = [
233233
"http://google.com",
234-
"http://localhost:6571/sessionrestore.html",
234+
"http://localhost:\(AppInfo.webserverPort)/sessionrestore.html",
235235
"http://localhost:1234/about/home/#panel=0"
236236
]
237237

@@ -243,7 +243,7 @@ class NSURLExtensionsTests: XCTestCase {
243243
func testoriginalURLFromErrorURL() {
244244
let goodurls = [
245245
("\(InternalURL.baseUrl)/\(InternalURL.Path.errorpage)?url=http%3A//mozilla.com", URL(string: "http://mozilla.com")),
246-
("\(InternalURL.baseUrl)/\(InternalURL.Path.errorpage)?url=http%3A//localhost%3A6571/about/home/%23panel%3D1", URL(string: "http://localhost:6571/about/home/#panel=1")),
246+
("\(InternalURL.baseUrl)/\(InternalURL.Path.errorpage)?url=http%3A//localhost%3A\(AppInfo.webserverPort)/about/home/%23panel%3D1", URL(string: "http://localhost:\(AppInfo.webserverPort)/about/home/#panel=1")),
247247
]
248248

249249
goodurls.forEach {
@@ -255,12 +255,12 @@ class NSURLExtensionsTests: XCTestCase {
255255

256256
func testisReaderModeURL() {
257257
let goodurls = [
258-
"http://localhost:6571/reader-mode/page",
259-
"http://localhost:6571/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage",
258+
"http://localhost:\(AppInfo.webserverPort)/reader-mode/page",
259+
"http://localhost:\(AppInfo.webserverPort)/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage",
260260
]
261261
let badurls = [
262262
"http://google.com",
263-
"http://localhost:6571/sessionrestore.html",
263+
"http://localhost:\(AppInfo.webserverPort)/sessionrestore.html",
264264
"http://localhost:1234/about/home/#panel=0"
265265
]
266266

@@ -277,7 +277,7 @@ class NSURLExtensionsTests: XCTestCase {
277277
]
278278
let badurls = [
279279
"http://google.com",
280-
"http://localhost:6571/sessionrestore.html",
280+
"http://localhost:\(AppInfo.webserverPort)/sessionrestore.html",
281281
"about:reader",
282282
"http://about:reader?url=http://example.com"
283283
]
@@ -288,25 +288,25 @@ class NSURLExtensionsTests: XCTestCase {
288288

289289
func testdecodeReaderModeURL() {
290290
let goodurls = [
291-
("http://localhost:6571/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage&uuidkey=AAAAA", URL(string: "https://en.m.wikipedia.org/wiki/Main_Page")),
291+
("http://localhost:\(AppInfo.webserverPort)/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage&uuidkey=AAAAA", URL(string: "https://en.m.wikipedia.org/wiki/Main_Page")),
292292
("about:reader?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage&uuidkey=AAAAA", URL(string: "https://en.m.wikipedia.org/wiki/Main_Page")),
293293
("about:reader?url=http%3A%2F%2Fexample%2Ecom%3Furl%3Dparam%26key%3Dvalue&uuidkey=AAAAA", URL(string: "http://example.com?url=param&key=value"))
294294
]
295295
let badurls = [
296296
"http://google.com",
297-
"http://localhost:6571/sessionrestore.html",
297+
"http://localhost:\(AppInfo.webserverPort)/sessionrestore.html",
298298
"http://localhost:1234/about/home/#panel=0",
299-
"http://localhost:6571/reader-mode/page",
299+
"http://localhost:\(AppInfo.webserverPort)/reader-mode/page",
300300
"about:reader?url="
301301
]
302302

303303
goodurls.forEach { XCTAssertEqual(URL(string:$0.0)!.decodeReaderModeURL, $0.1) }
304304
badurls.forEach { XCTAssertNil(URL(string:$0)!.decodeReaderModeURL, $0) } }
305305

306306
func testencodeReaderModeURL() {
307-
let ReaderURL = "http://localhost:6571/reader-mode/page"
307+
let ReaderURL = "http://localhost:\(AppInfo.webserverPort)/reader-mode/page"
308308
let goodurls = [
309-
("https://en.m.wikipedia.org/wiki/Main_Page", URL(string: "http://localhost:6571/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage"))
309+
("https://en.m.wikipedia.org/wiki/Main_Page", URL(string: "http://localhost:\(AppInfo.webserverPort)/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2FMain%5FPage"))
310310
]
311311
goodurls.forEach { XCTAssertEqual(URL(string:$0.0)!.encodeReaderModeURL(ReaderURL), $0.1) }
312312
}
@@ -322,13 +322,13 @@ class NSURLExtensionsTests: XCTestCase {
322322

323323
func testschemeIsValid() {
324324
let goodurls = [
325-
"http://localhost:6571/reader-mode/page",
325+
"http://localhost:\(AppInfo.webserverPort)/reader-mode/page",
326326
"https://google.com",
327327
"tel:6044044004"
328328
]
329329
let badurls = [
330330
"blah://google.com",
331-
"hax://localhost:6571/sessionrestore.html",
331+
"hax://localhost:\(AppInfo.webserverPort)/sessionrestore.html",
332332
"leet://codes.com"
333333
]
334334

@@ -338,15 +338,15 @@ class NSURLExtensionsTests: XCTestCase {
338338

339339
func testisWebPage() {
340340
let goodurls = [
341-
"http://localhost:6571/reader-mode/page",
342-
"https://127.0.0.1:6571/sessionrestore.html",
343-
"data://:6571/sessionrestore.html"
341+
"http://localhost:\(AppInfo.webserverPort)/reader-mode/page",
342+
"https://127.0.0.1:\(AppInfo.webserverPort)/sessionrestore.html",
343+
"data://:\(AppInfo.webserverPort)/sessionrestore.html"
344344

345345
]
346346
let badurls = [
347347
"about://google.com",
348348
"tel:6044044004",
349-
"hax://localhost:6571/about"
349+
"hax://localhost:\(AppInfo.webserverPort)/about"
350350
]
351351

352352
goodurls.forEach { XCTAssertTrue(URL(string:$0)!.isWebPage(), $0) }
@@ -364,15 +364,15 @@ class NSURLExtensionsTests: XCTestCase {
364364

365365
func testdisplayURL() {
366366
let goodurls = [
367-
("http://localhost:6571/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2F", "https://en.m.wikipedia.org/wiki/"),
367+
("http://localhost:\(AppInfo.webserverPort)/reader-mode/page?url=https%3A%2F%2Fen%2Em%2Ewikipedia%2Eorg%2Fwiki%2F", "https://en.m.wikipedia.org/wiki/"),
368368
("\(InternalURL.baseUrl)/\(InternalURL.Path.errorpage)?url=http%3A//mozilla.com", "http://mozilla.com"),
369369
("\(InternalURL.baseUrl)/\(InternalURL.Path.errorpage)?url=http%3A//mozilla.com", "http://mozilla.com"),
370-
("\(InternalURL.baseUrl)/\(InternalURL.Path.errorpage)?url=http%3A%2F%2Flocalhost%3A6571%2Freader-mode%2Fpage%3Furl%3Dhttps%253A%252F%252Fen%252Em%252Ewikipedia%252Eorg%252Fwiki%252F", "https://en.m.wikipedia.org/wiki/"),
370+
("\(InternalURL.baseUrl)/\(InternalURL.Path.errorpage)?url=http%3A%2F%2Flocalhost%3A\(AppInfo.webserverPort)%2Freader-mode%2Fpage%3Furl%3Dhttps%253A%252F%252Fen%252Em%252Ewikipedia%252Eorg%252Fwiki%252F", "https://en.m.wikipedia.org/wiki/"),
371371
("https://mail.example.co.uk/index.html", "https://mail.example.co.uk/index.html"),
372372
]
373373
let badurls = [
374-
"http://localhost:6571/errors/error.html?url=http%3A//localhost%3A6571/about/home/%23panel%3D1",
375-
"http://localhost:6571/errors/error.html",
374+
"http://localhost:\(AppInfo.webserverPort)/errors/error.html?url=http%3A//localhost%3A\(AppInfo.webserverPort)/about/home/%23panel%3D1",
375+
"http://localhost:\(AppInfo.webserverPort)/errors/error.html",
376376

377377
]
378378

@@ -389,7 +389,7 @@ class NSURLExtensionsTests: XCTestCase {
389389
]
390390
let badurls = [
391391
"http:///errors/error.html",
392-
"http://:6571/about/home",
392+
"http://:\(AppInfo.webserverPort)/about/home",
393393
]
394394

395395
goodurls.forEach { XCTAssertEqual(URL(string:$0.0)!.normalizedHostAndPath, $0.1) }

StorageTests/TestLogins.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,7 @@ class TestSQLiteLoginsPerf: XCTestCase {
361361

362362
// Measure time to find all matching results
363363
self.measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: true) {
364-
for _ in 0...5 {
365-
self.logins.searchLoginsWithQuery("username").succeeded()
366-
}
364+
self.logins.searchLoginsWithQuery("username").succeeded()
367365
self.stopMeasuring()
368366
}
369367

XCUITests/BaseTestCase.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import MappaMundi
66
import XCTest
77

8+
let serverPort = Int.random(in: 1025..<65000)
9+
810
func path(forTestPage page: String) -> String {
9-
return "http://localhost:6571/test-fixture/\(page)"
11+
return "http://localhost:\(serverPort)/test-fixture/\(page)"
1012
}
1113

1214
class BaseTestCase: XCTestCase {
@@ -19,7 +21,7 @@ class BaseTestCase: XCTestCase {
1921

2022
// These are used during setUp(). Change them prior to setUp() for the app to launch with different args,
2123
// or, use restart() to re-launch with custom args.
22-
var launchArguments = [LaunchArguments.ClearProfile, LaunchArguments.SkipIntro, LaunchArguments.SkipWhatsNew, LaunchArguments.StageServer, LaunchArguments.DeviceName]
24+
var launchArguments = [LaunchArguments.ClearProfile, LaunchArguments.SkipIntro, LaunchArguments.SkipWhatsNew, LaunchArguments.StageServer, LaunchArguments.DeviceName, "\(LaunchArguments.ServerPort)\(serverPort)"]
2325

2426
func setUpScreenGraph() {
2527
navigator = createScreenGraph(for: self, with: app).navigator()

XCUITests/FindInPageTest.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class FindInPageTests: BaseTestCase {
1717
}
1818

1919
func testFindInLargeDoc() {
20-
navigator.openURL("http://localhost:6571/test-fixture/find-in-page-test.html")
20+
navigator.openURL("http://localhost:\(serverPort)/test-fixture/find-in-page-test.html")
2121
// Workaround until FxSGraph is fixed to allow the previos way with goto
2222
navigator.nowAt(BrowserTab)
2323

@@ -80,7 +80,7 @@ class FindInPageTests: BaseTestCase {
8080
}
8181

8282
func testFindInPageTwoWordsSearchLargeDoc() {
83-
navigator.openURL("http://localhost:6571/test-fixture/find-in-page-test.html")
83+
navigator.openURL("http://localhost:\(serverPort)/test-fixture/find-in-page-test.html")
8484
// Workaround until FxSGraph is fixed to allow the previos way with goto
8585
navigator.nowAt(BrowserTab)
8686
waitForExistence(app/*@START_MENU_TOKEN@*/.buttons["TabLocationView.pageOptionsButton"]/*[[".buttons[\"Page Options Menu\"]",".buttons[\"TabLocationView.pageOptionsButton\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/, timeout: 15)

XCUITests/HistoryTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import XCTest
66

77
let webpage = ["url": "www.mozilla.org", "label": "Internet for people, not profit — Mozilla", "value": "mozilla.org"]
88
// This is part of the info the user will see in recent closed tabs once the default visited website (https://www.mozilla.org/en-US/book/) is closed
9-
let closedWebPageLabel = "localhost:6571/test-fixture/test-mozilla-book.html"
9+
let closedWebPageLabel = "localhost:\(serverPort)/test-fixture/test-mozilla-book.html"
1010

1111
class HistoryTests: BaseTestCase {
1212
let testWithDB = ["testOpenHistoryFromBrowserContextMenuOptions", "testClearHistoryFromSettings"]

XCUITests/SaveLoginsTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import XCTest
66

7-
let domain = "http://localhost:6571"
7+
let domain = "http://localhost:\(serverPort)"
88
let testLoginPage = path(forTestPage: "test-password.html")
9-
let savedLoginEntry = "[email protected], http://localhost:6571"
9+
let savedLoginEntry = "[email protected], http://localhost:\(serverPort)"
1010
let urlLogin = "linkedin.com"
1111
let mailLogin = "[email protected]"
1212

XCUITests/ToolbarTest.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import XCTest
66

7-
let website1: [String: String] = ["url": path(forTestPage: "test-mozilla-org.html"), "label": "Internet for people, not profit — Mozilla", "value": "localhost", "longValue": "localhost:6571/test-fixture/test-mozilla-org.html"]
7+
let website1: [String: String] = ["url": path(forTestPage: "test-mozilla-org.html"), "label": "Internet for people, not profit — Mozilla", "value": "localhost", "longValue": "localhost:\(serverPort)/test-fixture/test-mozilla-org.html"]
88
let website2 = path(forTestPage: "test-example.html")
99

1010
let PDFWebsite = ["url": "http://www.pdf995.com/samples/pdf.pdf"]
@@ -46,7 +46,7 @@ class ToolbarTests: BaseTestCase {
4646

4747
navigator.openURL(website2)
4848
waitUntilPageLoad()
49-
waitForValueContains(app.textFields["url"], value: "localhost:6571")
49+
waitForValueContains(app.textFields["url"], value: "localhost:\(serverPort)")
5050
XCTAssertTrue(app.buttons["URLBarView.backButton"].isEnabled)
5151
XCTAssertFalse(app.buttons["Forward"].isEnabled)
5252

XCUITests/TopTabsTest.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import XCTest
77
let url = "www.mozilla.org"
88
let urlLabel = "Internet for people, not profit — Mozilla"
99
let urlValue = "mozilla.org"
10-
let urlValueLong = "localhost:6571/test-fixture/test-mozilla-org.html"
10+
let urlValueLong = "localhost:\(serverPort)/test-fixture/test-mozilla-org.html"
1111

1212
let urlExample = path(forTestPage: "test-example.html")
1313
let urlLabelExample = "Example Domain"
1414
let urlValueExample = "example"
15-
let urlValueLongExample = "localhost:6571/test-fixture/test-example.html"
15+
let urlValueLongExample = "localhost:\(serverPort)/test-fixture/test-example.html"
1616

1717
let toastUrl = ["url": "twitter.com", "link": "About", "urlLabel": "about"]
1818

test-fixtures/test-password.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<body aria-label="body">
99

10-
<form method="GET" action="http://localhost:6571/test-fixture/test-password-submit.html">
10+
<form method="GET" action="test-fixture/test-password-submit.html">
1111
<p>Username: <input id="username" type="text" value="[email protected]"></p>
1212
<p>Password: <input id="password" type="password" value="verysecret"></p>
1313
<p><input type="submit" value="Login" aria-label="submit"/></p>

0 commit comments

Comments
 (0)