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
import Foundation
import XCTest
@testable import OldFella
extension ATProtoServiceTests {
func preferenceResponse(
_ request: URLRequest, token: String, failDefinitions: Bool, omitLabeler: Bool, malformedDefinitions: Bool
) throws -> (Int, Data) {
if let response = try preferenceResponseIdentity(
request, token: token, failDefinitions: failDefinitions, omitLabeler: omitLabeler,
malformedDefinitions: malformedDefinitions) {
return response
}
return try preferenceResponseAction(
request, token: token, failDefinitions: failDefinitions, omitLabeler: omitLabeler,
malformedDefinitions: malformedDefinitions)
}
private func preferenceResponseIdentity(
_ request: URLRequest, token: String, failDefinitions: Bool, omitLabeler: Bool, malformedDefinitions: Bool
) throws -> (Int, Data)? {
switch request.url?.path {
case "/xrpc/com.atproto.identity.resolveHandle": return (200, Data("{\"did\":\"did:plc:reader\"}".utf8))
case "/did:plc:reader":
return (
200,
Data(
("{\"id\":\"did:plc:reader\",\"service\":[{\"id\":\"#atproto_pds\","
+ "\"type\":\"AtprotoPersonalDataServer\","
+ "\"serviceEndpoint\":\"https://reader.example.test\"}]}")
.utf8)
)
case "/xrpc/com.atproto.server.createSession":
return (
200,
Data(
("{\"handle\":\"reader.example\",\"did\":\"did:plc:reader\","
+ "\"accessJwt\":\"\(token)\",\"refreshJwt\":\"refresh\"}")
.utf8)
)
case "/xrpc/app.bsky.actor.getProfile":
return (200, Data("{\"did\":\"did:plc:reader\",\"handle\":\"reader.example\"}".utf8))
default: return nil
}
}
private func preferenceResponseAction(
_ request: URLRequest, token: String, failDefinitions: Bool, omitLabeler: Bool, malformedDefinitions: Bool
) throws -> (Int, Data) {
switch request.url?.path {
case "/xrpc/app.bsky.actor.getPreferences":
return (
200,
Data(
ModerationPreferenceResponses.preferences.utf8)
)
case "/xrpc/app.bsky.labeler.getServices":
if failDefinitions { return (500, Data("{}".utf8)) }
if omitLabeler { return (200, Data("{\"views\":[]}".utf8)) }
if malformedDefinitions {
return (
200,
Data(
("{\"views\":[{\"creator\":{\"did\":\"did:plc:ar7c4by46qjdydhdevvrndac\"},"
+ "\"policies\":{\"labelValueDefinitions\":\"invalid\"}}]}")
.utf8)
)
}
return (
200,
Data(
ModerationPreferenceResponses.definitions.utf8)
)
case "/xrpc/app.bsky.feed.getTimeline": return (200, Data("{\"feed\":[]}".utf8))
default:
XCTFail("Unexpected request: \(request.url?.absoluteString ?? "nil")")
return (404, Data("{}".utf8))
}
}
}