Skip to content

Commit

Permalink
Merge pull request #236 from instanceof/rate-limit-parsing
Browse files Browse the repository at this point in the history
Update RateLimit parsing for Multiple values
  • Loading branch information
AvdLee authored Jul 10, 2023
2 parents d0a314b + 8c730a6 commit 2d7d7eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
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

0 comments on commit 2d7d7eb

Please sign in to comment.