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
import Foundation
import UIKit
import XCTest
@testable import OldFella
final class SecureSessionUploadFixture {
let suiteName: String
let defaults: UserDefaults
let session: URLSession
let token: String
var uploadedImageSizes: [[String: Int]] = []
lazy var service = ATProtoBlueskyService(defaults: defaults, session: session)
init() throws {
suiteName = "OldFellaServiceTests.\(UUID().uuidString)"
defaults = try XCTUnwrap(UserDefaults(suiteName: suiteName))
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [StubURLProtocol.self]
session = URLSession(configuration: configuration)
let futureExpiry = Int(Date().addingTimeInterval(3600).timeIntervalSince1970)
let payload = Data("{\"exp\":\(futureExpiry)}".utf8).base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
token = "header.\(payload).signature"
}
func install() {
StubURLProtocol.clearRequests()
StubURLProtocol.handler = response
}
func cleanUp() {
StubURLProtocol.handler = nil
session.invalidateAndCancel()
defaults.removePersistentDomain(forName: suiteName)
}
func response(_ request: URLRequest) throws -> (Int, Data) {
let routes: [String: (URLRequest) throws -> (Int, Data)] = [
"/xrpc/com.atproto.identity.resolveHandle": resolveHandle,
"/did:plc:reader": resolveDID,
"/xrpc/com.atproto.server.createSession": createSession,
"/xrpc/app.bsky.actor.getProfile": profile,
"/xrpc/app.bsky.feed.getTimeline": timeline,
"/xrpc/app.bsky.notification.updateSeen": updateSeen,
"/xrpc/app.bsky.notification.listNotifications": notifications,
"/xrpc/app.bsky.feed.getPosts": posts,
"/xrpc/app.bsky.feed.getPostThread": thread,
"/xrpc/com.atproto.repo.uploadBlob": uploadBlob,
"/xrpc/com.atproto.repo.createRecord": createRecord
]
return try routes[request.url?.path ?? ""]?(request)
?? (404, Data("{\"error\":\"NotFound\",\"message\":\"Unexpected test request\"}".utf8))
}
}