github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/reader_memory_test.go (about)

     1  package caches
     2  
     3  import "testing"
     4  
     5  func TestMemoryReader_Header(t *testing.T) {
     6  	item := &MemoryItem{
     7  		ExpiresAt:   0,
     8  		HeaderValue: []byte("0123456789"),
     9  		BodyValue:   nil,
    10  		Status:      2000,
    11  	}
    12  	reader := NewMemoryReader(item)
    13  	buf := make([]byte, 6)
    14  	err := reader.ReadHeader(buf, func(n int) (goNext bool, err error) {
    15  		t.Log("buf:", string(buf[:n]))
    16  		return true, nil
    17  	})
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  }
    22  
    23  func TestMemoryReader_Body(t *testing.T) {
    24  	item := &MemoryItem{
    25  		ExpiresAt:   0,
    26  		HeaderValue: nil,
    27  		BodyValue:   []byte("0123456789"),
    28  		Status:      2000,
    29  	}
    30  	reader := NewMemoryReader(item)
    31  	buf := make([]byte, 6)
    32  	err := reader.ReadBody(buf, func(n int) (goNext bool, err error) {
    33  		t.Log("buf:", string(buf[:n]))
    34  		return true, nil
    35  	})
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  }
    40  
    41  func TestMemoryReader_Body_Range(t *testing.T) {
    42  	item := &MemoryItem{
    43  		ExpiresAt:   0,
    44  		HeaderValue: nil,
    45  		BodyValue:   []byte("0123456789"),
    46  		Status:      2000,
    47  	}
    48  	reader := NewMemoryReader(item)
    49  	buf := make([]byte, 6)
    50  	var err error
    51  	{
    52  		err = reader.ReadBodyRange(buf, 0, 0, func(n int) (goNext bool, err error) {
    53  			t.Log("[0, 0]", "body:", string(buf[:n]))
    54  			return true, nil
    55  		})
    56  		if err != nil {
    57  			t.Fatal(err)
    58  		}
    59  	}
    60  	{
    61  		err = reader.ReadBodyRange(buf, 7, 7, func(n int) (goNext bool, err error) {
    62  			t.Log("[7, 7]", "body:", string(buf[:n]))
    63  			return true, nil
    64  		})
    65  		if err != nil {
    66  			t.Fatal(err)
    67  		}
    68  	}
    69  	{
    70  		err = reader.ReadBodyRange(buf, 0, 10, func(n int) (goNext bool, err error) {
    71  			t.Log("[0, 10]", "body:", string(buf[:n]))
    72  			return true, nil
    73  		})
    74  		if err != nil {
    75  			t.Fatal(err)
    76  		}
    77  	}
    78  	{
    79  		err = reader.ReadBodyRange(buf, 3, 5, func(n int) (goNext bool, err error) {
    80  			t.Log("[3, 5]", "body:", string(buf[:n]))
    81  			return true, nil
    82  		})
    83  		if err != nil {
    84  			t.Fatal(err)
    85  		}
    86  	}
    87  	{
    88  		err = reader.ReadBodyRange(buf, -1, -3, func(n int) (goNext bool, err error) {
    89  			t.Log("[, -3]", "body:", string(buf[:n]))
    90  			return true, nil
    91  		})
    92  		if err != nil {
    93  			t.Fatal(err)
    94  		}
    95  	}
    96  	{
    97  		err = reader.ReadBodyRange(buf, 3, -1, func(n int) (goNext bool, err error) {
    98  			t.Log("[3, ]", "body:", string(buf[:n]))
    99  			return true, nil
   100  		})
   101  		if err != nil {
   102  			t.Fatal(err)
   103  		}
   104  	}
   105  }