Skip to content

Commit ad33fad

Browse files
Merge pull request #2 from PassiveLogic/chore/sync-with-base
Chore: Pull base changes
2 parents d19e63c + 9d241fe commit ad33fad

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 PassiveLogic
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Sources/GraphQLTransportWS/Server.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class Server<InitPayload: Equatable & Codable> {
1313

1414
let onExecute: (GraphQLRequest) -> EventLoopFuture<GraphQLResult>
1515
let onSubscribe: (GraphQLRequest) -> EventLoopFuture<SubscriptionResult>
16-
17-
var auth: (InitPayload) throws -> Void = { _ in }
18-
var onExit: () -> Void = {}
16+
var auth: (InitPayload) throws -> EventLoopFuture<Void>
17+
18+
var onExit: () -> Void = { }
1919
var onOperationComplete: (String) -> Void = { _ in }
2020
var onOperationError: (String) -> Void = { _ in }
2121
var onMessage: (String) -> Void = { _ in }
@@ -33,14 +33,17 @@ public class Server<InitPayload: Equatable & Codable> {
3333
/// - messenger: The messenger to bind the server to.
3434
/// - onExecute: Callback run during `subscribe` resolution for non-streaming queries. Typically this is `API.execute`.
3535
/// - onSubscribe: Callback run during `subscribe` resolution for streaming queries. Typically this is `API.subscribe`.
36+
/// - eventLoop: EventLoop on which to perform server operations.
3637
public init(
3738
messenger: Messenger,
3839
onExecute: @escaping (GraphQLRequest) -> EventLoopFuture<GraphQLResult>,
39-
onSubscribe: @escaping (GraphQLRequest) -> EventLoopFuture<SubscriptionResult>
40+
onSubscribe: @escaping (GraphQLRequest) -> EventLoopFuture<SubscriptionResult>,
41+
eventLoop: EventLoop
4042
) {
4143
self.messenger = messenger
4244
self.onExecute = onExecute
4345
self.onSubscribe = onSubscribe
46+
self.auth = { _ in eventLoop.makeSucceededVoidFuture() }
4447

4548
messenger.onReceive { message in
4649
self.onMessage(message)
@@ -103,9 +106,9 @@ public class Server<InitPayload: Equatable & Codable> {
103106
}
104107

105108
/// Define the callback run during `connection_init` resolution that allows authorization using the `payload`.
106-
/// Throw to indicate that authorization has failed.
109+
/// Throw or fail the future to indicate that authorization has failed.
107110
/// - Parameter callback: The callback to assign
108-
public func auth(_ callback: @escaping (InitPayload) throws -> Void) {
111+
public func auth(_ callback: @escaping (InitPayload) throws -> EventLoopFuture<Void>) {
109112
self.auth = callback
110113
}
111114

@@ -150,14 +153,20 @@ public class Server<InitPayload: Equatable & Codable> {
150153
}
151154

152155
do {
153-
try self.auth(connectionInitRequest.payload)
156+
let authResult = try self.auth(connectionInitRequest.payload)
157+
authResult.whenSuccess {
158+
self.initialized = true
159+
self.sendConnectionAck()
160+
}
161+
authResult.whenFailure { error in
162+
self.error(.unauthorized())
163+
return
164+
}
154165
}
155166
catch {
156167
self.error(.unauthorized())
157168
return
158169
}
159-
initialized = true
160-
self.sendConnectionAck()
161170
}
162171

163172
private func onSubscribe(_ subscribeRequest: SubscribeRequest) {

Tests/GraphQLTransportWSTests/GraphQLTransportWSTests.swift

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ final class GraphqlTransportWSTests: XCTestCase {
3939
context: context,
4040
on: self.eventLoop
4141
)
42-
}
42+
},
43+
eventLoop: self.eventLoop
4344
)
4445
}
4546

@@ -73,8 +74,8 @@ final class GraphqlTransportWSTests: XCTestCase {
7374
}
7475

7576
/// Tests that throwing in the authorization callback forces an unauthorized error
76-
func testAuth() throws {
77-
server.auth { _ in
77+
func testAuthWithThrow() throws {
78+
server.auth { payload in
7879
throw TestError.couldBeAnything
7980
}
8081

@@ -99,6 +100,34 @@ final class GraphqlTransportWSTests: XCTestCase {
99100
["\(ErrorCode.unauthorized): Unauthorized"]
100101
)
101102
}
103+
104+
/// Tests that failing a future in the authorization callback forces an unauthorized error
105+
func testAuthWithFailedFuture() throws {
106+
server.auth { payload in
107+
self.eventLoop.makeFailedFuture(TestError.couldBeAnything)
108+
}
109+
110+
var messages = [String]()
111+
let completeExpectation = XCTestExpectation()
112+
113+
let client = Client<TokenInitPayload>(messenger: clientMessenger)
114+
client.onMessage { message, _ in
115+
messages.append(message)
116+
completeExpectation.fulfill()
117+
}
118+
119+
client.sendConnectionInit(
120+
payload: TokenInitPayload(
121+
authToken: ""
122+
)
123+
)
124+
125+
wait(for: [completeExpectation], timeout: 2)
126+
XCTAssertEqual(
127+
messages,
128+
["\(ErrorCode.unauthorized): Unauthorized"]
129+
)
130+
}
102131

103132
/// Tests a single-op conversation
104133
func testSingleOp() throws {

0 commit comments

Comments
 (0)