Skip to content

Commit 2d9c1d5

Browse files
authored
Move to localhost (#368)
Co-authored-by: Krish <>
1 parent 91f128b commit 2d9c1d5

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

.builder/actions/localhost_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ def start(self, env):
4949
def run(self, env):
5050
self.start(env)
5151
env.shell.setenv('AWS_CRT_MEMORY_TRACING', '2')
52+
actions = []
5253

53-
if os.system("env AWS_CRT_LOCALHOST=true swift test --filter 'HTTPTests|HTTP2ClientConnectionTests'"):
54+
if os.system("env AWS_CRT_LOCALHOST=true swift test --filter 'HTTPTests|HTTP2ClientConnectionTests|HTTP2StreamManagerTests'"):
5455
# Failed
5556
actions.append("exit 1")
5657

5758
# kill servers
5859
env.shell.exec("pkill", "-f", "h2tls_mock_server.py")
5960
env.shell.exec("pkill", "-f", "h2non_tls_server.py")
6061
env.shell.exec("pkill", "-f", "h11mock_server.py")
62+
63+
return Builder.Script(actions, name='localhost-test')

Test/AwsCommonRuntimeKitTests/http/HTTP2ClientConnectionTests.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,17 @@ class HTTP2ClientConnectionTests: XCBaseTestCase {
155155
}
156156

157157
func testHTTP2StreamUpload() async throws {
158+
try skipIfLocalhostUnavailable()
158159
let connectionManager = try await HTTPClientTestFixture.getHttpConnectionManager(
159-
endpoint: "nghttp2.org", alpnList: ["h2"])
160+
endpoint: host, port: port, alpnList: ["h2"])
160161
let semaphore = TestSemaphore(value: 0)
161162
var httpResponse = HTTPResponse()
162163
var onCompleteCalled = false
163164
let testBody = "testBody"
164165
let http2RequestOptions = try HTTPClientTestFixture.getHTTP2RequestOptions(
165166
method: "PUT",
166-
path: "/httpbin/put",
167-
authority: "nghttp2.org",
167+
path: "/echo",
168+
authority: host,
168169
body: testBody,
169170
response: &httpResponse,
170171
semaphore: semaphore,
@@ -192,12 +193,12 @@ class HTTP2ClientConnectionTests: XCBaseTestCase {
192193
XCTAssertNil(httpResponse.error)
193194
XCTAssertEqual(httpResponse.statusCode, 200)
194195

195-
// Parse json body
196+
// Parse json body - localhost echo server returns {"body": "...", "bytes": N}
196197
struct Response: Codable {
197-
let data: String
198+
let body: String
198199
}
199200

200201
let body: Response = try! JSONDecoder().decode(Response.self, from: httpResponse.body)
201-
XCTAssertEqual(body.data, testBody + HTTPClientTestFixture.TEST_DOC_LINE)
202+
XCTAssertEqual(body.body, testBody + HTTPClientTestFixture.TEST_DOC_LINE)
202203
}
203204
}

Test/AwsCommonRuntimeKitTests/http/HTTP2StreamManagerTests.swift

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import XCTest
55

66
@testable import AwsCommonRuntimeKit
77

8-
class HTT2StreamManagerTests: XCBaseTestCase {
8+
class HTTP2StreamManagerTests: XCBaseTestCase {
99
let endpoint = "d1cz66xoahf9cl.cloudfront.net" // Use cloudfront for HTTP/2
1010
let path = "/random_32_byte.data"
11-
let host = "nghttp2.org"
11+
let host = "localhost"
12+
let port = 3443
1213

1314
func testStreamManagerCreate() throws {
1415
let tlsContextOptions = TLSContextOptions()
@@ -26,7 +27,7 @@ class HTT2StreamManagerTests: XCBaseTestCase {
2627
hostName: endpoint,
2728
port: port,
2829
maxConnections: 30,
29-
proxyOptions: HTTPProxyOptions(hostName: "localhost", port: 80),
30+
proxyOptions: HTTPProxyOptions(hostName: host, port: 80),
3031
proxyEnvSettings: HTTPProxyEnvSettings(proxyConnectionType: HTTPProxyConnectionType.forward),
3132
socketOptions: SocketOptions(socketType: .stream),
3233
tlsOptions: tlsConnectionOptions,
@@ -75,6 +76,7 @@ class HTT2StreamManagerTests: XCBaseTestCase {
7576
func makeStreamManger(host: String, port: Int = 443) throws -> HTTP2StreamManager {
7677
let tlsContextOptions = TLSContextOptions()
7778
tlsContextOptions.setAlpnList(["h2"])
79+
tlsContextOptions.setVerifyPeer(false)
7880
let tlsContext = try TLSContext(options: tlsContextOptions, mode: .client)
7981

8082
var tlsConnectionOptions = TLSConnectionOptions(context: tlsContext)
@@ -89,7 +91,6 @@ class HTT2StreamManagerTests: XCBaseTestCase {
8991
hostResolver: hostResolver)
9092

9193
let socketOptions = SocketOptions(socketType: .stream)
92-
let port = UInt32(443)
9394
let streamManager = try HTTP2StreamManager(
9495
options: HTTP2StreamManagerOptions(
9596
clientBootstrap: bootstrap,
@@ -112,15 +113,16 @@ class HTT2StreamManagerTests: XCBaseTestCase {
112113
}
113114

114115
func testHTTP2StreamUpload() async throws {
115-
let streamManager = try makeStreamManger(host: host)
116+
try skipIfLocalhostUnavailable()
117+
let streamManager = try makeStreamManger(host: host, port: port)
116118
let semaphore = TestSemaphore(value: 0)
117119
var httpResponse = HTTPResponse()
118120
var onCompleteCalled = false
119121
let testBody = "testBody"
120122
let http2RequestOptions = try HTTPClientTestFixture.getHTTP2RequestOptions(
121123
method: "PUT",
122-
path: "/httpbin/put",
123-
authority: "nghttp2.org",
124+
path: "/echo",
125+
authority: host,
124126
body: testBody,
125127
response: &httpResponse,
126128
semaphore: semaphore,
@@ -150,38 +152,40 @@ class HTT2StreamManagerTests: XCBaseTestCase {
150152
XCTAssertNil(httpResponse.error)
151153
XCTAssertEqual(httpResponse.statusCode, 200)
152154

153-
// Parse json body
155+
// Parse json body - localhost echo server returns {"body": "...", "bytes": N}
154156
struct Response: Codable {
155-
let data: String
157+
let body: String
156158
}
157159

158160
let body: Response = try! JSONDecoder().decode(Response.self, from: httpResponse.body)
159-
XCTAssertEqual(body.data, testBody + HTTPClientTestFixture.TEST_DOC_LINE)
161+
XCTAssertEqual(body.body, testBody + HTTPClientTestFixture.TEST_DOC_LINE)
160162
}
161163

162164
// Test that the binding works not the actual functionality. C part has tests for functionality
163165
func testHTTP2StreamReset() async throws {
166+
try skipIfLocalhostUnavailable()
164167
let streamManager = try makeStreamManger(host: endpoint)
165168
let http2RequestOptions = try HTTPClientTestFixture.getHTTP2RequestOptions(
166169
method: "PUT",
167-
path: "/httpbin/put",
168-
authority: "nghttp2.org")
170+
path: "/echo",
171+
authority: host)
169172

170173
let stream = try await streamManager.acquireStream(requestOptions: http2RequestOptions)
171174
try stream.resetStream(error: HTTP2Error.internalError)
172175
}
173176

174177
func testHTTP2ParallelStreams() async throws {
178+
try skipIfLocalhostUnavailable()
175179
try await testHTTP2ParallelStreams(count: 10)
176180
}
177181

178182
func testHTTP2ParallelStreams(count: Int) async throws {
179-
let streamManager = try makeStreamManger(host: host)
183+
let streamManager = try makeStreamManger(host: host, port: port)
180184
return await withTaskGroup(of: Void.self) { taskGroup in
181185
for _ in 1...count {
182186
taskGroup.addTask {
183187
_ = try! await HTTPClientTestFixture.sendHTTP2Request(
184-
method: "GET", path: "/httpbin/get", authority: "nghttp2.org",
188+
method: "GET", path: "/echo", authority: "localhost",
185189
streamManager: streamManager)
186190
}
187191
}

0 commit comments

Comments
 (0)