Skip to content

Commit 76d7514

Browse files
authored
test: add test for extractParams function (#213)
1 parent b80898c commit 76d7514

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

Sources/Auth/Internal/Helpers.swift

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import Foundation
22

3-
func extractParams(from url: URL) -> [(name: String, value: String)] {
3+
struct Params: Hashable {
4+
var name: String
5+
var value: String
6+
}
7+
8+
func extractParams(from url: URL) -> [Params] {
49
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
510
return []
611
}
@@ -11,14 +16,14 @@ func extractParams(from url: URL) -> [(name: String, value: String)] {
1116

1217
if let queryItems = components.queryItems {
1318
return queryItems.map {
14-
(name: $0.name, value: $0.value ?? "")
19+
Params(name: $0.name, value: $0.value ?? "")
1520
}
1621
}
1722

1823
return []
1924
}
2025

21-
func extractParams(from fragment: String) -> [(name: String, value: String)] {
26+
func extractParams(from fragment: String) -> [Params] {
2227
let components =
2328
fragment
2429
.split(separator: "&")
@@ -28,7 +33,7 @@ func extractParams(from fragment: String) -> [(name: String, value: String)] {
2833
components
2934
.compactMap {
3035
$0.count == 2
31-
? (name: String($0[0]), value: String($0[1]))
36+
? Params(name: String($0[0]), value: String($0[1]))
3237
: nil
3338
}
3439
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// ExtractParamsTests.swift
3+
//
4+
//
5+
// Created by Guilherme Souza on 23/12/23.
6+
//
7+
8+
@testable import Auth
9+
import XCTest
10+
11+
final class ExtractParamsTests: XCTestCase {
12+
func testExtractParamsInQuery() {
13+
let code = UUID().uuidString
14+
let url = URL(string: "io.supabase.flutterquickstart://login-callback/?code=\(code)")!
15+
let params = extractParams(from: url)
16+
XCTAssertEqual(params, [Params(name: "code", value: code)])
17+
}
18+
19+
func testExtractParamsInFragment() {
20+
let code = UUID().uuidString
21+
let url = URL(string: "io.supabase.flutterquickstart://login-callback/#code=\(code)")!
22+
let params = extractParams(from: url)
23+
XCTAssertEqual(params, [Params(name: "code", value: code)])
24+
}
25+
}

0 commit comments

Comments
 (0)