@@ -12,60 +12,73 @@ import AwsCommonRuntimeKit
12
12
13
13
final class StreamableHttpBodyTests : XCTestCase {
14
14
15
- let testData = Data ( [ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A ] )
15
+ let testData = Data ( [
16
+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A ,
17
+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A
18
+ ] )
16
19
17
- func testRead( ) throws {
18
- let sut = StreamableHttpBody ( body: . data( testData) )
20
+ func test_streamWholeData( ) throws {
21
+ try read ( data: Data ( testData [ ..< 11 ] ) )
22
+ try seek ( data: Data ( testData [ 11 ... ] ) )
23
+ }
24
+
25
+ func test_streamSlicedData( ) throws {
26
+ try read ( data: testData [ ..< 11 ] )
27
+ try seek ( data: testData [ 11 ... ] )
28
+ }
29
+
30
+ private func read( data: Data , file: StaticString = #file, line: UInt = #line) throws {
31
+ let sut = StreamableHttpBody ( body: . data( data) )
19
32
20
33
// read first 4 bytes
21
34
let buffer = UnsafeMutableBufferPointer< UInt8> . allocate( capacity: 4 )
22
35
let bytesRead1 = try sut. read ( buffer: buffer)
23
36
let bytes1 = Data ( bytes: buffer. baseAddress!, count: bytesRead1!)
24
- XCTAssertEqual ( bytesRead1, 4 )
25
- XCTAssertEqual ( bytes1, Data ( [ 0x00 , 0x01 , 0x02 , 0x03 ] ) )
37
+ XCTAssertEqual ( bytesRead1, 4 , file : file , line : line )
38
+ XCTAssertEqual ( bytes1, Data ( [ 0x00 , 0x01 , 0x02 , 0x03 ] ) , file : file , line : line )
26
39
27
40
// read next 4 bytes from current position
28
41
let bytesRead2 = try sut. read ( buffer: buffer)
29
42
let bytes2 = Data ( bytes: buffer. baseAddress!, count: bytesRead2!)
30
- XCTAssertEqual ( bytesRead2, 4 )
31
- XCTAssertEqual ( bytes2, Data ( [ 0x04 , 0x05 , 0x06 , 0x07 ] ) )
43
+ XCTAssertEqual ( bytesRead2, 4 , file : file , line : line )
44
+ XCTAssertEqual ( bytes2, Data ( [ 0x04 , 0x05 , 0x06 , 0x07 ] ) , file : file , line : line )
32
45
33
46
// read next 4 bytes from current position
34
47
// this should only read 3 bytes since we are at the end of the stream
35
48
let bytesRead3 = try sut. read ( buffer: buffer)
36
49
let bytes3 = Data ( bytes: buffer. baseAddress!, count: bytesRead3!)
37
- XCTAssertEqual ( bytesRead3, 3 )
38
- XCTAssertEqual ( bytes3, Data ( [ 0x08 , 0x09 , 0x0A ] ) )
50
+ XCTAssertEqual ( bytesRead3, 3 , file : file , line : line )
51
+ XCTAssertEqual ( bytes3, Data ( [ 0x08 , 0x09 , 0x0A ] ) , file : file , line : line )
39
52
40
53
// read next 4 bytes from current position
41
54
// this should return nil since we are at the end of the stream
42
55
let bytesRead4 = try sut. read ( buffer: buffer)
43
- XCTAssertNil ( bytesRead4)
56
+ XCTAssertNil ( bytesRead4, file : file , line : line )
44
57
}
45
58
46
- func testSeek ( ) throws {
47
- let sut = StreamableHttpBody ( body: . data( testData ) )
59
+ private func seek ( data : Data , file : StaticString = #file , line : UInt = #line ) throws {
60
+ let sut = StreamableHttpBody ( body: . data( data ) )
48
61
49
62
// read first 4 bytes
50
63
let buffer = UnsafeMutableBufferPointer< UInt8> . allocate( capacity: 4 )
51
64
let bytesRead1 = try sut. read ( buffer: buffer)
52
65
let bytes1 = Data ( bytes: buffer. baseAddress!, count: bytesRead1!)
53
- XCTAssertEqual ( bytesRead1!, 4 )
54
- XCTAssertEqual ( bytes1, Data ( [ 0x00 , 0x01 , 0x02 , 0x03 ] ) )
66
+ XCTAssertEqual ( bytesRead1!, 4 , file : file , line : line )
67
+ XCTAssertEqual ( bytes1, Data ( [ 0x00 , 0x01 , 0x02 , 0x03 ] ) , file : file , line : line )
55
68
56
69
// seek to offset 2 and read 4 bytes
57
70
try sut. seek ( offset: 2 , streamSeekType: . begin)
58
71
let bytesRead2 = try sut. read ( buffer: buffer)
59
72
let bytes2 = Data ( bytes: buffer. baseAddress!, count: bytesRead2!)
60
- XCTAssertEqual ( bytesRead2!, 4 )
61
- XCTAssertEqual ( bytes2, Data ( [ 0x02 , 0x03 , 0x04 , 0x05 ] ) )
73
+ XCTAssertEqual ( bytesRead2!, 4 , file : file , line : line )
74
+ XCTAssertEqual ( bytes2, Data ( [ 0x02 , 0x03 , 0x04 , 0x05 ] ) , file : file , line : line )
62
75
63
76
// seek to offset 8 and read 3 bytes
64
77
// this should only read 3 bytes since we are at the end of the stream
65
78
try sut. seek ( offset: 8 , streamSeekType: . begin)
66
79
let bytesRead3 = try sut. read ( buffer: buffer)
67
80
let bytes3 = Data ( bytes: buffer. baseAddress!, count: bytesRead3!)
68
- XCTAssertEqual ( bytesRead3!, 3 )
69
- XCTAssertEqual ( bytes3, Data ( [ 0x08 , 0x09 , 0x0A ] ) )
81
+ XCTAssertEqual ( bytesRead3!, 3 , file : file , line : line )
82
+ XCTAssertEqual ( bytes3, Data ( [ 0x08 , 0x09 , 0x0A ] ) , file : file , line : line )
70
83
}
71
84
}
0 commit comments