Skip to content

Commit 5e3a988

Browse files
committed
Remove deprecated fetchReadme
1 parent f32e475 commit 5e3a988

File tree

2 files changed

+44
-60
lines changed

2 files changed

+44
-60
lines changed

Sources/App/Core/Github.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -208,31 +208,6 @@ extension Github {
208208
return try? await Github.fetchResource(Github.License.self, url: url)
209209
}
210210

211-
@available(*, deprecated)
212-
static func fetchReadme(client: Client, owner: String, repository: String) async -> Readme? {
213-
let uri = Github.apiUri(owner: owner, repository: repository, resource: .readme)
214-
215-
// Fetch readme html content
216-
let readme = try? await Github.fetch(client: client, uri: uri, headers: [
217-
("Accept", "application/vnd.github.html+json")
218-
])
219-
guard var html = readme?.content else { return nil }
220-
221-
// Fetch readme html url
222-
let htmlUrl: String? = await {
223-
struct Response: Decodable {
224-
var htmlUrl: String
225-
}
226-
return try? await Github.fetchResource(Response.self, client: client, uri: uri).htmlUrl
227-
}()
228-
guard let htmlUrl else { return nil }
229-
230-
// Extract and replace images that need caching
231-
let imagesToCache = replaceImagesRequiringCaching(owner: owner, repository: repository, readme: &html)
232-
233-
return .init(etag: readme?.etag, html: html, htmlUrl: htmlUrl, imagesToCache: imagesToCache)
234-
}
235-
236211
static func fetchReadme(owner: String, repository: String) async -> Readme? {
237212
let url = Github.apiURL(owner: owner, repository: repository, resource: .readme)
238213

Tests/AppTests/GithubTests.swift

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -349,48 +349,52 @@ class GithubTests: AppTestCase {
349349
// setup
350350
Current.githubToken = { "secr3t" }
351351
let requestCount = QueueIsolated(0)
352-
let client = MockClient { req, resp in
353-
requestCount.increment()
354-
switch req.headers[.accept] {
355-
case ["application/vnd.github.html+json"]:
356-
resp.status = .ok
357-
resp.body = makeBody("readme html")
358-
resp.headers.add(name: .eTag, value: "etag")
359-
case []:
360-
resp.status = .ok
361-
struct Response: Encodable {
362-
var htmlUrl: String
363-
}
364-
resp.body = makeBody(try! JSONEncoder().encode(Response(htmlUrl: "readme url")))
365-
default:
366-
XCTFail("unexpected accept header")
352+
await withDependencies {
353+
$0.httpClient.get = { @Sendable _, headers in
354+
requestCount.increment()
355+
switch headers[.accept] {
356+
case ["application/vnd.github.html+json"]:
357+
return .ok(body: "readme html", headers: ["ETag": "etag"])
358+
case []:
359+
struct Response: Encodable {
360+
var htmlUrl: String
361+
}
362+
return try .ok(jsonEncode: Response(htmlUrl: "readme url"))
363+
default:
364+
XCTFail("unexpected accept header")
365+
}
366+
enum Error: Swift.Error { case unexpectedCodePath }
367+
throw Error.unexpectedCodePath
367368
}
368-
}
369+
} operation: {
370+
// MUT
371+
let res = await Github.fetchReadme(owner: "foo", repository: "bar")
369372

370-
// MUT
371-
let res = await Github.fetchReadme(client: client, owner: "foo", repository: "bar")
372-
373-
// validate
374-
XCTAssertEqual(requestCount.value, 2)
375-
XCTAssertEqual(
376-
res,
377-
.init(etag: "etag",
378-
html: "readme html",
379-
htmlUrl: "readme url",
380-
imagesToCache: [])
381-
)
373+
// validate
374+
XCTAssertEqual(requestCount.value, 2)
375+
XCTAssertEqual(
376+
res,
377+
.init(etag: "etag",
378+
html: "readme html",
379+
htmlUrl: "readme url",
380+
imagesToCache: [])
381+
)
382+
}
382383
}
383384

384385
func test_fetchReadme_notFound() async throws {
385386
// setup
386387
Current.githubToken = { "secr3t" }
387-
let client = MockClient { _, resp in resp.status = .notFound }
388388

389-
// MUT
390-
let res = await Github.fetchReadme(client: client, owner: "foo", repository: "bar")
389+
await withDependencies {
390+
$0.httpClient.get = { @Sendable _, headers in .notFound }
391+
} operation: {
392+
// MUT
393+
let res = await Github.fetchReadme(owner: "foo", repository: "bar")
391394

392-
// validate
393-
XCTAssertEqual(res, nil)
395+
// validate
396+
XCTAssertEqual(res, nil)
397+
}
394398
}
395399

396400
func test_extractImagesRequiringCaching() async throws {
@@ -482,11 +486,16 @@ class GithubTests: AppTestCase {
482486

483487

484488
private extension HTTPClient.Response {
485-
static func ok(body: String) -> Self {
486-
.init(status: .ok, body: .init(string: body))
489+
static func ok(body: String, headers: HTTPHeaders = .init()) -> Self {
490+
.init(status: .ok, headers: headers, body: .init(string: body))
487491
}
488492

489493
static func ok(fixture: String) throws -> Self {
490494
try .init(status: .ok, body: .init(data: fixtureData(for: fixture)))
491495
}
496+
497+
static func ok<T: Encodable>(jsonEncode value: T) throws -> Self {
498+
let data = try JSONEncoder().encode(value)
499+
return .init(status: .ok, body: .init(data: data))
500+
}
492501
}

0 commit comments

Comments
 (0)