Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RateLimit parsing for Multiple values #236

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Sources/RateLimit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ 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

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
Expand Down
29 changes: 22 additions & 7 deletions Tests/RateLimitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -16,29 +16,44 @@ 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)
XCTAssertEqual(rateLimit.entries["user-minute-lim"], 150)
XCTAssertEqual(rateLimit.entries["user-minute-rem"], 149)
}


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)
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 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 {
Expand Down