summaryrefslogtreecommitdiff
path: root/SoraTests/ViewDerivedDataTests.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-03-22 13:40:57 +0000
committerFuwn <[email protected]>2026-03-22 14:02:13 +0000
commit92a34a801f423e7fafa9432bc2f48080855a5a53 (patch)
tree757f725d3b890fea93f2d3798ce0e9cff6e6c237 /SoraTests/ViewDerivedDataTests.swift
parentbuild: wire xcodegen team id through just (diff)
downloadsora-testing-92a34a801f423e7fafa9432bc2f48080855a5a53.tar.xz
sora-testing-92a34a801f423e7fafa9432bc2f48080855a5a53.zip
fix: send booru headers for image requests
Diffstat (limited to 'SoraTests/ViewDerivedDataTests.swift')
-rw-r--r--SoraTests/ViewDerivedDataTests.swift131
1 files changed, 105 insertions, 26 deletions
diff --git a/SoraTests/ViewDerivedDataTests.swift b/SoraTests/ViewDerivedDataTests.swift
index 28fd59e..f737e1a 100644
--- a/SoraTests/ViewDerivedDataTests.swift
+++ b/SoraTests/ViewDerivedDataTests.swift
@@ -1083,23 +1083,18 @@ final class ViewDerivedDataTests: XCTestCase { // swiftlint:disable:this type_b
)
}
- func testBooruManagerSupportsOptionalAndCustomUserAgentHeaders() throws {
- let source = try loadSource(at: "Sora/Data/Booru/BooruManager.swift")
- let initSignatureCount = tokenCount(
- matching:
- #"sendUserAgent:\s*Bool\s*=\s*true,\s*customUserAgent:\s*String\s*=\s*"""#,
- in: source
- )
+ func testBooruRequestConfigurationSupportsOptionalAndCustomUserAgentHeaders() throws {
+ let source = try loadSource(at: "Sora/Data/Booru/BooruRequestConfiguration.swift")
let userAgentResolverSection = try extractFunction(
- named: "private static func resolvedUserAgent(",
+ named: "static func resolvedUserAgent(",
from: source
)
let refererResolverSection = try extractFunction(
- named: "private static func baseReferer(for domain: String) -> String",
+ named: "static func baseReferer(for domain: String) -> String",
from: source
)
- let requestURLSection = try extractFunction(
- named: "func requestURL(_ url: URL) async throws -> Data",
+ let requestBuilderSection = try extractFunction(
+ named: "static func request(",
from: source
)
let disableBypassCount = tokenCount(
@@ -1115,49 +1110,133 @@ final class ViewDerivedDataTests: XCTestCase { // swiftlint:disable:this type_b
in: refererResolverSection
)
let refererHeaderCount = tokenCount(
- matching: #"HTTPHeader\(name:\s*"Referer",\s*value:\s*referer\)"#,
- in: requestURLSection
+ matching: #"request\.setValue\(referer,\s*forHTTPHeaderField:\s*"Referer"\)"#,
+ in: requestBuilderSection
)
let explicitUserAgentHeaderCount = tokenCount(
- matching: #"headers\.add\(name:\s*"User-Agent",\s*value:\s*userAgent\)"#,
- in: requestURLSection
+ matching: #"request\.setValue\(userAgent,\s*forHTTPHeaderField:\s*"User-Agent"\)"#,
+ in: requestBuilderSection
)
// swiftlint:disable:next prefer_nimble
XCTAssertGreaterThan(
- initSignatureCount,
- 0,
- "BooruManager should accept booru User-Agent enable and custom override options."
- )
- // swiftlint:disable:next prefer_nimble
- XCTAssertGreaterThan(
disableBypassCount,
0,
- "BooruManager should allow booru requests to omit the User-Agent header."
+ "Booru request configuration should allow booru requests to omit the User-Agent header."
)
// swiftlint:disable:next prefer_nimble
XCTAssertGreaterThan(
customUserAgentTrimCount,
0,
- "BooruManager should normalize custom booru User-Agent values before use."
+ "Booru request configuration should normalize custom booru User-Agent values before use."
)
// swiftlint:disable:next prefer_nimble
XCTAssertGreaterThan(
refererFormatCount,
0,
- "BooruManager should derive booru Referer headers from the provider base URL with a trailing slash."
+ "Booru request configuration should derive Referer headers from the provider base URL"
+ + " with a trailing slash."
)
// swiftlint:disable:next prefer_nimble
XCTAssertGreaterThan(
refererHeaderCount,
0,
- "BooruManager should always attach a Referer header for booru requests."
+ "Booru request configuration should always attach a Referer header for booru requests."
)
// swiftlint:disable:next prefer_nimble
XCTAssertGreaterThan(
explicitUserAgentHeaderCount,
0,
- "BooruManager should continue attaching the User-Agent header only when available."
+ "Booru request configuration should continue attaching the User-Agent header only when available."
+ )
+ }
+
+ func testBooruManagerUsesSharedRequestConfigurationForAPIRequests() throws {
+ let source = try loadSource(at: "Sora/Data/Booru/BooruManager.swift")
+ let requestURLSection = try extractFunction(
+ named: "func requestURL(_ url: URL) async throws -> Data",
+ from: source
+ )
+ let requestBuilderUsageCount = tokenCount(
+ matching: #"BooruRequestConfiguration\.request\("#,
+ in: requestURLSection
+ )
+
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ requestBuilderUsageCount,
+ 0,
+ "BooruManager should build API requests through shared booru request configuration."
+ )
+ }
+
+ func testImageCacheManagerUsesHeaderAwareImageRequests() throws {
+ let source = try loadSource(at: "Sora/Data/ImageCacheManager.swift")
+ let preloadSection = try extractFunction(
+ named: "func preloadImages(",
+ from: source
+ )
+ let loadSection = try extractFunction(
+ named: "func loadImageData(",
+ from: source
+ )
+ let requestBuilderUsageCount = tokenCount(
+ matching: #"BooruRequestConfiguration\.request\("#,
+ in: preloadSection + loadSection
+ )
+ let dataTaskRequestCount = tokenCount(
+ matching: #"URLSession\.shared\.dataTask\(with:\s*request\)"#,
+ in: preloadSection
+ )
+ let dataForRequestCount = tokenCount(
+ matching: #"URLSession\.shared\.data\(for:\s*request\)"#,
+ in: loadSection
+ )
+
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ requestBuilderUsageCount,
+ 1,
+ "ImageCacheManager should build image requests through shared booru request configuration."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ dataTaskRequestCount,
+ 0,
+ "Image preloading should send a header-aware URLRequest."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ dataForRequestCount,
+ 0,
+ "Image loading should send a header-aware URLRequest."
+ )
+ }
+
+ func testNetworkImageViewsUseBooruNetworkImageLoader() throws {
+ let postGridSource = try loadSource(at: "Sora/Views/Post/Grid/PostGridThumbnailView.swift")
+ let detailsSource = try loadSource(at: "Sora/Views/Post/Details/PostDetailsImageView.swift")
+ let favoritesSource = try loadSource(at: "Sora/Views/FavoritePostThumbnailView.swift")
+ let loaderUsageCount = tokenCount(
+ matching: #"\.networkImageLoader\(networkImageLoader\)"#,
+ in: postGridSource + detailsSource + favoritesSource
+ )
+ let loaderConstructionCount = tokenCount(
+ matching: #"BooruNetworkImageLoader\("#,
+ in: postGridSource + detailsSource + favoritesSource
+ )
+
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ loaderUsageCount,
+ 2,
+ "Image views should inject a booru-aware network image loader so CDN requests include required headers."
+ )
+ // swiftlint:disable:next prefer_nimble
+ XCTAssertGreaterThan(
+ loaderConstructionCount,
+ 2,
+ "Image views should construct booru-aware loaders using the current provider domain and request settings."
)
}