File tree 2 files changed +34
-4
lines changed
2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change 1
1
import Foundation
2
2
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 ] {
4
9
guard let components = URLComponents ( url: url, resolvingAgainstBaseURL: false ) else {
5
10
return [ ]
6
11
}
@@ -11,14 +16,14 @@ func extractParams(from url: URL) -> [(name: String, value: String)] {
11
16
12
17
if let queryItems = components. queryItems {
13
18
return queryItems. map {
14
- ( name: $0. name, value: $0. value ?? " " )
19
+ Params ( name: $0. name, value: $0. value ?? " " )
15
20
}
16
21
}
17
22
18
23
return [ ]
19
24
}
20
25
21
- func extractParams( from fragment: String ) -> [ ( name : String , value : String ) ] {
26
+ func extractParams( from fragment: String ) -> [ Params ] {
22
27
let components =
23
28
fragment
24
29
. split ( separator: " & " )
@@ -28,7 +33,7 @@ func extractParams(from fragment: String) -> [(name: String, value: String)] {
28
33
components
29
34
. compactMap {
30
35
$0. count == 2
31
- ? ( name: String ( $0 [ 0 ] ) , value: String ( $0 [ 1 ] ) )
36
+ ? Params ( name: String ( $0 [ 0 ] ) , value: String ( $0 [ 1 ] ) )
32
37
: nil
33
38
}
34
39
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments