github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/upstream/upstream_test.go (about)

     1  package upstream
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/vicanso/elton"
     9  	"github.com/vicanso/pike/config"
    10  	us "github.com/vicanso/upstream"
    11  	"golang.org/x/net/http2"
    12  )
    13  
    14  func TestNewTransport(t *testing.T) {
    15  	assert := assert.New(t)
    16  
    17  	transport := newTransport(true)
    18  
    19  	h2Transport, ok := transport.(*http2.Transport)
    20  	assert.True(ok)
    21  	assert.True(h2Transport.AllowHTTP)
    22  
    23  	transport = newTransport(false)
    24  
    25  	hTransport, ok := transport.(*http.Transport)
    26  	assert.True(ok)
    27  	assert.True(hTransport.ForceAttemptHTTP2)
    28  }
    29  
    30  func TestNewTargetPicker(t *testing.T) {
    31  	assert := assert.New(t)
    32  
    33  	uh := &us.HTTP{
    34  		Policy: us.PolicyLeastconn,
    35  	}
    36  	err := uh.Add("http://127.0.0.1:3000")
    37  	assert.Nil(err)
    38  	for _, up := range uh.GetUpstreamList() {
    39  		up.Healthy()
    40  	}
    41  	fn := newTargetPicker(uh)
    42  	c := elton.NewContext(nil, nil)
    43  	url, done, err := fn(c)
    44  	assert.Nil(err)
    45  	assert.NotNil(done)
    46  	assert.Equal("http://127.0.0.1:3000", url.String())
    47  	done(c)
    48  }
    49  
    50  func TestUpstreamServer(t *testing.T) {
    51  	assert := assert.New(t)
    52  	addr := "https://www.bing.com/"
    53  	server := NewUpstreamServer(UpstreamServerOption{
    54  		Policy:      us.PolicyLeastconn,
    55  		Name:        "bing",
    56  		HealthCheck: "/",
    57  		Servers: []UpstreamServerConfig{
    58  			{
    59  				Addr: addr,
    60  			},
    61  			{
    62  				Addr:   "https://bing.com/",
    63  				Backup: true,
    64  			},
    65  		},
    66  		OnStatus: func(info StatusInfo) {
    67  			assert.Equal("healthy", info.Status)
    68  		},
    69  	})
    70  	up, done := server.HTTPUpstream.Next()
    71  	assert.NotNil(up)
    72  	assert.False(up.Backup)
    73  	assert.Equal(addr, up.URL.String())
    74  	assert.NotNil(done)
    75  
    76  	statusList := server.GetServerStatusList()
    77  	assert.Equal(len(server.servers), len(statusList))
    78  
    79  	server.Destroy()
    80  }
    81  
    82  func TestUpstreamServers(t *testing.T) {
    83  	assert := assert.New(t)
    84  	baidu := "baidu"
    85  	servers := NewUpstreamServers([]UpstreamServerOption{
    86  		{
    87  			Name:        baidu,
    88  			HealthCheck: "/",
    89  			Servers: []UpstreamServerConfig{
    90  				{
    91  					Addr: "https://www.baidu.com",
    92  				},
    93  			},
    94  		},
    95  	})
    96  	baiduServer := servers.Get(baidu)
    97  	assert.NotNil(baiduServer)
    98  	assert.Equal(baidu, baiduServer.Option.Name)
    99  
   100  	bing := "bing"
   101  	servers.Reset([]UpstreamServerOption{
   102  		{
   103  			Name:        bing,
   104  			HealthCheck: "/",
   105  			Servers: []UpstreamServerConfig{
   106  				{
   107  					Addr: "https://www.bing.com/",
   108  				},
   109  			},
   110  		},
   111  	})
   112  	assert.Nil(servers.Get(baidu))
   113  	bingServer := servers.Get(bing)
   114  	assert.NotNil(bingServer)
   115  	assert.Equal(bing, bingServer.Option.Name)
   116  }
   117  
   118  func TestConvertConfig(t *testing.T) {
   119  	assert := assert.New(t)
   120  
   121  	name := "upstream-test"
   122  	healthCheck := "/ping"
   123  	policy := "first"
   124  	enableH2C := true
   125  	acceptEncoding := "gzip, br"
   126  	addr := "http://127.0.0.1:3015"
   127  	backup := true
   128  
   129  	configs := []config.UpstreamConfig{
   130  		{
   131  			Name:           name,
   132  			HealthCheck:    healthCheck,
   133  			Policy:         policy,
   134  			EnableH2C:      enableH2C,
   135  			AcceptEncoding: acceptEncoding,
   136  			Servers: []config.UpstreamServerConfig{
   137  				{
   138  					Addr:   addr,
   139  					Backup: backup,
   140  				},
   141  			},
   142  		},
   143  	}
   144  	opts := convertConfigs(configs, nil)
   145  	assert.Equal(1, len(opts))
   146  	assert.Equal(name, opts[0].Name)
   147  	assert.Equal(healthCheck, opts[0].HealthCheck)
   148  	assert.Equal(policy, opts[0].Policy)
   149  	assert.Equal(enableH2C, opts[0].EnableH2C)
   150  	assert.Equal(acceptEncoding, opts[0].AcceptEncoding)
   151  	assert.Equal(1, len(opts[0].Servers))
   152  	assert.Equal(addr, opts[0].Servers[0].Addr)
   153  	assert.True(opts[0].Servers[0].Backup)
   154  }
   155  
   156  func TestDefaultUpstreamServers(t *testing.T) {
   157  	bing := "bing"
   158  	assert := assert.New(t)
   159  	Reset([]config.UpstreamConfig{
   160  		{
   161  			Name:        bing,
   162  			HealthCheck: "/",
   163  			Servers: []config.UpstreamServerConfig{
   164  				{
   165  					Addr: "https://www.bing.com/",
   166  				},
   167  			},
   168  		},
   169  	})
   170  	bingServer := Get(bing)
   171  	assert.NotNil(bingServer)
   172  	assert.Equal(bing, bingServer.Option.Name)
   173  }