Skip to content

Commit 25d8d3c

Browse files
committed
fix url trailing slash & better tests
1 parent 3a2376a commit 25d8d3c

File tree

5 files changed

+53
-69
lines changed

5 files changed

+53
-69
lines changed

Sources/SwiftHttp/HttpUrl.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public extension HttpUrl {
168168
components.host = host
169169
components.port = port
170170
var path = "/" + path.joined(separator: "/")
171+
171172
if let resource = resource {
172173
path += (resource.hasPrefix("/") ? resource : "/" + resource)
173174
}
@@ -176,9 +177,9 @@ public extension HttpUrl {
176177
path += "/"
177178
}
178179
}
179-
if path.last == "/", !query.isEmpty {
180-
path.removeLast()
181-
}
180+
// if !isTrailingSlashEnabled, path.last == "/", !query.isEmpty {
181+
// path.removeLast()
182+
// }
182183
components.path = path
183184
components.fragment = fragment
184185
components.queryItems = query.map { .init(name: $0.key, value: $0.value) }

Tests/SwiftHttpTests/Helpers/FeatherApi.swift

Lines changed: 0 additions & 42 deletions
This file was deleted.

Tests/SwiftHttpTests/Helpers/PostApi.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,39 @@ import Foundation
99
import SwiftHttp
1010

1111
struct PostApi: HttpCodablePipelineCollection {
12+
13+
// struct Failure: Codable {
14+
// let message: String
15+
// }
1216

1317
let client: HttpClient = UrlSessionHttpClient(log: true)
1418
let apiBaseUrl = HttpUrl(host: "jsonplaceholder.typicode.com")
15-
19+
20+
/// NOTE: this API call should return a 404 response
21+
func invalidApiCall() async throws -> [Post] {
22+
try await decodableRequest(executor: client.dataTask,
23+
url: apiBaseUrl.path("invalid-posts"),
24+
method: .get)
25+
}
1626

1727
func listPosts() async throws -> [Post] {
1828
try await decodableRequest(executor: client.dataTask,
1929
url: apiBaseUrl.path("posts"),
2030
method: .get)
2131
}
2232

33+
func filterPosts(_ userId: Int) async throws -> [Post] {
34+
try await decodableRequest(
35+
executor: client.dataTask,
36+
url: apiBaseUrl
37+
.path("posts")
38+
.query([
39+
"userId": String(userId),
40+
]),
41+
method: .get)
42+
}
43+
44+
2345
func getPost(_ id: Int) async throws -> Post {
2446
try await decodableRequest(executor: client.dataTask,
2547
url: apiBaseUrl.path("posts", String(id)),

Tests/SwiftHttpTests/HttpUrlTests.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,39 @@ import XCTest
1010

1111
final class HttpUrlTests: XCTestCase {
1212

13-
func testBaseUrl() async throws {
13+
func testPaths() async throws {
1414
let baseUrl = HttpUrl(host: "jsonplaceholder.typicode.com")
1515

16-
let todosUrl = baseUrl.path("todos")
17-
XCTAssertEqual(todosUrl.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/")
18-
1916
let todoUrl = baseUrl.path("todos", String(1))
2017
XCTAssertEqual(todoUrl.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/1/")
2118

22-
let query1Url = baseUrl.path("todos").query("foo", "bar")
23-
XCTAssertEqual(query1Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos?foo=bar")
24-
2519
let query2Url = baseUrl.path("todos").query([
2620
"foo": "1",
2721
])
28-
XCTAssertEqual(query2Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos?foo=1")
22+
XCTAssertEqual(query2Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/?foo=1")
2923
}
3024

31-
func testTrailingSlash() {
25+
func testTrailingSlashEnabled() {
26+
let baseUrl = HttpUrl(host: "jsonplaceholder.typicode.com", trailingSlashEnabled: true)
27+
28+
let todosUrl = baseUrl.path("todos")
29+
XCTAssertEqual(todosUrl.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/")
30+
31+
let sitemapUrl = baseUrl.path("todos").resource("sitemap.xml")
32+
XCTAssertEqual(sitemapUrl.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/sitemap.xml")
33+
34+
let query1Url = baseUrl.path("todos").query("foo", "bar")
35+
XCTAssertEqual(query1Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/?foo=bar")
36+
}
37+
38+
func testTrailingSlashDisabled() {
3239
let baseUrl = HttpUrl(host: "jsonplaceholder.typicode.com", trailingSlashEnabled: false)
3340

3441
let todosUrl = baseUrl.path("todos")
3542
XCTAssertEqual(todosUrl.url.absoluteString, "https://jsonplaceholder.typicode.com/todos")
43+
44+
let sitemapUrl = baseUrl.path("todos").resource("sitemap.xml")
45+
XCTAssertEqual(sitemapUrl.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/sitemap.xml")
3646

3747
let query1Url = baseUrl.path("todos").query("foo", "bar")
3848
XCTAssertEqual(query1Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos?foo=bar")

Tests/SwiftHttpTests/SwiftHttpTests.swift

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,22 @@ final class SwiftHttpTests: XCTestCase {
3131
}
3232

3333
func testError() async throws {
34-
let api = FeatherApi()
34+
let api = PostApi()
3535
do {
36-
_ = try await api.test()
36+
_ = try await api.invalidApiCall()
3737
}
3838
catch HttpError.invalidStatusCode(let res) {
39-
let decoder = HttpResponseDecoder<FeatherError>(decoder: JSONDecoder())
40-
do {
41-
let error = try decoder.decode(res.data)
42-
print(res.statusCode, error)
43-
}
44-
catch {
45-
print(error.localizedDescription)
46-
}
39+
XCTAssertEqual(res.statusCode, .notFound)
4740
}
4841
catch {
49-
print(error.localizedDescription)
42+
XCTFail(error.localizedDescription)
5043
}
5144
}
5245

5346
func testQueryParams() async throws {
54-
let api = FeatherApi()
55-
let res = try await api.testQueryParams()
56-
XCTAssertEqual(res, "ok")
47+
let api = PostApi()
48+
let res = try await api.filterPosts(1)
49+
XCTAssertEqual(res.count, 10)
5750
}
5851
}
5952

0 commit comments

Comments
 (0)