github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/http_request_utils_test.go (about)

     1  package nodes
     2  
     3  import (
     4  	teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
     5  	"github.com/TeaOSLab/EdgeNode/internal/zero"
     6  	"github.com/iwind/TeaGo/assert"
     7  	"runtime"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestHTTPRequest_httpRequestGenBoundary(t *testing.T) {
    14  	for i := 0; i < 10; i++ {
    15  		var boundary = httpRequestGenBoundary()
    16  		t.Log(boundary, "[", len(boundary), "bytes", "]")
    17  	}
    18  }
    19  
    20  func TestHTTPRequest_httpRequestParseBoundary(t *testing.T) {
    21  	var a = assert.NewAssertion(t)
    22  	a.IsTrue(httpRequestParseBoundary("multipart/byteranges") == "")
    23  	a.IsTrue(httpRequestParseBoundary("multipart/byteranges; boundary=123") == "123")
    24  	a.IsTrue(httpRequestParseBoundary("multipart/byteranges; boundary=123; 456") == "123")
    25  }
    26  
    27  func TestHTTPRequest_httpRequestParseRangeHeader(t *testing.T) {
    28  	var a = assert.NewAssertion(t)
    29  	{
    30  		_, ok := httpRequestParseRangeHeader("")
    31  		a.IsFalse(ok)
    32  	}
    33  	{
    34  		_, ok := httpRequestParseRangeHeader("byte=")
    35  		a.IsFalse(ok)
    36  	}
    37  	{
    38  		_, ok := httpRequestParseRangeHeader("byte=")
    39  		a.IsFalse(ok)
    40  	}
    41  	{
    42  		set, ok := httpRequestParseRangeHeader("bytes=")
    43  		a.IsTrue(ok)
    44  		a.IsTrue(len(set) == 0)
    45  	}
    46  	{
    47  		_, ok := httpRequestParseRangeHeader("bytes=60-50")
    48  		a.IsFalse(ok)
    49  	}
    50  	{
    51  		set, ok := httpRequestParseRangeHeader("bytes=0-50")
    52  		a.IsTrue(ok)
    53  		a.IsTrue(len(set) > 0)
    54  		t.Log(set)
    55  	}
    56  	{
    57  		set, ok := httpRequestParseRangeHeader("bytes=0-")
    58  		a.IsTrue(ok)
    59  		a.IsTrue(len(set) > 0)
    60  		if len(set) > 0 {
    61  			a.IsTrue(set[0][0] == 0)
    62  		}
    63  		t.Log(set)
    64  	}
    65  	{
    66  		set, ok := httpRequestParseRangeHeader("bytes=-50")
    67  		a.IsTrue(ok)
    68  		a.IsTrue(len(set) > 0)
    69  		t.Log(set)
    70  	}
    71  	{
    72  		set, ok := httpRequestParseRangeHeader("bytes=0-50, 60-100")
    73  		a.IsTrue(ok)
    74  		a.IsTrue(len(set) > 0)
    75  		t.Log(set)
    76  	}
    77  }
    78  
    79  func TestHTTPRequest_httpRequestParseContentRangeHeader(t *testing.T) {
    80  	{
    81  		var c1 = "bytes 0-100/*"
    82  		t.Log(httpRequestParseContentRangeHeader(c1))
    83  	}
    84  	{
    85  		var c1 = "bytes 30-100/*"
    86  		t.Log(httpRequestParseContentRangeHeader(c1))
    87  	}
    88  	{
    89  		var c1 = "bytes1 0-100/*"
    90  		t.Log(httpRequestParseContentRangeHeader(c1))
    91  	}
    92  }
    93  
    94  func BenchmarkHTTPRequest_httpRequestParseContentRangeHeader(b *testing.B) {
    95  	for i := 0; i < b.N; i++ {
    96  		var c1 = "bytes 0-100/*"
    97  		httpRequestParseContentRangeHeader(c1)
    98  	}
    99  }
   100  
   101  func TestHTTPRequest_httpRequestNextId(t *testing.T) {
   102  	teaconst.NodeId = 123
   103  	teaconst.NodeIdString = "123"
   104  	t.Log(httpRequestNextId())
   105  	t.Log(httpRequestNextId())
   106  	t.Log(httpRequestNextId())
   107  	time.Sleep(1 * time.Second)
   108  	t.Log(httpRequestNextId())
   109  	t.Log(httpRequestNextId())
   110  	time.Sleep(1 * time.Second)
   111  	t.Log(httpRequestNextId())
   112  }
   113  
   114  func TestHTTPRequest_httpRequestNextId_Concurrent(t *testing.T) {
   115  	var m = map[string]zero.Zero{}
   116  	var locker = sync.Mutex{}
   117  
   118  	var count = 4000
   119  	var wg = &sync.WaitGroup{}
   120  	wg.Add(count)
   121  
   122  	var countDuplicated = 0
   123  	for i := 0; i < count; i++ {
   124  		go func() {
   125  			defer wg.Done()
   126  
   127  			var requestId = httpRequestNextId()
   128  
   129  			locker.Lock()
   130  
   131  			_, ok := m[requestId]
   132  			if ok {
   133  				t.Log("duplicated:", requestId)
   134  				countDuplicated++
   135  			}
   136  
   137  			m[requestId] = zero.New()
   138  			locker.Unlock()
   139  		}()
   140  	}
   141  	wg.Wait()
   142  	t.Log("ok", countDuplicated, "duplicated")
   143  
   144  	var a = assert.NewAssertion(t)
   145  	a.IsTrue(countDuplicated == 0)
   146  }
   147  
   148  func TestHTTPParseURL(t *testing.T) {
   149  	for _, s := range []string{
   150  		"",
   151  		"null",
   152  		"example.com",
   153  		"https://example.com",
   154  		"https://example.com/hello",
   155  	} {
   156  		host, err := httpParseHost(s)
   157  		if err == nil {
   158  			t.Log(s, "=>", host)
   159  		} else {
   160  			t.Log(s, "=>")
   161  		}
   162  	}
   163  }
   164  
   165  func BenchmarkHTTPRequest_httpRequestNextId(b *testing.B) {
   166  	runtime.GOMAXPROCS(1)
   167  
   168  	teaconst.NodeIdString = "123"
   169  
   170  	for i := 0; i < b.N; i++ {
   171  		_ = httpRequestNextId()
   172  	}
   173  }