github.com/searKing/golang/go@v1.2.117/net/mux/matcher_test.go (about)

     1  // Copyright 2020 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 mux_test
     6  
     7  import (
     8  	"io"
     9  	"io/ioutil"
    10  	"net"
    11  	"strings"
    12  	"sync"
    13  	"testing"
    14  
    15  	"github.com/searKing/golang/go/net/mux"
    16  	"github.com/searKing/golang/go/testing/leakcheck"
    17  )
    18  
    19  func TestHTTP1Fast(t *testing.T) {
    20  	defer leakcheck.Check(t)
    21  	const payload = "GET /version HTTP/1.1\r\n"
    22  	const mult = 2
    23  
    24  	test(t, mux.HTTP1Fast(), payload, mult)
    25  }
    26  
    27  func TestHTTP1(t *testing.T) {
    28  	defer leakcheck.Check(t)
    29  	const payload = "GET /version HTTP/1.1\r\n"
    30  	const mult = 2
    31  
    32  	test(t, mux.HTTP1(), payload, mult)
    33  }
    34  
    35  func test(t *testing.T, matcher mux.Matcher, payload string, mult int) {
    36  	errCh := make(chan error)
    37  	defer func() {
    38  		select {
    39  		case err := <-errCh:
    40  			t.Fatal(err)
    41  		default:
    42  		}
    43  	}()
    44  
    45  	writer, reader := net.Pipe()
    46  	defer reader.Close()
    47  
    48  	var wg sync.WaitGroup
    49  	wg.Add(1)
    50  	go func() {
    51  		defer wg.Done()
    52  		if _, err := io.WriteString(writer, strings.Repeat(payload, mult)); err != nil {
    53  			t.Fatal(err)
    54  		}
    55  		_ = writer.Close()
    56  
    57  	}()
    58  	if !matcher.Match(ioutil.Discard, reader) {
    59  		t.Errorf("expect false but accept true")
    60  	}
    61  	_, _ = ioutil.ReadAll(reader)
    62  
    63  	wg.Wait()
    64  }