(t *testing.T)
| 10 | ) |
| 11 | |
| 12 | func TestBodyWrapper_Close(t *testing.T) { |
| 13 | t.Run("close after read", func(t *testing.T) { |
| 14 | body := newMockBody("test_body") |
| 15 | |
| 16 | cancelCount := 0 |
| 17 | cancelFn := func() { |
| 18 | cancelCount++ |
| 19 | } |
| 20 | |
| 21 | wrp := newBodyWrapper(body, cancelFn) |
| 22 | |
| 23 | b, err := io.ReadAll(wrp) |
| 24 | assert.NoError(t, err) |
| 25 | assert.Equal(t, "test_body", string(b)) |
| 26 | |
| 27 | // fully read and closed |
| 28 | assert.Equal(t, 2, body.readCount) |
| 29 | assert.Equal(t, 1, body.closeCount) |
| 30 | assert.Equal(t, 1, cancelCount) |
| 31 | |
| 32 | err = wrp.Close() |
| 33 | assert.NoError(t, err) |
| 34 | |
| 35 | // nothing changed |
| 36 | assert.Equal(t, 2, body.readCount) |
| 37 | assert.Equal(t, 1, body.closeCount) |
| 38 | assert.Equal(t, 1, cancelCount) |
| 39 | }) |
| 40 | |
| 41 | t.Run("close before read", func(t *testing.T) { |
| 42 | body := newMockBody("test_body") |
| 43 | |
| 44 | cancelCount := 0 |
| 45 | cancelFn := func() { |
| 46 | cancelCount++ |
| 47 | } |
| 48 | |
| 49 | wrp := newBodyWrapper(body, cancelFn) |
| 50 | |
| 51 | err := wrp.Close() |
| 52 | assert.NoError(t, err) |
| 53 | |
| 54 | // fully read and closed |
| 55 | assert.Equal(t, 2, body.readCount) |
| 56 | assert.Equal(t, 1, body.closeCount) |
| 57 | assert.Equal(t, 1, cancelCount) |
| 58 | |
| 59 | b := make([]byte, 1) |
| 60 | |
| 61 | n, err := wrp.Read(b) |
| 62 | assert.Error(t, io.EOF, err) |
| 63 | assert.Equal(t, 0, n) |
| 64 | |
| 65 | // nothing changed |
| 66 | assert.Equal(t, 2, body.readCount) |
| 67 | assert.Equal(t, 1, body.closeCount) |
| 68 | assert.Equal(t, 1, cancelCount) |
| 69 | }) |
nothing calls this directly
no test coverage detected
searching dependent graphs…