-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow retrieval of metadata from
NWConnection
(#163)
* Allow retrieval of metadata for a specific protocol from the underlying `NWConnection` * Address review comments * Address review comment * Add availability annotation to tests * Rename `NIOTSChannelIsNotATransportServicesChannel ` to `NIOTSChannelIsNotANIOTSConnectionChannel `
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Tests/NIOTransportServicesTests/NIOTSChannelMetadataTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if canImport(Network) | ||
import Network | ||
import NIOCore | ||
import XCTest | ||
@testable import NIOTransportServices | ||
|
||
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) | ||
final class NIOTSChannelMetadataTests: XCTestCase { | ||
func testThrowsIfCalledOnWrongChannel() throws { | ||
let eventLoopGroup = NIOTSEventLoopGroup() | ||
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } | ||
let listenerBootsrap = NIOTSListenerBootstrap(group: eventLoopGroup) | ||
let listenerChannel = try listenerBootsrap.bind(host: "localhost", port: 0).wait() | ||
defer { XCTAssertNoThrow(try listenerChannel.close().wait()) } | ||
|
||
XCTAssertThrowsError(try listenerChannel.getMetadata(definition: NWProtocolTLS.definition).wait()) { error in | ||
XCTAssertTrue(error is NIOTSChannelIsNotANIOTSConnectionChannel, "unexpected error \(error)") | ||
} | ||
try! listenerChannel.eventLoop.submit { | ||
XCTAssertThrowsError(try listenerChannel.getMetadataSync(definition: NWProtocolTLS.definition)) { error in | ||
XCTAssertTrue(error is NIOTSChannelIsNotANIOTSConnectionChannel, "unexpected error \(error)") | ||
} | ||
}.wait() | ||
|
||
} | ||
func testThowsIfCalledOnANonInitializedChannel() { | ||
let eventLoopGroup = NIOTSEventLoopGroup() | ||
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } | ||
let channel = NIOTSConnectionChannel(eventLoop: eventLoopGroup.next() as! NIOTSEventLoop, tcpOptions: .init(), tlsOptions: .init()) | ||
XCTAssertThrowsError(try channel.getMetadata(definition: NWProtocolTLS.definition).wait()) { error in | ||
XCTAssertTrue(error is NIOTSConnectionNotInitialized, "unexpected error \(error)") | ||
} | ||
try! channel.eventLoop.submit { | ||
XCTAssertThrowsError(try channel.getMetadataSync(definition: NWProtocolTLS.definition)) { error in | ||
XCTAssertTrue(error is NIOTSConnectionNotInitialized, "unexpected error \(error)") | ||
} | ||
}.wait() | ||
} | ||
} | ||
#endif |