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
import Foundation
import UIKit
import XCTest
@testable import OldFella
extension ATProtoServiceTests {
func assertSecureSessionImageUpload(_ fixture: SecureSessionUploadFixture) async throws -> Data {
let service = fixture.service
let (imageData, portraitData) = try secureSessionImages()
let post = try await service.publish(
text: "Photo from the harbor.",
replyTo: nil,
quotePost: nil,
images: [
ImageAttachment(data: imageData, altText: "A small red boat"),
ImageAttachment(data: portraitData, altText: "A portrait image")
]
)
XCTAssertEqual(post.id, "at://did:plc:reader/app.bsky.feed.post/new-post")
let requests = StubURLProtocol.recordedRequests
let uploadRequest = try XCTUnwrap(requests.first { $0.url?.path == "/xrpc/com.atproto.repo.uploadBlob" })
XCTAssertEqual(uploadRequest.url?.host, "pds.example.test")
XCTAssertEqual(uploadRequest.httpMethod, "POST")
XCTAssertNil(uploadRequest.value(forHTTPHeaderField: "atproto-proxy"))
XCTAssertEqual(uploadRequest.value(forHTTPHeaderField: "Authorization"), "Bearer \(fixture.token)")
XCTAssertEqual(uploadRequest.value(forHTTPHeaderField: "Content-Type"), "image/jpeg")
let uploadBody = try XCTUnwrap(StubURLProtocol.recordedBody(for: try XCTUnwrap(uploadRequest.url)))
XCTAssertFalse(uploadBody.isEmpty)
let postRequest = try XCTUnwrap(requests.first { $0.url?.path == "/xrpc/com.atproto.repo.createRecord" })
XCTAssertEqual(postRequest.url?.host, "pds.example.test")
XCTAssertEqual(postRequest.httpMethod, "POST")
XCTAssertEqual(postRequest.value(forHTTPHeaderField: "Authorization"), "Bearer \(fixture.token)")
XCTAssertNil(postRequest.value(forHTTPHeaderField: "atproto-proxy"))
try assertSecureSessionImageRecord(fixture, request: postRequest)
return imageData
}
private func assertSecureSessionImageRecord(_ fixture: SecureSessionUploadFixture, request: URLRequest) throws {
let postBody = try XCTUnwrap(StubURLProtocol.recordedBody(for: try XCTUnwrap(request.url)))
let postObject = try XCTUnwrap(JSONSerialization.jsonObject(with: postBody) as? [String: Any])
XCTAssertEqual(postObject["repo"] as? String, "did:plc:reader")
XCTAssertEqual(postObject["collection"] as? String, "app.bsky.feed.post")
let record = try XCTUnwrap(postObject["record"] as? [String: Any])
XCTAssertEqual(record["text"] as? String, "Photo from the harbor.")
let embed = try XCTUnwrap(record["embed"] as? [String: Any])
let images = try XCTUnwrap(embed["images"] as? [[String: Any]])
XCTAssertEqual(embed["$type"] as? String, "app.bsky.embed.images")
XCTAssertEqual(images.count, 2)
let aspectRatios = try images.map { try XCTUnwrap($0["aspectRatio"] as? [String: Int]) }
XCTAssertEqual(aspectRatios, fixture.uploadedImageSizes)
XCTAssertEqual(aspectRatios, [["width": 160, "height": 90], ["width": 90, "height": 160]])
XCTAssertEqual(images.first?["alt"] as? String, "A small red boat")
XCTAssertEqual(images.last?["alt"] as? String, "A portrait image")
let blob = try XCTUnwrap(images.first?["image"] as? [String: Any])
let reference = try XCTUnwrap(blob["ref"] as? [String: Any])
XCTAssertEqual(reference["$link"] as? String, "bafk-image")
}
private func secureSessionImages() throws -> (Data, Data) {
let imageFormat = UIGraphicsImageRendererFormat()
imageFormat.scale = 1
let sourceRenderer = UIGraphicsImageRenderer(size: CGSize(width: 160, height: 90), format: imageFormat)
let sourceImage = sourceRenderer.image { context in
UIColor.systemBlue.setFill()
context.fill(CGRect(x: 0, y: 0, width: 160, height: 90))
}
let imageData = try XCTUnwrap(sourceImage.pngData())
let portraitRenderer = UIGraphicsImageRenderer(size: CGSize(width: 90, height: 160), format: imageFormat)
let portrait = portraitRenderer.image { context in
UIColor.systemRed.setFill()
context.fill(CGRect(x: 0, y: 0, width: 90, height: 160))
}
let portraitData = try XCTUnwrap(portrait.pngData())
return (imageData, portraitData)
}
}