From e2458f5dad0cac554dcaaaec1b31976c4413b17f Mon Sep 17 00:00:00 2001 From: Jeff Huff Date: Fri, 30 Jun 2023 10:37:29 -0700 Subject: [PATCH 1/2] Update RateLimit parsing for Multiple values. --- Sources/RateLimit.swift | 8 ++++++-- Tests/RateLimitTests.swift | 11 ++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Sources/RateLimit.swift b/Sources/RateLimit.swift index 74777fb4..ed127776 100644 --- a/Sources/RateLimit.swift +++ b/Sources/RateLimit.swift @@ -16,8 +16,12 @@ public struct RateLimit: Codable { init(value: String, requestURL: URL?) { self.requestURL = requestURL - - let items = value.split(separator: ";", omittingEmptySubsequences: true) + + let items = value.split(separator: ",", omittingEmptySubsequences: true).flatMap { + $0.trimmingCharacters(in: CharacterSet.whitespaces) + .split(separator: ";", omittingEmptySubsequences: true) + .map({ $0.trimmingCharacters(in: CharacterSet.whitespaces) }) + } entries = items.reduce(into: [:]) { partialResult, item in guard let colonIdx = item.firstIndex(of: ":") else { return diff --git a/Tests/RateLimitTests.swift b/Tests/RateLimitTests.swift index 69300233..a7e3e2dd 100644 --- a/Tests/RateLimitTests.swift +++ b/Tests/RateLimitTests.swift @@ -36,7 +36,16 @@ final class RateLimitTests: XCTestCase { XCTAssertEqual(rateLimit.entries["user-minute-lim"], 150) XCTAssertEqual(rateLimit.entries["user-minute-rem"], 149) } - + + func testMultipleItems() { + let rateLimit = RateLimit(value: "user-hour-lim:3600;user-hour-rem:3598;, user-minute-lim:150;user-minute-rem:149;", requestURL: nil) + XCTAssertEqual(rateLimit.entries.count, 4) + XCTAssertEqual(rateLimit.entries["user-hour-lim"], 3600) + XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3598) + XCTAssertEqual(rateLimit.entries["user-minute-lim"], 150) + XCTAssertEqual(rateLimit.entries["user-minute-rem"], 149) + } + func testExtractingHeaderField() { let httpUrlResponse = HTTPURLResponse(url: URL(string: "https://api.appstoreconnect.apple.com/")!, statusCode: 200, httpVersion: nil, headerFields: ["X-ratE-limiT": "user-hour-lim:3600;user-hour-rem:3545;"]) let rateLimit = httpUrlResponse?.rateLimit From a5477883a1d4bafd6be4f31af5997e5adc065bd5 Mon Sep 17 00:00:00 2001 From: Jeff Huff Date: Mon, 3 Jul 2023 11:42:52 -0700 Subject: [PATCH 2/2] Fix swfitlint warnings --- Sources/RateLimit.swift | 4 ++-- Tests/RateLimitTests.swift | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Sources/RateLimit.swift b/Sources/RateLimit.swift index ed127776..5b3125e7 100644 --- a/Sources/RateLimit.swift +++ b/Sources/RateLimit.swift @@ -10,10 +10,10 @@ import Foundation public struct RateLimit: Codable { /// Rate limit entries public let entries: [String: Int] - + /// The url of the request the rate limit were returned for. public let requestURL: URL? - + init(value: String, requestURL: URL?) { self.requestURL = requestURL diff --git a/Tests/RateLimitTests.swift b/Tests/RateLimitTests.swift index a7e3e2dd..26e03edc 100644 --- a/Tests/RateLimitTests.swift +++ b/Tests/RateLimitTests.swift @@ -5,8 +5,8 @@ // Created by Mathias Emil Mortensen on 05/04/2023. // -import XCTest @testable import AppStoreConnect_Swift_SDK +import XCTest final class RateLimitTests: XCTestCase { @@ -16,20 +16,22 @@ final class RateLimitTests: XCTestCase { XCTAssertEqual(rateLimit.entries["user-hour-lim"], 3600) XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3545) } - + func testInvalidValue() { let rateLimit = RateLimit(value: "user-hour-rem3545", requestURL: nil) XCTAssertEqual(rateLimit.entries.count, 0) } - + func testPartialInvalidValue() { let rateLimit = RateLimit(value: "user-hour-rem3545;user-hour-rem:3545;", requestURL: nil) XCTAssertEqual(rateLimit.entries.count, 1) XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3545) } - + func testMoreItems() { - let rateLimit = RateLimit(value: "user-hour-lim:3600;user-hour-rem:3598;user-minute-lim:150;user-minute-rem:149;", requestURL: nil) + let rateLimit = RateLimit( + value: "user-hour-lim:3600;user-hour-rem:3598;user-minute-lim:150;user-minute-rem:149;", + requestURL: nil) XCTAssertEqual(rateLimit.entries.count, 4) XCTAssertEqual(rateLimit.entries["user-hour-lim"], 3600) XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3598) @@ -37,8 +39,10 @@ final class RateLimitTests: XCTestCase { XCTAssertEqual(rateLimit.entries["user-minute-rem"], 149) } - func testMultipleItems() { - let rateLimit = RateLimit(value: "user-hour-lim:3600;user-hour-rem:3598;, user-minute-lim:150;user-minute-rem:149;", requestURL: nil) + func testMultipleHeaders() { + let rateLimit = RateLimit( + value: "user-hour-lim:3600;user-hour-rem:3598;, user-minute-lim:150;user-minute-rem:149;", + requestURL: nil) XCTAssertEqual(rateLimit.entries.count, 4) XCTAssertEqual(rateLimit.entries["user-hour-lim"], 3600) XCTAssertEqual(rateLimit.entries["user-hour-rem"], 3598) @@ -47,7 +51,9 @@ final class RateLimitTests: XCTestCase { } func testExtractingHeaderField() { - let httpUrlResponse = HTTPURLResponse(url: URL(string: "https://api.appstoreconnect.apple.com/")!, statusCode: 200, httpVersion: nil, headerFields: ["X-ratE-limiT": "user-hour-lim:3600;user-hour-rem:3545;"]) + let httpUrlResponse = HTTPURLResponse(url: URL(string: "https://api.appstoreconnect.apple.com/")!, + statusCode: 200, httpVersion: nil, + headerFields: ["X-ratE-limiT": "user-hour-lim:3600;user-hour-rem:3545;"]) let rateLimit = httpUrlResponse?.rateLimit XCTAssertNotNil(rateLimit) if let rateLimit {