@@ -70,12 +70,19 @@ final class HTTPClientTracingTests: XCTestCaseHTTPClientTestsBaseClass {
7070 XCTFail ( " Still active spans which were not finished ( \( tracer. activeSpans. count) )! \( tracer. activeSpans) " )
7171 return
7272 }
73- guard let span = tracer. finishedSpans. first else {
74- XCTFail ( " No span was recorded! " )
75- return
76- }
73+ let span = try XCTUnwrap ( tracer. finishedSpans. first, " No span was recorded! " )
7774
7875 XCTAssertEqual ( span. operationName, " GET " )
76+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. requestMethod) , " GET " )
77+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. urlScheme) , " http " )
78+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. urlPath) , " /echo-method " )
79+ XCTAssertNotNil ( span. attributes. get ( client. tracing. attributeKeys. serverAddress) )
80+ XCTAssertEqual (
81+ span. attributes. get ( client. tracing. attributeKeys. serverPort) ,
82+ SpanAttribute . int64 ( Int64 ( self . defaultHTTPBin. port) )
83+ )
84+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. fullUrl) , " \( url) " )
85+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. requestBodySize) , nil )
7986 }
8087
8188 func testTrace_post_sync( ) throws {
@@ -86,12 +93,14 @@ final class HTTPClientTracingTests: XCTestCaseHTTPClientTestsBaseClass {
8693 XCTFail ( " Still active spans which were not finished ( \( tracer. activeSpans. count) )! \( tracer. activeSpans) " )
8794 return
8895 }
89- guard let span = tracer. finishedSpans. first else {
90- XCTFail ( " No span was recorded! " )
91- return
92- }
96+ let span = try XCTUnwrap ( tracer. finishedSpans. first, " No span was recorded! " )
9397
9498 XCTAssertEqual ( span. operationName, " POST " )
99+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. requestMethod) , " POST " )
100+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. urlScheme) , " http " )
101+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. urlPath) , " /echo-method " )
102+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. fullUrl) , " \( url) " )
103+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. requestBodySize) , nil )
95104 }
96105
97106 func testTrace_post_sync_404_error( ) throws {
@@ -102,10 +111,7 @@ final class HTTPClientTracingTests: XCTestCaseHTTPClientTestsBaseClass {
102111 XCTFail ( " Still active spans which were not finished ( \( tracer. activeSpans. count) )! \( tracer. activeSpans) " )
103112 return
104113 }
105- guard let span = tracer. finishedSpans. first else {
106- XCTFail ( " No span was recorded! " )
107- return
108- }
114+ let span = try XCTUnwrap ( tracer. finishedSpans. first, " No span was recorded! " )
109115
110116 XCTAssertEqual ( span. operationName, " POST " )
111117 XCTAssertTrue ( span. errors. isEmpty, " Should have recorded error " )
@@ -121,12 +127,16 @@ final class HTTPClientTracingTests: XCTestCaseHTTPClientTestsBaseClass {
121127 XCTFail ( " Still active spans which were not finished ( \( tracer. activeSpans. count) )! \( tracer. activeSpans) " )
122128 return
123129 }
124- guard let span = tracer. finishedSpans. first else {
125- XCTFail ( " No span was recorded! " )
126- return
127- }
130+ let span = try XCTUnwrap ( tracer. finishedSpans. first, " No span was recorded! " )
128131
129132 XCTAssertEqual ( span. operationName, " GET " )
133+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. requestMethod) , " GET " )
134+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. urlScheme) , " http " )
135+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. urlPath) , " /echo-method " )
136+ XCTAssertNotNil ( span. attributes. get ( client. tracing. attributeKeys. serverAddress) )
137+ XCTAssertNotNil ( span. attributes. get ( client. tracing. attributeKeys. serverPort) )
138+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. fullUrl) , " \( url) " )
139+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. requestBodySize) , nil )
130140 }
131141
132142 func testTrace_execute_async_404_error( ) async throws {
@@ -138,13 +148,48 @@ final class HTTPClientTracingTests: XCTestCaseHTTPClientTestsBaseClass {
138148 XCTFail ( " Still active spans which were not finished ( \( tracer. activeSpans. count) )! \( tracer. activeSpans) " )
139149 return
140150 }
141- guard let span = tracer. finishedSpans. first else {
142- XCTFail ( " No span was recorded! " )
143- return
144- }
151+ let span = try XCTUnwrap ( tracer. finishedSpans. first, " No span was recorded! " )
145152
146153 XCTAssertEqual ( span. operationName, " GET " )
147154 XCTAssertTrue ( span. errors. isEmpty, " Should have recorded error " )
148155 XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. responseStatusCode) , 404 )
149156 }
157+
158+ func testTrace_record_request_body_size_async( ) async throws {
159+ let url = self . defaultHTTPBinURLPrefix + " echo-method "
160+ var request = HTTPClientRequest ( url: url)
161+ request. body = . bytes( ByteBuffer ( string: " test " ) )
162+ let _ = try await client. execute ( request, deadline: . distantFuture)
163+
164+ guard tracer. activeSpans. isEmpty else {
165+ XCTFail ( " Still active spans which were not finished ( \( tracer. activeSpans. count) )! \( tracer. activeSpans) " )
166+ return
167+ }
168+ let span = try XCTUnwrap ( tracer. finishedSpans. first, " No span was recorded! " )
169+
170+ XCTAssertEqual ( span. operationName, " GET " )
171+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. requestMethod) , " GET " )
172+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. urlPath) , " /echo-method " )
173+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. requestBodySize) , 4 )
174+ }
175+
176+ func testTrace_strips_credentials_from_full_url_async( ) async throws {
177+ let urlWithoutCredentials = self . defaultHTTPBinURLPrefix + " echo-method "
178+ let urlWithCredentials = urlWithoutCredentials. replacingOccurrences (
179+ of: " http:// " ,
180+ with: " http://user:password@ "
181+ )
182+ let request = HTTPClientRequest ( url: urlWithCredentials)
183+ let _ = try await client. execute ( request, deadline: . distantFuture)
184+
185+ guard tracer. activeSpans. isEmpty else {
186+ XCTFail ( " Still active spans which were not finished ( \( tracer. activeSpans. count) )! \( tracer. activeSpans) " )
187+ return
188+ }
189+ let span = try XCTUnwrap ( tracer. finishedSpans. first, " No span was recorded! " )
190+
191+ XCTAssertEqual ( span. operationName, " GET " )
192+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. urlPath) , " /echo-method " )
193+ XCTAssertEqual ( span. attributes. get ( client. tracing. attributeKeys. fullUrl) , " \( urlWithoutCredentials) " )
194+ }
150195}
0 commit comments