forked from quoid/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserscriptsTests.swift
More file actions
202 lines (186 loc) · 7.31 KB
/
Copy pathUserscriptsTests.swift
File metadata and controls
202 lines (186 loc) · 7.31 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// UserscriptsTests.swift
// UserscriptsTests
//
// Created by Justin Wasack on 1/23/22.
// Copyright © 2022 Justin Wasack. All rights reserved.
//
import XCTest
@testable import Userscripts_Mac_Safari_Extension
class UserscriptsTests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}
func testStringSanitization() throws {
// given
let strs = [
"String",
"https://something.com/?foo=12",
"I have backslashes \\\\",
".....ok",
":Akneh.,><>dkie:lm",
"..解锁B站大会员番剧、",
"解锁B站大会员番剧、B站视频解析下载;全网VIP视频免费破解去广告;全网音乐直接下载;油管、Facebook等国外视频解析下载;网盘搜索引擎破解无限下载等",
"5CLksm3AAbb2F2F2f----___--+87363&^#%o%3O3",
"Example Userscript Name"
]
// when
var result = [String]()
for str in strs {
let sanitizedString = sanitize(str)
let unsanitizedString = unsanitize(sanitizedString)
result.append(unsanitizedString)
}
// then
XCTAssert(result.elementsEqual(strs))
}
func testEncodedCheck() throws {
let urls = [
"https://greasyfork.org/scripts/416338-redirect-外链跳转/code/redirect%20外链跳转.user.js",
"https://raw.githubusercontent.com/Anarios/return-youtube-dislike/main/Extensions/UserScript/Return%20Youtube%20Dislike.user.js",
"https://cdn.frankerfacez.com/static/ffz_injector.user.js",
"http://www.k21p.com/example.user.js", // add http protocol
"https://greasyfork.org/scripts/416338-redirect-外链跳转/code/redirect 外链跳转.user.js"
]
var result = [String]()
for url in urls {
if isEncoded(url) {
result.append(url)
}
}
// 2 urls already percent encoded
XCTAssert(result.count == 2)
}
func testGetRemoteFileContents() throws {
let urls = [
"https://greasyfork.org/scripts/416338-redirect-外链跳转/code/redirect%20外链跳转.user.js",
"https://greasyfork.org/scripts/416338-redirect-外链跳转/code/redirect 外链跳转.user.js",
"https://raw.githubusercontent.com/Anarios/return-youtube-dislike/main/Extensions/UserScript/Return%20Youtube%20Dislike.user.js",
"https://cdn.frankerfacez.com/static/ffz_injector.user.js",
"http://www.k21p.com/example.user.js" // add http protocol
]
var result = [String]()
for url in urls {
if let contents = getRemoteFileContents(url) {
result.append(contents)
}
}
XCTAssert(result.count == urls.count)
}
func testFileRemoteUpdate() throws {
let urls = [
"https://www.k21p.com/example.user.js",
"https://www.k21p.com/example.user.js?foo=bar", // query string
"http://www.k21p.com/example.user.js", // http protocol
"https://greasyfork.org/scripts/416338-redirect-外链跳转/code/redirect 外链跳转.user.js", // non latin chars
"https://www.k21p.com/example.user.jsx" // should fail
]
var result = [Int]()
for url in urls {
let content = """
// ==UserScript==
// @name test
// @match *://*/*
// @version 0.1
// @updateURL http://www.k21p.com/example.user.js
// @downloadURL \(url)
// ==/UserScript==
""";
let response = getFileRemoteUpdate(content)
if !response.keys.contains("error") {
result.append(1)
}
}
XCTAssert(result.count == (urls.count - 1))
}
func testMatching() throws {
var count = 0
var result = [String]()
let patternDict = [
"*://*/*": [
"https://www.bing.com/",
"https://example.org/foo/bar.html",
"https://a.org/some/path/"
],
"*://*.mozilla.org/*": [
"http://mozilla.org/",
"https://mozilla.org/",
"https://b.mozilla.org/path/"
],
"*://www.google.com/*": [
"https://www.google.com/://aa",
"https://www.google.com/preferences?prev=https://www.google.com/",
"https://www.google.com/preferences?prev=",
"https://www.google.com/"
],
"*://localhost/*": [
"http://localhost:8000/",
"https://localhost:3000/foo.html"
],
"http://127.0.0.1/*": [
"http://127.0.0.1/",
"http://127.0.0.1/foo/bar.html"
],
"*://*.example.com/*?a=1*": [
"http://example.com/?a=1",
"https://www.example.com/index?a=1&b=2"
]
]
let patternDictFails = [
"https://www.example.com/*": [
"file://www.example.com/",
"ftp://www.example.com/",
"ws://www.example.com/",
"http://www.example.com/"
],
"http://www.example.com/index.html": [
"http://www.example.com/",
"https://www.example.com/index.html"
],
"*://localhost/*": [
"https://localhost.com/",
"ftp://localhost:8080/"
],
"https://www.example*/*": [
"https://www.example.com/"
],
"*://*.example.com/*?a=1*": [
"http://example.com/",
"https://www.example.com/?a=2"
]
]
for (pattern, urls) in patternDict {
count = count + urls.count
for url in urls {
if match(url, pattern) {
result.append("1")
}
}
}
for (pattern, urls) in patternDictFails {
// don't increment count since these tests should fail
for url in urls {
if match(url, pattern) {
result.removeLast()
}
}
}
XCTAssert(result.count == count)
}
func testPerformanceExample() throws {
// This is an example of a performance test case.
measure {
// Put the code you want to measure the time of here.
}
}
}