github.com/searKing/golang/go@v1.2.117/io/sniff_test.go (about) 1 // Copyright 2024 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package io_test 6 7 import ( 8 "io" 9 "strings" 10 "testing" 11 12 io_ "github.com/searKing/golang/go/io" 13 ) 14 15 func shrinkSnifferMayBeWithHoleInHistoryBuffers(r io_.ReadSniffer, sniffing bool) { 16 // stop sniffing and start sniffing, to move buffer to history buffers 17 r.Sniff(!sniffing).Sniff(sniffing) 18 } 19 20 func TestSniffReaderSeeker(t *testing.T) { 21 r := strings.NewReader("HEADER BODY TAILER") 22 sniff := io_.SniffReader(r) 23 24 // start sniffing 25 sniff.Sniff(true) 26 // ["HEADER BODY TAILER"] 27 28 { // sniff "HEADER" 29 b := make([]byte, len("HEADER")) 30 n, err := sniff.Read(b) 31 if err != nil { 32 t.Errorf("Error reading header: %v", err) 33 } 34 if string(b[:n]) != "HEADER" { 35 t.Errorf("expected %q, got %q", "HEADER", b[:n]) 36 } 37 shrinkSnifferMayBeWithHoleInHistoryBuffers(sniff, true) 38 // ["HEADER BODY TAILER"] 39 } 40 41 { // sniff "HEAD" 42 b := make([]byte, len("HEAD")) 43 n, err := sniff.Read(b) 44 if err != nil { 45 t.Errorf("Error reading body: %v", err) 46 } 47 if string(b[:n]) != "HEAD" { 48 t.Errorf("expected %q, got %q", " BODY", b[:n]) 49 } 50 shrinkSnifferMayBeWithHoleInHistoryBuffers(sniff, true) 51 // ["HEADER BODY TAILER"] 52 } 53 54 // stop sniffing 55 sniff.Sniff(false) 56 57 { // sniff "HEADER" 58 b := make([]byte, len("HEADER")) 59 n, err := sniff.Read(b) 60 if err != nil { 61 t.Errorf("Error reading header: %v", err) 62 } 63 if string(b[:n]) != "HEADER" { 64 t.Errorf("expected %q, got %q", "HEAD", b[:n]) 65 } 66 } 67 { 68 b, err := io.ReadAll(sniff) 69 if err != nil { 70 t.Errorf("Error reading all: %v", err) 71 } 72 if string(b) != " BODY TAILER" { 73 t.Errorf("expected %q, got %q", " BODY TAIL", b) 74 } 75 } 76 } 77 78 type reader struct { 79 r io.Reader 80 } 81 82 func (r *reader) Read(b []byte) (int, error) { 83 return r.r.Read(b) 84 } 85 86 func TestSniffReaderNotSeeker(t *testing.T) { 87 r := &reader{r: strings.NewReader("HEADER BODY TAILER")} 88 sniff := io_.SniffReader(r) 89 90 // start sniffing 91 sniff.Sniff(true) 92 // ["HEADER BODY TAILER"] 93 94 { // sniff "HEADER" 95 b := make([]byte, len("HEADER")) 96 n, err := sniff.Read(b) 97 if err != nil { 98 t.Errorf("Error reading header: %v", err) 99 } 100 if string(b[:n]) != "HEADER" { 101 t.Errorf("expected %q, got %q", "HEADER", b[:n]) 102 } 103 shrinkSnifferMayBeWithHoleInHistoryBuffers(sniff, true) 104 // ["HEADER", " BODY TAILER"] 105 } 106 107 { // sniff "HEAD" 108 b := make([]byte, len("HEAD")) 109 n, err := sniff.Read(b) 110 if err != nil { 111 t.Errorf("Error reading body: %v", err) 112 } 113 if string(b[:n]) != "HEAD" { 114 t.Errorf("expected %q, got %q", " BODY", b[:n]) 115 } 116 shrinkSnifferMayBeWithHoleInHistoryBuffers(sniff, true) 117 // ["HEAD", "ER", " BODY TAILER"] 118 } 119 120 // stop sniffing 121 sniff.Sniff(false) 122 123 { // sniff "HEADER" 124 b := make([]byte, len("HEADER")) 125 n, err := sniff.Read(b) 126 if err != nil { 127 t.Errorf("Error reading header: %v", err) 128 } 129 if string(b[:n]) != "HEAD" { 130 t.Errorf("expected %q, got %q", "HEAD", b[:n]) 131 } 132 n, err = sniff.Read(b) 133 if err != nil { 134 t.Errorf("Error reading header: %v", err) 135 } 136 if string(b[:n]) != "ER" { 137 t.Errorf("expected %q, got %q", "ER", b[:n]) 138 } 139 } 140 { 141 b, err := io.ReadAll(sniff) 142 if err != nil { 143 t.Errorf("Error reading all: %v", err) 144 } 145 if string(b) != " BODY TAILER" { 146 t.Errorf("expected %q, got %q", " BODY TAIL", b) 147 } 148 } 149 }