mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-03 02:42:54 +00:00
Fix potential crashing bug (but never seen in production) parsing Feedbin link paging info. Add tests.
This commit is contained in:
1 parent
0eacfe05cd
commit
665bf4d18e
2 files changed
+53
No files matched your search
@@ -27,6 +27,9 @@ public struct HTTPLinkPagingInfo {
|
||||
var dict: [String: String] = [:]
|
||||
for link in links {
|
||||
let components = link.components(separatedBy: "; ")
|
||||
guard components.count > 1 else {
|
||||
continue
|
||||
}
|
||||
let page = components[0].trimmingCharacters(in: CharacterSet(charactersIn: " <>"))
|
||||
dict[components[1]] = page
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// HTTPLinkPagingInfoTests.swift
|
||||
// RSWebTests
|
||||
//
|
||||
// Created by Brent Simmons on 9/1/26.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
import RSWeb
|
||||
|
||||
struct HTTPLinkPagingInfoTests {
|
||||
|
||||
@Test func parsesNextAndLast() throws {
|
||||
let value = "<https://api.feedbin.com/v2/entries.json?page=2>; rel=\"next\", <https://api.feedbin.com/v2/entries.json?page=5>; rel=\"last\""
|
||||
let info = try makeInfo(linkHeader: value)
|
||||
#expect(info.nextPage == "https://api.feedbin.com/v2/entries.json?page=2")
|
||||
#expect(info.lastPage == "https://api.feedbin.com/v2/entries.json?page=5")
|
||||
}
|
||||
|
||||
@Test func malformedSegmentDoesNotCrash() throws {
|
||||
// A segment with no "; " separator used to trap on components[1].
|
||||
let info = try makeInfo(linkHeader: "<https://api.feedbin.com/v2/entries.json?page=2>")
|
||||
#expect(info.nextPage == nil)
|
||||
#expect(info.lastPage == nil)
|
||||
}
|
||||
|
||||
@Test func mixedValidAndMalformedSegments() throws {
|
||||
let value = "<https://api.feedbin.com/v2/entries.json?page=2>; rel=\"next\", <https://api.feedbin.com/v2/entries.json?page=9>"
|
||||
let info = try makeInfo(linkHeader: value)
|
||||
#expect(info.nextPage == "https://api.feedbin.com/v2/entries.json?page=2")
|
||||
#expect(info.lastPage == nil)
|
||||
}
|
||||
|
||||
@Test func noLinkHeader() throws {
|
||||
let info = try makeInfo(linkHeader: nil)
|
||||
#expect(info.nextPage == nil)
|
||||
#expect(info.lastPage == nil)
|
||||
}
|
||||
|
||||
private func makeInfo(linkHeader: String?) throws -> HTTPLinkPagingInfo {
|
||||
let url = try #require(URL(string: "https://api.feedbin.com/v2/entries.json"))
|
||||
var headerFields: [String: String] = [:]
|
||||
if let linkHeader {
|
||||
headerFields[HTTPResponseHeader.link] = linkHeader
|
||||
}
|
||||
let response = try #require(HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: headerFields))
|
||||
return HTTPLinkPagingInfo(urlResponse: response)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user