Skip to content

Commit b42c56d

Browse files
committed
Fix PlausibleTests
1 parent 9835ba3 commit b42c56d

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

Sources/App/Core/Dependencies/HTTPClient.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import Vapor
2020

2121
@DependencyClient
2222
struct HTTPClient {
23+
typealias Request = Vapor.HTTPClient.Request
2324
typealias Response = Vapor.HTTPClient.Response
2425

25-
var client: @Sendable () -> Vapor.HTTPClient = { XCTFail("client"); return .shared }
26+
var post: @Sendable (_ url: String, _ headers: HTTPHeaders, _ body: Data) async throws -> Response
2627
var fetchDocumentation: @Sendable (_ url: URI) async throws -> Response
2728
var fetchHTTPStatusCode: @Sendable (_ url: String) async throws -> HTTPStatus
2829
var postPlausibleEvent: @Sendable (_ kind: Plausible.Event.Kind, _ path: Plausible.Path, _ user: User?) async throws -> Void
@@ -31,7 +32,10 @@ struct HTTPClient {
3132
extension HTTPClient: DependencyKey {
3233
static var liveValue: HTTPClient {
3334
.init(
34-
client: { .shared },
35+
post: { url, headers, body in
36+
let req = try Request(url: url, method: .POST, headers: headers, body: .data(body))
37+
return try await Vapor.HTTPClient.shared.execute(request: req).get()
38+
},
3539
fetchDocumentation: { url in
3640
try await Vapor.HTTPClient.shared.get(url: url.string).get()
3741
},

Sources/App/Core/Plausible.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import Vapor
16+
import Dependencies
1617

1718

1819
enum Plausible {
@@ -46,19 +47,15 @@ enum Plausible {
4647
static let postEventURL = "https://plausible.io/api/event"
4748

4849
static func postEvent(kind: Event.Kind, path: Path, user: User?) async throws {
49-
#warning("FIXME: need to inject a http client here to so we can test this in PlausibleTests")
5050
guard let siteID = Current.plausibleBackendReportingSiteID() else {
5151
throw Error(message: "PLAUSIBLE_BACKEND_REPORTING_SITE_ID not set")
5252
}
53-
let data = try JSONEncoder().encode(Event(name: .pageview,
53+
let body = try JSONEncoder().encode(Event(name: .pageview,
5454
url: "https://\(siteID)\(path.rawValue)",
5555
domain: siteID,
5656
props: user.props))
57-
let req = try Vapor.HTTPClient.Request(url: postEventURL,
58-
method: .POST,
59-
headers: .applicationJSON,
60-
body: .data(data))
61-
let res = try await Vapor.HTTPClient.shared.execute(request: req).get()
57+
@Dependency(\.httpClient) var httpClient
58+
let res = try await httpClient.post(url: postEventURL, headers: .applicationJSON, body: body)
6259
guard res.status.succeeded else {
6360
throw Error(message: "Request failed with status code: \(res.status)")
6461
}

Tests/AppTests/PlausibleTests.swift

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import XCTest
1616

1717
@testable import App
1818

19+
import Dependencies
20+
1921

2022
final class PlausibleTests: XCTestCase {
2123

@@ -29,43 +31,49 @@ final class PlausibleTests: XCTestCase {
2931
}
3032

3133
func test_postEvent_anonymous() async throws {
32-
Current.plausibleBackendReportingSiteID = { "foo.bar" }
33-
34-
var called = false
35-
let client = MockClient { req, _ in
36-
called = true
37-
// validate
38-
XCTAssertEqual(try? req.content.decode(Plausible.Event.self),
39-
.init(name: .pageview,
40-
url: "https://foo.bar/api/search",
41-
domain: "foo.bar",
42-
props: ["user": "none"]))
43-
}
34+
let called = ActorIsolated(false)
35+
try await withDependencies {
36+
$0.httpClient.post = { @Sendable _, _, body in
37+
await called.withValue { $0 = true }
38+
// validate
39+
XCTAssertEqual(try? JSONDecoder().decode(Plausible.Event.self, from: body),
40+
.init(name: .pageview,
41+
url: "https://foo.bar/api/search",
42+
domain: "foo.bar",
43+
props: ["user": "none"]))
44+
return .ok
45+
}
46+
} operation: {
47+
Current.plausibleBackendReportingSiteID = { "foo.bar" }
4448

45-
// MUT
46-
_ = try await Plausible.postEvent(kind: .pageview, path: .search, user: nil)
49+
// MUT
50+
_ = try await Plausible.postEvent(kind: .pageview, path: .search, user: nil)
4751

48-
XCTAssertTrue(called)
52+
await called.withValue { XCTAssertTrue($0) }
53+
}
4954
}
5055

5156
func test_postEvent_package() async throws {
52-
Current.plausibleBackendReportingSiteID = { "foo.bar" }
57+
let called = ActorIsolated(false)
58+
try await withDependencies {
59+
$0.httpClient.post = { @Sendable _, _, body in
60+
await called.withValue { $0 = true }
61+
// validate
62+
XCTAssertEqual(try? JSONDecoder().decode(Plausible.Event.self, from: body),
63+
.init(name: .pageview,
64+
url: "https://foo.bar/api/packages/{owner}/{repository}",
65+
domain: "foo.bar",
66+
props: ["user": "3c469e9d"]))
67+
return .ok
68+
}
69+
} operation: {
70+
Current.plausibleBackendReportingSiteID = { "foo.bar" }
71+
let user = User(name: "api", identifier: "3c469e9d")
5372

54-
let user = User(name: "api", identifier: "3c469e9d")
55-
var called = false
56-
let client = MockClient { req, _ in
57-
called = true
58-
// validate
59-
XCTAssertEqual(try? req.content.decode(Plausible.Event.self),
60-
.init(name: .pageview,
61-
url: "https://foo.bar/api/packages/{owner}/{repository}",
62-
domain: "foo.bar",
63-
props: ["user": user.identifier]))
64-
}
65-
66-
// MUT
67-
_ = try await Plausible.postEvent(kind: .pageview, path: .package, user: user)
73+
// MUT
74+
_ = try await Plausible.postEvent(kind: .pageview, path: .package, user: user)
6875

69-
XCTAssertTrue(called)
76+
await called.withValue { XCTAssertTrue($0) }
77+
}
7078
}
7179
}

0 commit comments

Comments
 (0)