@@ -7,15 +7,10 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
77
88 // MARK: - Initialization Tests
99
10- func testInit_withJSONDictionary_shouldParseCorrectly( ) {
10+ func testInit_withJSONDictionary_shouldParseCorrectly( ) throws {
1111 // -- Arrange --
1212 let bodyContent : [ String : Any ] = [ " key " : " value " , " number " : 42 ]
13- let bodyData : Data
14- do {
15- bodyData = try JSONSerialization . data ( withJSONObject: bodyContent)
16- } catch {
17- return XCTFail ( " Failed to create JSON data: \( error) " )
18- }
13+ let bodyData = try JSONSerialization . data ( withJSONObject: bodyContent)
1914
2015 // -- Act --
2116 let body = Body ( data: bodyData, contentType: " application/json " )
@@ -30,15 +25,10 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
3025 }
3126 }
3227
33- func testInit_withJSONArray_shouldParseCorrectly( ) {
28+ func testInit_withJSONArray_shouldParseCorrectly( ) throws {
3429 // -- Arrange --
3530 let bodyContent = [ " item1 " , " item2 " , " item3 " ]
36- let bodyData : Data
37- do {
38- bodyData = try JSONSerialization . data ( withJSONObject: bodyContent)
39- } catch {
40- return XCTFail ( " Failed to create JSON data: \( error) " )
41- }
31+ let bodyData = try JSONSerialization . data ( withJSONObject: bodyContent)
4232
4333 // -- Act --
4434 let body = Body ( data: bodyData, contentType: " application/json " )
@@ -58,9 +48,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
5848 func testInit_withTextData_shouldStoreAsString( ) {
5949 // -- Arrange --
6050 let bodyContent = " This is plain text content "
61- guard let bodyData = bodyContent. data ( using: . utf8) else {
62- return XCTFail ( " Failed to create text data " )
63- }
51+ let bodyData = Data ( bodyContent. utf8)
6452
6553 // -- Act --
6654 let body = Body ( data: bodyData, contentType: " text/plain " )
@@ -85,9 +73,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
8573 func testInit_withInvalidJSON_shouldFallbackToString( ) {
8674 // -- Arrange --
8775 let invalidJSON = " { invalid json } "
88- guard let bodyData = invalidJSON. data ( using: . utf8) else {
89- return XCTFail ( " Failed to create data " )
90- }
76+ let bodyData = Data ( invalidJSON. utf8)
9177
9278 // -- Act --
9379 let body = Body ( data: bodyData, contentType: " application/json " )
@@ -102,12 +88,10 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
10288 XCTAssertTrue ( body? . warnings. contains ( . bodyParseError) == true )
10389 }
10490
105- func testInit_withLargeData_shouldTruncate( ) {
91+ func testInit_withLargeData_shouldTruncate( ) throws {
10692 // -- Arrange --
10793 let largeString = String ( repeating: " a " , count: 200_000 )
108- guard let bodyData = largeString. data ( using: . utf8) else {
109- return XCTFail ( " Failed to create large data " )
110- }
94+ let bodyData = Data ( largeString. utf8)
11195
11296 // -- Act --
11397 let body = Body ( data: bodyData, contentType: " text/plain " )
@@ -123,15 +107,12 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
123107 XCTAssertTrue ( body? . warnings. contains ( . textTruncated) == true )
124108
125109 // Check serialized warnings
126- let serialized = body? . serialize ( )
127- if let warningStrings = serialized ? [ " warnings " ] as? [ String ] {
128- XCTAssertEqual ( warningStrings, [ " TEXT_TRUNCATED " ] )
129- } else {
130- XCTFail ( " Expected warnings to be serialized as string array " )
131- }
110+ let serialized = try XCTUnwrap ( body? . serialize ( ) )
111+ let warningStrings = try XCTUnwrap ( serialized [ " warnings " ] as? [ String ] , " Expected warnings to be serialized as string array " )
112+ XCTAssertEqual ( warningStrings, [ " TEXT_TRUNCATED " ] )
132113 }
133114
134- func testInit_withBinaryContentType_shouldCreateArtificialString( ) {
115+ func testInit_withBinaryContentType_shouldCreateArtificialString( ) throws {
135116 // -- Arrange --
136117 let binaryData = Data ( [ 0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A ] ) // PNG header
137118 let contentType = " image/png "
@@ -149,12 +130,9 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
149130 XCTFail ( " Expected .text content with binary description " )
150131 }
151132
152- let result = body? . serialize ( )
153- if let bodyString = result ? [ " body " ] as? String {
154- XCTAssertTrue ( bodyString. hasPrefix ( " [Body not captured " ) )
155- } else {
156- XCTFail ( " Expected body to be a string with binary data prefix " )
157- }
133+ let result = try XCTUnwrap ( body? . serialize ( ) )
134+ let bodyString = try XCTUnwrap ( result [ " body " ] as? String , " Expected body to be a string with binary data prefix " )
135+ XCTAssertTrue ( bodyString. hasPrefix ( " [Body not captured " ) )
158136 }
159137
160138 func testInit_withNilContentType_shouldCreatePlaceholder( ) {
@@ -198,9 +176,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
198176 func testInit_withFormURLEncoded_shouldParseAsForm( ) {
199177 // -- Arrange --
200178 let formString = " key1=value1&key2=value2&key3=value%20with%20spaces "
201- guard let bodyData = formString. data ( using: . utf8) else {
202- return XCTFail ( " Failed to create form data " )
203- }
179+ let bodyData = Data ( formString. utf8)
204180
205181 // -- Act --
206182 let body = Body ( data: bodyData, contentType: " application/x-www-form-urlencoded " )
@@ -219,7 +195,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
219195 func testInit_withFormURLEncoded_duplicateKeys_shouldPromoteToArray( ) throws {
220196 // -- Act --
221197 let body = try XCTUnwrap ( Body (
222- data: " color=red&color=blue&color=green " . data ( using : . utf8) ! ,
198+ data: Data ( " color=red&color=blue&color=green " . utf8) ,
223199 contentType: " application/x-www-form-urlencoded; charset=utf-8 "
224200 ) )
225201
@@ -231,7 +207,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
231207 func testInit_withFormURLEncoded_emptyValue_shouldParseAsEmptyString( ) throws {
232208 // -- Act --
233209 let body = try XCTUnwrap ( Body (
234- data: " key1=&key2=value2 " . data ( using : . utf8) ! ,
210+ data: Data ( " key1=&key2=value2 " . utf8) ,
235211 contentType: " application/x-www-form-urlencoded; charset=utf-8 "
236212 ) )
237213
@@ -244,7 +220,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
244220 func testInit_withFormURLEncoded_missingEquals_shouldFallbackToText( ) throws {
245221 // -- Act --
246222 let body = try XCTUnwrap ( Body (
247- data: " key1=value1&malformed&key2=value2 " . data ( using : . utf8) ! ,
223+ data: Data ( " key1=value1&malformed&key2=value2 " . utf8) ,
248224 contentType: " application/x-www-form-urlencoded; charset=utf-8 "
249225 ) )
250226
@@ -259,7 +235,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
259235 func testInit_withFormURLEncoded_emptyKeys_shouldBeSkipped( ) throws {
260236 // -- Act --
261237 let body = try XCTUnwrap ( Body (
262- data: " =value1&key2=value2 " . data ( using : . utf8) ! ,
238+ data: Data ( " =value1&key2=value2 " . utf8) ,
263239 contentType: " application/x-www-form-urlencoded; charset=utf-8 "
264240 ) )
265241
@@ -272,7 +248,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
272248 func testInit_withFormURLEncoded_equalsInValue_shouldPreserve( ) throws {
273249 // -- Act --
274250 let body = try XCTUnwrap ( Body (
275- data: " query=a=1&token=abc " . data ( using : . utf8) ! ,
251+ data: Data ( " query=a=1&token=abc " . utf8) ,
276252 contentType: " application/x-www-form-urlencoded; charset=utf-8 "
277253 ) )
278254
@@ -285,7 +261,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
285261 func testInit_withFormURLEncoded_plusAsSpace_shouldDecode( ) throws {
286262 // -- Act --
287263 let body = try XCTUnwrap ( Body (
288- data: " greeting=hello+world&name=Jane+Doe " . data ( using : . utf8) ! ,
264+ data: Data ( " greeting=hello+world&name=Jane+Doe " . utf8) ,
289265 contentType: " application/x-www-form-urlencoded; charset=utf-8 "
290266 ) )
291267
@@ -299,7 +275,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
299275 // %ZZ is invalid percent-encoding → removingPercentEncoding returns nil.
300276 // The fallback should still preserve +-to-space and = in values.
301277 let body = try XCTUnwrap ( Body (
302- data: " key=a%ZZ=b&greeting=hello+world " . data ( using : . utf8) ! ,
278+ data: Data ( " key=a%ZZ=b&greeting=hello+world " . utf8) ,
303279 contentType: " application/x-www-form-urlencoded; charset=utf-8 "
304280 ) )
305281
@@ -319,7 +295,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
319295
320296 // -- dropLast(1): 3-byte char split after 2 bytes --
321297 // "你好" = 6 bytes; prefix(5) cuts second char after 2 of 3 bytes
322- let cjk = " 你好 " . data ( using : . utf8) !
298+ let cjk = Data ( " 你好 " . utf8)
323299 XCTAssertEqual ( cjk. count, 6 )
324300 let body1 = try XCTUnwrap ( Body ( data: cjk. prefix ( 5 ) , contentType: " text/plain; charset=utf-8 " ) )
325301 XCTAssertEqual ( body1. serialize ( ) [ " body " ] as? String , " 你 " )
@@ -331,7 +307,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
331307
332308 // -- dropLast(3): 4-byte emoji split after 1 byte --
333309 // "A😀" = 1 + 4 = 5 bytes; prefix(2) cuts emoji after 1 of 4 bytes
334- let emoji = " A😀 " . data ( using : . utf8) !
310+ let emoji = Data ( " A😀 " . utf8)
335311 XCTAssertEqual ( emoji. count, 5 )
336312 let body3 = try XCTUnwrap ( Body ( data: emoji. prefix ( 2 ) , contentType: " text/plain; charset=utf-8 " ) )
337313 XCTAssertEqual ( body3. serialize ( ) [ " body " ] as? String , " A " )
@@ -342,13 +318,13 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
342318 XCTAssertEqual ( body4. serialize ( ) [ " body " ] as? String , " 你 " )
343319
344320 // -- pure ASCII: never affected --
345- let ascii = " hello " . data ( using : . utf8) !
321+ let ascii = Data ( " hello " . utf8)
346322 let body5 = try XCTUnwrap ( Body ( data: ascii. prefix ( 3 ) , contentType: " text/plain; charset=utf-8 " ) )
347323 XCTAssertEqual ( body5. serialize ( ) [ " body " ] as? String , " hel " )
348324
349325 // -- 2-byte char split after 1 byte --
350326 // "Aé" = 1 + 2 = 3 bytes; prefix(2) cuts "é" after 1 of 2 bytes
351- let accented = " Aé " . data ( using : . utf8) !
327+ let accented = Data ( " Aé " . utf8)
352328 XCTAssertEqual ( accented. count, 3 )
353329 let body6 = try XCTUnwrap ( Body ( data: accented. prefix ( 2 ) , contentType: " text/plain; charset=utf-8 " ) )
354330 XCTAssertEqual ( body6. serialize ( ) [ " body " ] as? String , " A " )
@@ -359,9 +335,7 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
359335 func testSerialize_withStringBody_shouldReturnDictionary( ) {
360336 // -- Arrange --
361337 let bodyContent = " test body "
362- guard let bodyData = bodyContent. data ( using: . utf8) else {
363- return XCTFail ( " Failed to create data " )
364- }
338+ let bodyData = Data ( bodyContent. utf8)
365339 let body = Body ( data: bodyData, contentType: " text/plain " )
366340
367341 // -- Act --
@@ -372,58 +346,40 @@ class SentryReplayNetworkDetailsBodyTests: XCTestCase {
372346 XCTAssertEqual ( result ? [ " body " ] as? String , bodyContent)
373347 }
374348
375- func testSerialize_withJSONDictionary_shouldReturnDictionary( ) {
349+ func testSerialize_withJSONDictionary_shouldReturnDictionary( ) throws {
376350 // -- Arrange --
377351 let bodyContent : [ String : Any ] = [ " user " : " test " , " id " : 123 ]
378- let bodyData : Data
379- do {
380- bodyData = try JSONSerialization . data ( withJSONObject: bodyContent)
381- } catch {
382- return XCTFail ( " Failed to create JSON data: \( error) " )
383- }
352+ let bodyData = try JSONSerialization . data ( withJSONObject: bodyContent)
384353 let body = Body ( data: bodyData, contentType: " application/json " )
385354
386355 // -- Act --
387- let result = body? . serialize ( )
356+ let result = try XCTUnwrap ( body? . serialize ( ) )
388357
389358 // -- Assert --
390- XCTAssertNotNil ( result)
391- guard let bodyDict = result ? [ " body " ] as? NSDictionary else {
392- return XCTFail ( " Expected body to be NSDictionary " )
393- }
359+ let bodyDict = try XCTUnwrap ( result [ " body " ] as? NSDictionary , " Expected body to be NSDictionary " )
394360 XCTAssertEqual ( bodyDict [ " user " ] as? String , " test " )
395361 XCTAssertEqual ( bodyDict [ " id " ] as? Int , 123 )
396362 }
397363
398- func testSerialize_withJSONArray_shouldReturnArray( ) {
364+ func testSerialize_withJSONArray_shouldReturnArray( ) throws {
399365 // -- Arrange --
400366 let bodyContent = [ " item1 " , " item2 " , " item3 " ]
401- let bodyData : Data
402- do {
403- bodyData = try JSONSerialization . data ( withJSONObject: bodyContent)
404- } catch {
405- return XCTFail ( " Failed to create JSON data: \( error) " )
406- }
367+ let bodyData = try JSONSerialization . data ( withJSONObject: bodyContent)
407368 let body = Body ( data: bodyData, contentType: " application/json " )
408369
409370 // -- Act --
410- let result = body? . serialize ( )
371+ let result = try XCTUnwrap ( body? . serialize ( ) )
411372
412373 // -- Assert --
413- XCTAssertNotNil ( result)
414- guard let bodyArray = result ? [ " body " ] as? NSArray else {
415- return XCTFail ( " Expected body to be NSArray " )
416- }
374+ let bodyArray = try XCTUnwrap ( result [ " body " ] as? NSArray , " Expected body to be NSArray " )
417375 XCTAssertEqual ( bodyArray. count, 3 )
418376 XCTAssertEqual ( bodyArray [ 0 ] as? String , " item1 " )
419377 }
420378
421379 func testSerialize_withNoContentType_shouldCreatePlaceholder( ) {
422380 // -- Arrange --
423381 let bodyContent = " some content "
424- guard let bodyData = bodyContent. data ( using: . utf8) else {
425- return XCTFail ( " Failed to create data " )
426- }
382+ let bodyData = Data ( bodyContent. utf8)
427383 let body = Body ( data: bodyData, contentType: nil )
428384
429385 // -- Act --
0 commit comments