forked from cliqz-oss/browser-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockSyncServer.swift
447 lines (367 loc) · 17 KB
/
MockSyncServer.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* 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 Shared
import Sync
import XCTest
private let log = Logger.syncLogger
private func optTimestamp(x: AnyObject?) -> Timestamp? {
guard let str = x as? String else {
return nil
}
return decimalSecondsStringToTimestamp(str)
}
private func optStringArray(x: AnyObject?) -> [String]? {
guard let str = x as? String else {
return nil
}
return str.componentsSeparatedByString(",").map { $0.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) }
}
private struct SyncRequestSpec {
let collection: String
let id: String?
let ids: [String]?
let limit: Int?
let offset: String?
let sort: SortOption?
let newer: Timestamp?
let full: Bool
static func fromRequest(request: GCDWebServerRequest) -> SyncRequestSpec? {
// Input is "/1.5/user/storage/collection", possibly with "/id" at the end.
// That means we get five or six path components here, the first being empty.
let parts = request.path!.componentsSeparatedByString("/").filter { !$0.isEmpty }
let id: String?
let ids = optStringArray(request.query["ids"])
let newer = optTimestamp(request.query["newer"])
let full: Bool = request.query["full"] != nil
let limit: Int?
if let lim = request.query["limit"] as? String {
limit = Int(lim)
} else {
limit = nil
}
let offset = request.query["offset"] as? String
let sort: SortOption?
switch request.query["sort"] as? String ?? "" {
case "oldest":
sort = SortOption.OldestFirst
case "newest":
sort = SortOption.NewestFirst
case "index":
sort = SortOption.Index
default:
sort = nil
}
if parts.count < 4 {
return nil
}
if parts[2] != "storage" {
return nil
}
// Use dropFirst, you say! It's buggy.
switch parts.count {
case 4:
id = nil
case 5:
id = parts[4]
default:
// Uh oh.
return nil
}
return SyncRequestSpec(collection: parts[3], id: id, ids: ids, limit: limit, offset: offset, sort: sort, newer: newer, full: full)
}
}
struct SyncDeleteRequestSpec {
let collection: String?
let id: GUID?
let ids: [GUID]?
let wholeCollection: Bool
static func fromRequest(request: GCDWebServerRequest) -> SyncDeleteRequestSpec? {
// Input is "/1.5/user{/storage{/collection{/id}}}".
// That means we get four, five, or six path components here, the first being empty.
return SyncDeleteRequestSpec.fromPath(request.path!, withQuery: request.query)
}
static func fromPath(path: String, withQuery query: [NSObject: AnyObject]) -> SyncDeleteRequestSpec? {
let parts = path.componentsSeparatedByString("/").filter { !$0.isEmpty }
let queryIDs: [GUID]? = (query["ids"] as? String)?.componentsSeparatedByString(",")
guard [2, 4, 5].contains(parts.count) else {
return nil
}
if parts.count == 2 {
return SyncDeleteRequestSpec(collection: nil, id: nil, ids: queryIDs, wholeCollection: true)
}
if parts[2] != "storage" {
return nil
}
if parts.count == 4 {
let hasIDs = queryIDs != nil
return SyncDeleteRequestSpec(collection: parts[3], id: nil, ids: queryIDs, wholeCollection: !hasIDs)
}
return SyncDeleteRequestSpec(collection: parts[3], id: parts[4], ids: queryIDs, wholeCollection: false)
}
}
private struct SyncPutRequestSpec {
let collection: String
let id: String
static func fromRequest(request: GCDWebServerRequest) -> SyncPutRequestSpec? {
// Input is "/1.5/user/storage/collection/id}}}".
// That means we get six path components here, the first being empty.
let parts = request.path!.componentsSeparatedByString("/").filter { !$0.isEmpty }
guard parts.count == 5 else {
return nil
}
if parts[2] != "storage" {
return nil
}
return SyncPutRequestSpec(collection: parts[3], id: parts[4])
}
}
class MockSyncServer {
let server = GCDWebServer()
let username: String
var offsets: Int = 0
var continuations: [String: [EnvelopeJSON]] = [:]
var collections: [String: (modified: Timestamp, records: [String: EnvelopeJSON])] = [:]
var baseURL: String!
init(username: String) {
self.username = username
}
class func makeValidEnvelope(guid: GUID, modified: Timestamp) -> EnvelopeJSON {
let clientBody: [String: AnyObject] = [
"id": guid,
"name": "Foobar",
"commands": [],
"type": "mobile",
]
let clientBodyString = JSON(clientBody).toString(false)
let clientRecord: [String : AnyObject] = [
"id": guid,
"collection": "clients",
"payload": clientBodyString,
"modified": Double(modified) / 1000,
]
return EnvelopeJSON(JSON(clientRecord).toString(false))
}
class func withHeaders(response: GCDWebServerResponse, lastModified: Timestamp? = nil, records: Int? = nil, timestamp: Timestamp? = nil) -> GCDWebServerResponse {
let timestamp = timestamp ?? NSDate.now()
let xWeaveTimestamp = millisecondsToDecimalSeconds(timestamp)
response.setValue("\(xWeaveTimestamp)", forAdditionalHeader: "X-Weave-Timestamp")
if let lastModified = lastModified {
let xLastModified = millisecondsToDecimalSeconds(lastModified)
response.setValue("\(xLastModified)", forAdditionalHeader: "X-Last-Modified")
}
if let records = records {
response.setValue("\(records)", forAdditionalHeader: "X-Weave-Records")
}
return response;
}
func storeRecords(records: [EnvelopeJSON], inCollection collection: String, now: Timestamp? = nil) {
let now = now ?? NSDate.now()
let coll = self.collections[collection]
var out = coll?.records ?? [:]
records.forEach {
out[$0.id] = $0.withModified(now)
}
let newModified = max(now, coll?.modified ?? 0)
self.collections[collection] = (modified: newModified, records: out)
}
private func splitArray<T>(items: [T], at: Int) -> ([T], [T]) {
return (Array(items.dropLast(items.count - at)), Array(items.dropFirst(at)))
}
private func recordsMatchingSpec(spec: SyncRequestSpec) -> (records: [EnvelopeJSON], offsetID: String?)? {
// If we have a provided offset, handle that directly.
if let offset = spec.offset {
log.debug("Got provided offset \(offset).")
guard let remainder = self.continuations[offset] else {
log.error("Unknown offset.")
return nil
}
// Remove the old one.
self.continuations.removeValueForKey(offset)
// Handle the smaller-than-limit or no-provided-limit cases.
guard let limit = spec.limit where limit < remainder.count else {
log.debug("Returning all remaining items.")
return (remainder, nil)
}
// Record the next continuation and return the first slice of records.
let next = "\(self.offsets++)"
let (returned, remaining) = splitArray(remainder, at: limit)
self.continuations[next] = remaining
log.debug("Returning \(limit) items; next continuation is \(next).")
return (returned, next)
}
guard let records = self.collections[spec.collection]?.records.values else {
// No matching records.
return ([], nil)
}
var items = Array(records)
log.debug("Got \(items.count) candidate records.")
if spec.newer ?? 0 > 0 {
items = items.filter { $0.modified > spec.newer }
}
if let ids = spec.ids {
let ids = Set(ids)
items = items.filter { ids.contains($0.id) }
}
if let sort = spec.sort {
switch sort {
case SortOption.NewestFirst:
items.sortInPlace { $0.modified > $1.modified }
log.debug("Sorted items newest first: \(items.map { $0.modified })")
case SortOption.OldestFirst:
items.sortInPlace { $0.modified < $1.modified }
log.debug("Sorted items oldest first: \(items.map { $0.modified })")
case SortOption.Index:
log.warning("Index sorting not yet supported.")
}
}
if let limit = spec.limit where items.count > limit {
let next = "\(self.offsets++)"
let (returned, remaining) = splitArray(items, at: limit)
self.continuations[next] = remaining
return (returned, next)
}
return (items, nil)
}
private func recordResponse(record: EnvelopeJSON) -> GCDWebServerResponse {
let body = JSON(record.asJSON()).toString()
let bodyData = body.utf8EncodedData
let response = GCDWebServerDataResponse(data: bodyData, contentType: "application/json")
return MockSyncServer.withHeaders(response, lastModified: record.modified)
}
private func modifiedResponse(timestamp: Timestamp) -> GCDWebServerResponse {
let body = JSON(["modified": NSNumber(unsignedLongLong: timestamp)]).toString()
let bodyData = body.utf8EncodedData
let response = GCDWebServerDataResponse(data: bodyData, contentType: "application/json")
return MockSyncServer.withHeaders(response)
}
func modifiedTimeForCollection(collection: String) -> Timestamp? {
return self.collections[collection]?.modified
}
func removeAllItemsFromCollection(collection: String, atTime: Timestamp) {
if self.collections[collection] != nil {
self.collections[collection] = (atTime, [:])
}
}
func start() {
let basePath = "/1.5/\(self.username)"
let storagePath = "\(basePath)/storage/"
let infoCollectionsPath = "\(basePath)/info/collections"
server.addHandlerForMethod("GET", path: infoCollectionsPath, requestClass: GCDWebServerRequest.self) { (request) -> GCDWebServerResponse! in
var ic = [String: NSNumber]()
var lastModified: Timestamp = 0
for collection in self.collections.keys {
if let timestamp = self.modifiedTimeForCollection(collection) {
ic[collection] = NSNumber(double: Double(timestamp) / 1000)
lastModified = max(lastModified, timestamp)
}
}
let body = JSON(ic).toString()
let bodyData = body.utf8EncodedData
let response = GCDWebServerDataResponse(data: bodyData, contentType: "application/json")
return MockSyncServer.withHeaders(response, lastModified: lastModified, records: ic.count)
}
let matchPut: GCDWebServerMatchBlock = { method, url, headers, path, query -> GCDWebServerRequest! in
guard method == "PUT" && path.startsWith(basePath) else {
return nil
}
return GCDWebServerDataRequest(method: method, url: url, headers: headers, path: path, query: query)
}
server.addHandlerWithMatchBlock(matchPut) { (request) -> GCDWebServerResponse! in
guard let request = request as? GCDWebServerDataRequest else {
return MockSyncServer.withHeaders(GCDWebServerDataResponse(statusCode: 400))
}
guard let spec = SyncPutRequestSpec.fromRequest(request) else {
return MockSyncServer.withHeaders(GCDWebServerDataResponse(statusCode: 400))
}
var body = JSON(request.jsonObject).asDictionary!
body["modified"] = JSON(millisecondsToDecimalSeconds(NSDate.now()))
let record = EnvelopeJSON(JSON(body))
self.storeRecords([record], inCollection: spec.collection)
let timestamp = self.modifiedTimeForCollection(spec.collection)!
let response = GCDWebServerDataResponse(data: millisecondsToDecimalSeconds(timestamp).utf8EncodedData, contentType: "application/json")
return MockSyncServer.withHeaders(response)
}
let matchDelete: GCDWebServerMatchBlock = { method, url, headers, path, query -> GCDWebServerRequest! in
guard method == "DELETE" && path.startsWith(basePath) else {
return nil
}
return GCDWebServerRequest(method: method, url: url, headers: headers, path: path, query: query)
}
server.addHandlerWithMatchBlock(matchDelete) { (request) -> GCDWebServerResponse! in
guard let spec = SyncDeleteRequestSpec.fromRequest(request) else {
return GCDWebServerDataResponse(statusCode: 400)
}
if let collection = spec.collection, id = spec.id {
guard var items = self.collections[collection]?.records else {
// Unable to find the requested collection.
return MockSyncServer.withHeaders(GCDWebServerDataResponse(statusCode: 404))
}
guard let item = items[id] else {
// Unable to find the requested id.
return MockSyncServer.withHeaders(GCDWebServerDataResponse(statusCode: 404))
}
items.removeValueForKey(id)
return self.modifiedResponse(item.modified)
}
if let collection = spec.collection {
if spec.wholeCollection {
self.collections.removeValueForKey(collection)
} else {
if let ids = spec.ids,
var map = self.collections[collection]?.records {
for id in ids {
map.removeValueForKey(id)
}
self.collections[collection] = (modified: NSDate.now(), records: map)
}
}
return self.modifiedResponse(NSDate.now())
}
self.collections = [:]
return MockSyncServer.withHeaders(GCDWebServerDataResponse(data: "{}".utf8EncodedData, contentType: "application/json"))
}
let match: GCDWebServerMatchBlock = { method, url, headers, path, query -> GCDWebServerRequest! in
guard method == "GET" && path.startsWith(storagePath) else {
return nil
}
return GCDWebServerRequest(method: method, url: url, headers: headers, path: path, query: query)
}
server.addHandlerWithMatchBlock(match) { (request) -> GCDWebServerResponse! in
// 1. Decide what the URL is asking for. It might be a collection fetch or
// an individual record, and it might have query parameters.
guard let spec = SyncRequestSpec.fromRequest(request) else {
return MockSyncServer.withHeaders(GCDWebServerDataResponse(statusCode: 400))
}
// 2. Grab the matching set of records. Prune based on TTL, exclude with X-I-U-S, etc.
if let id = spec.id {
guard let collection = self.collections[spec.collection], record = collection.records[id] else {
// Unable to find the requested collection/id.
return MockSyncServer.withHeaders(GCDWebServerDataResponse(statusCode: 404))
}
return self.recordResponse(record)
}
guard let (items, offset) = self.recordsMatchingSpec(spec) else {
// Unable to find the provided offset.
return MockSyncServer.withHeaders(GCDWebServerDataResponse(statusCode: 400))
}
// TODO: TTL
// TODO: X-I-U-S
let body = JSON(items.map { $0.asJSON() }).toString()
let bodyData = body.utf8EncodedData
let response = GCDWebServerDataResponse(data: bodyData, contentType: "application/json")
// 3. Compute the correct set of headers: timestamps, X-Weave-Records, etc.
if let offset = offset {
response.setValue(offset, forAdditionalHeader: "X-Weave-Next-Offset")
}
let timestamp = self.modifiedTimeForCollection(spec.collection)!
log.debug("Returning GET response with X-Last-Modified for \(items.count) records: \(timestamp).")
return MockSyncServer.withHeaders(response, lastModified: timestamp, records: items.count)
}
if server.startWithPort(0, bonjourName: nil) == false {
XCTFail("Can't start the GCDWebServer.")
}
baseURL = "http://localhost:\(server.port)\(basePath)"
}
}