1
+ import Mocker
1
2
import XCTest
2
3
3
4
@testable import Functions
@@ -6,12 +7,15 @@ final class FunctionsClientTests: XCTestCase {
6
7
let url = URL ( string: " http://localhost:5432/functions/v1 " ) !
7
8
let apiKey = " supabase.anon.key "
8
9
10
+ lazy var sut = FunctionsClient ( url: url, headers: [ " apikey " : apiKey] )
11
+
9
12
func testInvoke( ) async throws {
13
+ let url = URL ( string: " http://localhost:5432/functions/v1/hello_world " ) !
10
14
var _request : URLRequest ?
11
- let sut = FunctionsClient ( url : url , headers : [ " apikey " : apiKey ] ) {
12
- _request = $0
13
- return ( Data ( ) , HTTPURLResponse . mock ( url : self . url ) )
14
- }
15
+
16
+ var mock = Mock ( url : url , dataType : . json , statusCode : 200 , data : [ . post : Data ( ) ] )
17
+ mock . onRequestHandler = . init { _request = $0 }
18
+ mock . register ( )
15
19
16
20
let body = [ " name " : " Supabase " ]
17
21
@@ -22,18 +26,22 @@ final class FunctionsClientTests: XCTestCase {
22
26
23
27
let request = try XCTUnwrap ( _request)
24
28
25
- XCTAssertEqual ( request. url, URL ( string : " http://localhost:5432/functions/v1/hello_world " ) )
29
+ XCTAssertEqual ( request. url, url )
26
30
XCTAssertEqual ( request. httpMethod, " POST " )
27
31
XCTAssertEqual ( request. value ( forHTTPHeaderField: " apikey " ) , apiKey)
28
32
XCTAssertEqual ( request. value ( forHTTPHeaderField: " X-Custom-Key " ) , " value " )
29
33
XCTAssertEqual (
30
- request. value ( forHTTPHeaderField: " X-Client-Info " ) , " functions-swift/ \( Functions . version) " )
34
+ request. value ( forHTTPHeaderField: " X-Client-Info " ) ,
35
+ " functions-swift/ \( Functions . version) "
36
+ )
31
37
}
32
38
33
39
func testInvoke_shouldThrow_URLError_badServerResponse( ) async {
34
- let sut = FunctionsClient ( url: url) { _ in
35
- ( Data ( ) , URLResponse ( ) )
36
- }
40
+ let url = URL ( string: " http://localhost:5432/functions/v1/hello_world " ) !
41
+ let mock = Mock (
42
+ url: url, dataType: . json, statusCode: 200 , data: [ . post: Data ( ) ] ,
43
+ requestError: URLError ( . badServerResponse) )
44
+ mock. register ( )
37
45
38
46
do {
39
47
try await sut. invoke ( functionName: " hello_world " )
@@ -45,12 +53,10 @@ final class FunctionsClientTests: XCTestCase {
45
53
}
46
54
47
55
func testInvoke_shouldThrow_FunctionsError_httpError( ) async {
48
- let sut = FunctionsClient ( url: url) { _ in
49
- (
50
- " error " . data ( using: . utf8) !,
51
- HTTPURLResponse . mock ( url: self . url, statusCode: 300 )
52
- )
53
- }
56
+ let url = URL ( string: " http://localhost:5432/functions/v1/hello_world " ) !
57
+ let mock = Mock (
58
+ url: url, dataType: . json, statusCode: 300 , data: [ . post: " error " . data ( using: . utf8) !] )
59
+ mock. register ( )
54
60
55
61
do {
56
62
try await sut. invoke ( functionName: " hello_world " )
@@ -64,12 +70,11 @@ final class FunctionsClientTests: XCTestCase {
64
70
}
65
71
66
72
func testInvoke_shouldThrow_FunctionsError_relayError( ) async {
67
- let sut = FunctionsClient ( url: url) { _ in
68
- (
69
- Data ( ) ,
70
- HTTPURLResponse . mock ( url: self . url, headerFields: [ " x-relay-error " : " true " ] )
71
- )
72
- }
73
+ let url = URL ( string: " http://localhost:5432/functions/v1/hello_world " ) !
74
+ let mock = Mock (
75
+ url: url, dataType: . json, statusCode: 200 , data: [ . post: Data ( ) ] ,
76
+ additionalHeaders: [ " x-relay-error " : " true " ] )
77
+ mock. register ( )
73
78
74
79
do {
75
80
try await sut. invoke ( functionName: " hello_world " )
@@ -79,14 +84,10 @@ final class FunctionsClientTests: XCTestCase {
79
84
XCTFail ( " Unexpected error thrown \( error) " )
80
85
}
81
86
}
82
- }
83
87
84
- extension HTTPURLResponse {
85
- static func mock(
86
- url: URL ,
87
- statusCode: Int = 200 ,
88
- headerFields: [ String : String ] ? = nil
89
- ) -> HTTPURLResponse {
90
- HTTPURLResponse ( url: url, statusCode: statusCode, httpVersion: nil , headerFields: headerFields) !
88
+ func test_setAuth( ) async {
89
+ await sut. setAuth ( token: " access.token " )
90
+ let headers = await sut. headers
91
+ XCTAssertEqual ( headers [ " Authorization " ] , " Bearer access.token " )
91
92
}
92
93
}
0 commit comments