@@ -10,6 +10,7 @@ class StreamTests: XCTestCase {
1010 ( " testTCPInternetSocket " , testTCPInternetSocket) ,
1111 ( " testDirect " , testDirect) ,
1212 ( " testTCPInternetSocketThrows " , testTCPInternetSocketThrows) ,
13+ ( " testTCPInternetSocketThrowsClosedError " , testTCPInternetSocketThrowsClosedError) ,
1314 ( " testTCPServer " , testTCPServer) ,
1415 ]
1516
@@ -56,20 +57,34 @@ class StreamTests: XCTestCase {
5657 hostname: " google.com " ,
5758 port: 80
5859 )
59-
60- do {
61- _ = try google. write ( " GET / \r \n \r \n " . makeBytes ( ) )
62- XCTFail ( " should throw -- not connected " )
63- } catch {
64- // pass
65- }
66-
67- do {
68- _ = try google. read ( max: 2048 )
69- XCTFail ( " should throw -- not connected " )
70- } catch {
71- // pass
60+
61+ XCTAssertThrowsError ( _ = try google. write ( " GET / \r \n \r \n " . makeBytes ( ) ) , " should throw -- not connected " )
62+ XCTAssertThrowsError ( _ = try google. read ( max: 2048 ) , " should throw -- not connected " )
63+ }
64+
65+ func testTCPInternetSocketThrowsClosedError( ) throws {
66+ let httpBin = try TCPInternetSocket (
67+ scheme: " http " ,
68+ hostname: " httpbin.org " ,
69+ port: 80
70+ )
71+ try httpBin. setTimeout ( 10 )
72+ try httpBin. connect ( )
73+ try httpBin. close ( )
74+
75+ let errorCheck = { ( error: Error ) -> Void in
76+ guard let socketError = error as? SocketsError else {
77+ XCTFail ( " thrown error must be a SocketsError " )
78+ return
79+ }
80+ guard case . socketIsClosed = socketError. type else {
81+ XCTFail ( " thrown error must be specifically a .socketIsClosed error " )
82+ return
83+ }
7284 }
85+
86+ XCTAssertThrowsError ( _ = try httpBin. write ( " GET / \r \n \r \n " ) , " should throw -- not connected " , errorCheck)
87+ XCTAssertThrowsError ( _ = try httpBin. read ( max: 2048 ) , " should throw -- not connected " , errorCheck)
7388 }
7489
7590
0 commit comments