|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the AsyncHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +/* NOT @testable */ import AsyncHTTPClient // Tests that need @testable go into HTTPClientInternalTests.swift |
| 16 | +import Logging |
| 17 | +import NIO |
| 18 | +import NIOSOCKS |
| 19 | +import XCTest |
| 20 | + |
| 21 | +class HTTPClientSOCKSTests: XCTestCase { |
| 22 | + typealias Request = HTTPClient.Request |
| 23 | + |
| 24 | + var clientGroup: EventLoopGroup! |
| 25 | + var serverGroup: EventLoopGroup! |
| 26 | + var defaultHTTPBin: HTTPBin! |
| 27 | + var defaultClient: HTTPClient! |
| 28 | + var backgroundLogStore: CollectEverythingLogHandler.LogStore! |
| 29 | + |
| 30 | + var defaultHTTPBinURLPrefix: String { |
| 31 | + return "http://localhost:\(self.defaultHTTPBin.port)/" |
| 32 | + } |
| 33 | + |
| 34 | + override func setUp() { |
| 35 | + XCTAssertNil(self.clientGroup) |
| 36 | + XCTAssertNil(self.serverGroup) |
| 37 | + XCTAssertNil(self.defaultHTTPBin) |
| 38 | + XCTAssertNil(self.defaultClient) |
| 39 | + XCTAssertNil(self.backgroundLogStore) |
| 40 | + |
| 41 | + self.clientGroup = getDefaultEventLoopGroup(numberOfThreads: 1) |
| 42 | + self.serverGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 43 | + self.defaultHTTPBin = HTTPBin() |
| 44 | + self.backgroundLogStore = CollectEverythingLogHandler.LogStore() |
| 45 | + var backgroundLogger = Logger(label: "\(#function)", factory: { _ in |
| 46 | + CollectEverythingLogHandler(logStore: self.backgroundLogStore!) |
| 47 | + }) |
| 48 | + backgroundLogger.logLevel = .trace |
| 49 | + self.defaultClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), |
| 50 | + backgroundActivityLogger: backgroundLogger) |
| 51 | + } |
| 52 | + |
| 53 | + override func tearDown() { |
| 54 | + if let defaultClient = self.defaultClient { |
| 55 | + XCTAssertNoThrow(try defaultClient.syncShutdown()) |
| 56 | + self.defaultClient = nil |
| 57 | + } |
| 58 | + |
| 59 | + XCTAssertNotNil(self.defaultHTTPBin) |
| 60 | + XCTAssertNoThrow(try self.defaultHTTPBin.shutdown()) |
| 61 | + self.defaultHTTPBin = nil |
| 62 | + |
| 63 | + XCTAssertNotNil(self.clientGroup) |
| 64 | + XCTAssertNoThrow(try self.clientGroup.syncShutdownGracefully()) |
| 65 | + self.clientGroup = nil |
| 66 | + |
| 67 | + XCTAssertNotNil(self.serverGroup) |
| 68 | + XCTAssertNoThrow(try self.serverGroup.syncShutdownGracefully()) |
| 69 | + self.serverGroup = nil |
| 70 | + |
| 71 | + XCTAssertNotNil(self.backgroundLogStore) |
| 72 | + self.backgroundLogStore = nil |
| 73 | + } |
| 74 | + |
| 75 | + func testProxySOCKS() throws { |
| 76 | + let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!") |
| 77 | + let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), |
| 78 | + configuration: .init(proxy: .socksServer(host: "localhost"))) |
| 79 | + |
| 80 | + defer { |
| 81 | + XCTAssertNoThrow(try localClient.syncShutdown()) |
| 82 | + XCTAssertNoThrow(try socksBin.shutdown()) |
| 83 | + } |
| 84 | + |
| 85 | + var response: HTTPClient.Response? |
| 86 | + XCTAssertNoThrow(response = try localClient.get(url: "http://localhost/socks/test").wait()) |
| 87 | + XCTAssertEqual(.ok, response?.status) |
| 88 | + XCTAssertEqual(ByteBuffer(string: "it works!"), response?.body) |
| 89 | + } |
| 90 | + |
| 91 | + func testProxySOCKSBogusAddress() throws { |
| 92 | + let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), |
| 93 | + configuration: .init(proxy: .socksServer(host: "127.0.."))) |
| 94 | + |
| 95 | + defer { |
| 96 | + XCTAssertNoThrow(try localClient.syncShutdown()) |
| 97 | + } |
| 98 | + XCTAssertThrowsError(try localClient.get(url: "http://localhost/socks/test").wait()) |
| 99 | + } |
| 100 | + |
| 101 | + // there is no socks server, so we should fail |
| 102 | + func testProxySOCKSFailureNoServer() throws { |
| 103 | + let localHTTPBin = HTTPBin() |
| 104 | + let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), |
| 105 | + configuration: .init(proxy: .socksServer(host: "localhost", port: localHTTPBin.port))) |
| 106 | + defer { |
| 107 | + XCTAssertNoThrow(try localClient.syncShutdown()) |
| 108 | + XCTAssertNoThrow(try localHTTPBin.shutdown()) |
| 109 | + } |
| 110 | + XCTAssertThrowsError(try localClient.get(url: "http://localhost/socks/test").wait()) |
| 111 | + } |
| 112 | + |
| 113 | + // speak to a server that doesn't speak SOCKS |
| 114 | + func testProxySOCKSFailureInvalidServer() throws { |
| 115 | + let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), |
| 116 | + configuration: .init(proxy: .socksServer(host: "localhost"))) |
| 117 | + defer { |
| 118 | + XCTAssertNoThrow(try localClient.syncShutdown()) |
| 119 | + } |
| 120 | + XCTAssertThrowsError(try localClient.get(url: "http://localhost/socks/test").wait()) |
| 121 | + } |
| 122 | + |
| 123 | + // test a handshake failure with a misbehaving server |
| 124 | + func testProxySOCKSMisbehavingServer() throws { |
| 125 | + let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!", misbehave: true) |
| 126 | + let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), |
| 127 | + configuration: .init(proxy: .socksServer(host: "localhost"))) |
| 128 | + |
| 129 | + defer { |
| 130 | + XCTAssertNoThrow(try localClient.syncShutdown()) |
| 131 | + XCTAssertNoThrow(try socksBin.shutdown()) |
| 132 | + } |
| 133 | + |
| 134 | + // the server will send a bogus message in response to the clients request |
| 135 | + XCTAssertThrowsError(try localClient.get(url: "http://localhost/socks/test").wait()) |
| 136 | + } |
| 137 | +} |
0 commit comments