github.com/qiniu/go-sdk/v7@v7.27.0/pili/domain_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package pili
     5  
     6  import (
     7  	"context"
     8  	"math/rand"
     9  	"strconv"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestManager_GetDomainsList(t *testing.T) {
    16  	if SkipTest() {
    17  		t.SkipNow()
    18  	}
    19  	ast := assert.New(t)
    20  	tests := map[string]struct {
    21  		inputHub    string
    22  		expectedErr string
    23  	}{
    24  		"get domains list": {inputHub: TestHub, expectedErr: ""},
    25  		"no hub name":      {inputHub: "", expectedErr: "Key: 'GetDomainsListRequest.Hub' Error:Field validation for 'Hub' failed on the 'required' tag"},
    26  		"unknown hub name": {inputHub: "unknown_hub", expectedErr: "hub not found"},
    27  	}
    28  	for name, tt := range tests {
    29  		t.Run(name, func(t *testing.T) {
    30  			manager := NewManager(ManagerConfig{APIHost: ApiHost, AccessKey: AccessKey, SecretKey: SecretKey})
    31  			response, err := manager.GetDomainsList(context.Background(), GetDomainsListRequest{Hub: tt.inputHub})
    32  			CheckErr(ast, err, tt.expectedErr)
    33  			if err == nil {
    34  				t.Logf("response :[%+v]", *response)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  func TestManager_GetDomainInfo(t *testing.T) {
    41  	if SkipTest() || TestDomain == "" {
    42  		t.SkipNow()
    43  	}
    44  	ast := assert.New(t)
    45  	tests := map[string]struct {
    46  		inputHub    string
    47  		inputDomain string
    48  		expectedErr string
    49  	}{
    50  		"get domain info":           {inputHub: TestHub, inputDomain: TestDomain, expectedErr: ""},
    51  		"mismatched domain and hub": {inputHub: TestHub, inputDomain: "www.qiniu.com", expectedErr: "no such domain"},
    52  		"unknown hub":               {inputHub: "unknown_hub", inputDomain: TestDomain, expectedErr: "hub not found"},
    53  		"no hub":                    {inputHub: "", inputDomain: TestDomain, expectedErr: "Key: 'GetDomainInfoRequest.Hub' Error:Field validation for 'Hub' failed on the 'required' tag"},
    54  		"no domain":                 {inputHub: TestHub, inputDomain: "", expectedErr: "Key: 'GetDomainInfoRequest.Domain' Error:Field validation for 'Domain' failed on the 'required' tag"},
    55  	}
    56  	for name, tt := range tests {
    57  		t.Run(name, func(t *testing.T) {
    58  			manager := NewManager(ManagerConfig{APIHost: ApiHost, AccessKey: AccessKey, SecretKey: SecretKey})
    59  			response, err := manager.GetDomainInfo(context.Background(), GetDomainInfoRequest{
    60  				Hub:    tt.inputHub,
    61  				Domain: tt.inputDomain,
    62  			})
    63  			CheckErr(ast, err, tt.expectedErr)
    64  			if err == nil {
    65  				t.Log(response)
    66  				ast.Equal(response.Domain, tt.inputDomain)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestManager_Bind_Unbind_Domains(t *testing.T) {
    73  	if SkipTest() {
    74  		t.SkipNow()
    75  	}
    76  	ast := assert.New(t)
    77  	manager := NewManager(ManagerConfig{APIHost: ApiHost, AccessKey: AccessKey, SecretKey: SecretKey})
    78  	domain := "testbind-" + strconv.Itoa(rand.Intn(9999)) + ".fake.qiniu.com"
    79  
    80  	err := manager.BindDomain(context.Background(), BindDomainRequest{
    81  		Hub:    TestHub,
    82  		Domain: domain,
    83  		Type:   DomainLiveHLS,
    84  	})
    85  	ast.Nil(err)
    86  	err = manager.UnbindDomain(context.Background(), UnbindDomainRequest{
    87  		Hub:    TestHub,
    88  		Domain: domain,
    89  	})
    90  	ast.Nil(err)
    91  }
    92  
    93  func TestManager_ErrorBindDomains(t *testing.T) {
    94  	if SkipTest() || TestDomain == "" {
    95  		t.SkipNow()
    96  	}
    97  	ast := assert.New(t)
    98  	tests := map[string]struct {
    99  		inputHub    string
   100  		inputDomain string
   101  		expectedErr string
   102  	}{
   103  		"existed domain": {inputHub: TestHub, inputDomain: TestDomain, expectedErr: "domain confict"},
   104  		"invalid domain": {inputHub: TestHub, inputDomain: "notdomain", expectedErr: "invalid domain"},
   105  		"unknown hub":    {inputHub: "unknown_hub", inputDomain: "testbind-" + strconv.Itoa(rand.Intn(9999)) + ".fake.qiniu.com", expectedErr: "hub not found"},
   106  		"no hub":         {inputHub: "", inputDomain: "testbind-" + strconv.Itoa(rand.Intn(9999)) + ".fake.qiniu.com", expectedErr: "Key: 'BindDomainRequest.Hub' Error:Field validation for 'Hub' failed on the 'required' tag"},
   107  		"no domain":      {inputHub: TestHub, inputDomain: "", expectedErr: "Key: 'BindDomainRequest.Domain' Error:Field validation for 'Domain' failed on the 'required' tag"},
   108  	}
   109  
   110  	for name, tt := range tests {
   111  		t.Run(name, func(t *testing.T) {
   112  			manager := NewManager(ManagerConfig{APIHost: ApiHost, AccessKey: AccessKey, SecretKey: SecretKey})
   113  
   114  			err := manager.BindDomain(context.Background(), BindDomainRequest{
   115  				Hub:    tt.inputHub,
   116  				Domain: tt.inputDomain,
   117  				Type:   DomainLiveHLS,
   118  			})
   119  			CheckErr(ast, err, tt.expectedErr)
   120  		})
   121  	}
   122  }
   123  
   124  func TestManager_ErrUnbindDomains(t *testing.T) {
   125  	if SkipTest() {
   126  		t.SkipNow()
   127  	}
   128  	ast := assert.New(t)
   129  	tests := map[string]struct {
   130  		inputHub    string
   131  		inputDomain string
   132  		expectedErr string
   133  	}{
   134  		"unknown domain": {inputHub: TestHub, inputDomain: "unknown.fake.qiniu.com", expectedErr: "not found"},
   135  		"unknown hub":    {inputHub: "unknown_hub", inputDomain: "testbind-" + strconv.Itoa(rand.Intn(9999)) + ".fake.qiniu.com", expectedErr: "not found"},
   136  		"no hub":         {inputHub: "", inputDomain: "testbind-" + strconv.Itoa(rand.Intn(9999)) + ".fake.qiniu.com", expectedErr: "Key: 'UnbindDomainRequest.Hub' Error:Field validation for 'Hub' failed on the 'required' tag"},
   137  		"no domain":      {inputHub: TestHub, inputDomain: "", expectedErr: "Key: 'UnbindDomainRequest.Domain' Error:Field validation for 'Domain' failed on the 'required' tag"},
   138  	}
   139  
   140  	for name, tt := range tests {
   141  		t.Run(name, func(t *testing.T) {
   142  			manager := NewManager(ManagerConfig{APIHost: ApiHost, AccessKey: AccessKey, SecretKey: SecretKey})
   143  			err := manager.UnbindDomain(context.Background(), UnbindDomainRequest{
   144  				Hub:    tt.inputHub,
   145  				Domain: tt.inputDomain,
   146  			})
   147  			CheckErr(ast, err, tt.expectedErr)
   148  		})
   149  	}
   150  }
   151  
   152  func TestManager_BindVodDomain(t *testing.T) {
   153  	if SkipTest() || TestVodDomain == "" {
   154  		t.SkipNow()
   155  	}
   156  	ast := assert.New(t)
   157  	manager := NewManager(ManagerConfig{APIHost: ApiHost, AccessKey: AccessKey, SecretKey: SecretKey})
   158  	err := manager.BindVodDomain(context.Background(), BindVodDomainRequest{
   159  		Hub:       TestHub,
   160  		VodDomain: TestVodDomain,
   161  	})
   162  	ast.Nil(err)
   163  }
   164  
   165  func TestManager_DomainCert(t *testing.T) {
   166  	if SkipTest() || TestCertName == "" {
   167  		t.SkipNow()
   168  	}
   169  	ast := assert.New(t)
   170  	manager := NewManager(ManagerConfig{APIHost: ApiHost, AccessKey: AccessKey, SecretKey: SecretKey})
   171  	err := manager.SetDomainCert(context.Background(), SetDomainCertRequest{
   172  		Hub:      TestHub,
   173  		Domain:   TestDomain,
   174  		CertName: TestCertName,
   175  	})
   176  	ast.Nil(err)
   177  }
   178  
   179  func TestManager_DomainUrlRewrite(t *testing.T) {
   180  	if SkipTest() {
   181  		t.SkipNow()
   182  	}
   183  	ast := assert.New(t)
   184  	manager := NewManager(ManagerConfig{APIHost: ApiHost, AccessKey: AccessKey, SecretKey: SecretKey})
   185  
   186  	rules := []DomainURLRewriteRule{
   187  		{Pattern: "live.qiniu.com/pili/(.+)", Replace: "live.qiniu.com/live-pili/${1}"},
   188  		{Pattern: "live-origin.qiniu.com/pili/(.+)", Replace: "live-origin.qiniu.com/live-pili/${1}"},
   189  		{Pattern: "(.+)/pili/(.+)/playlist.m3u8", Replace: "${1}/live-pili/${2}.m3u8"},
   190  	}
   191  	err := manager.SetDomainURLRewrite(context.Background(), SetDomainURLRewriteRequest{
   192  		Hub:    TestHub,
   193  		Domain: TestDomain,
   194  		Rules:  rules,
   195  	})
   196  	ast.Nil(err)
   197  }