Skip to content

Commit fe9b061

Browse files
authored
Merge pull request #260 from mvanhorn/fix/256-fix-percent-encode--in-setqueryitems-so-
fix: percent-encode `+` in setQueryItems so cursor pagination works
2 parents 01f6fbc + 2fbb077 commit fe9b061

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ let package = Package(
4040
// dependencies: ["VersionNumberPluginExec"]
4141
// )
4242

43-
// .testTarget(
44-
// name: "ATProtoKitTests",
45-
// dependencies: ["ATProtoKit"]),
43+
.testTarget(
44+
name: "ATProtoKitTests",
45+
dependencies: ["ATProtoKit"]),
4646
]
4747
)
4848

Sources/ATProtoKit/Utilities/APIClientService.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ public struct APIClientService: Sendable {
170170

171171
// Map out each URLQueryItem with the key ($0.0) and value ($0.1) of the item.
172172
components?.queryItems = queryItems.map { URLQueryItem(name: $0.0, value: $0.1) }
173+
if let percentEncodedQuery = components?.percentEncodedQuery {
174+
components?.percentEncodedQuery = percentEncodedQuery.replacingOccurrences(of: "+", with: "%2B")
175+
}
173176

174177
guard let finalURL = components?.url else {
175178
throw ATHTTPRequestError.failedToConstructURLWithParameters

Tests/ATProtoKitTests/ATProtoKitTests.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@ import XCTest
22
@testable import ATProtoKit
33

44
final class ATProtoKitTests: XCTestCase {
5-
func testExample() throws {
6-
// XCTest Documentation
7-
// https://developer.apple.com/documentation/xctest
5+
func testSetQueryItemsPercentEncodesPlusSigns() throws {
6+
let apiClientService = APIClientService(with: APIClientConfiguration())
7+
let requestURL = try XCTUnwrap(URL(string: "https://example.com/xrpc/app.bsky.feed.getFeed"))
88

9-
// Defining Test Cases and Test Methods
10-
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
9+
let urlWithPlus = try apiClientService.setQueryItems(for: requestURL, with: [
10+
("cursor", "abc+def")
11+
])
12+
XCTAssertEqual(urlWithPlus.absoluteString, "https://example.com/xrpc/app.bsky.feed.getFeed?cursor=abc%2Bdef")
13+
XCTAssertFalse(urlWithPlus.absoluteString.contains("+"))
14+
15+
let urlWithoutPlus = try apiClientService.setQueryItems(for: requestURL, with: [
16+
("cursor", "abcdef")
17+
])
18+
XCTAssertEqual(urlWithoutPlus.absoluteString, "https://example.com/xrpc/app.bsky.feed.getFeed?cursor=abcdef")
19+
XCTAssertFalse(urlWithoutPlus.absoluteString.contains("+"))
20+
21+
let urlWithMultipleItems = try apiClientService.setQueryItems(for: requestURL, with: [
22+
("cursor", "abc+def"),
23+
("limit", "50")
24+
])
25+
XCTAssertEqual(urlWithMultipleItems.absoluteString, "https://example.com/xrpc/app.bsky.feed.getFeed?cursor=abc%2Bdef&limit=50")
26+
XCTAssertFalse(urlWithMultipleItems.absoluteString.contains("+"))
1127
}
1228
}

0 commit comments

Comments
 (0)