github.com/qiniu/go-sdk/v7@v7.27.0/storage/region_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package storage
     5  
     6  import (
     7  	"encoding/json"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/qiniu/go-sdk/v7/client"
    12  )
    13  
    14  func TestRegion(t *testing.T) {
    15  	region1, err := GetRegion(testAK, testBucket)
    16  	if err != nil {
    17  		t.Fatalf("GetRegion error: %v\n", err)
    18  	}
    19  
    20  	if !strings.HasPrefix(region1.IovipHost, "iovip") || !strings.HasSuffix(region1.IovipHost, ".qbox.me") {
    21  		t.Fatalf("region1.IovipHost is wrong")
    22  	}
    23  }
    24  
    25  func TestRegionWithNoProtocol(t *testing.T) {
    26  	UcHost = "uc.qbox.me"
    27  	region1, err := GetRegion(testAK, testBucket)
    28  	if err != nil {
    29  		t.Fatalf("GetRegion error: %v\n", err)
    30  	}
    31  
    32  	if !strings.HasPrefix(region1.IovipHost, "iovip") || !strings.HasSuffix(region1.IovipHost, ".qbox.me") {
    33  		t.Fatalf("region1.IovipHost is wrong: %v\v", region1.IovipHost)
    34  	}
    35  }
    36  
    37  func TestRegionInfo(t *testing.T) {
    38  	rs, err := GetRegionsInfo(mac)
    39  	if err != nil {
    40  		t.Fatalf("GetRegionsInfo error: %v\n", err)
    41  	}
    42  	if len(rs) == 0 {
    43  		t.Fatal("GetRegionsInfo error: region is empty \n")
    44  	}
    45  
    46  	r := rs[0]
    47  	if len(r.ID) == 0 {
    48  		t.Fatalf("GetRegionsInfo error: r id is empty, %+v", r)
    49  	}
    50  }
    51  
    52  func TestUcQueryRetUnmarshalJSON(t *testing.T) {
    53  	retJson := `{
    54    "region": "z2",
    55    "ttl": 86400,
    56    "io": {
    57      "src": {
    58        "main": [
    59          "iovip-z2.qbox.me"
    60        ],
    61        "info": "compatible to non-SNI device"
    62      }
    63    },
    64    "up": {
    65      "acc": {
    66        "main": [
    67          "upload-z2.qiniup.com"
    68        ],
    69        "backup": [
    70          "upload-dg.qiniup.com",
    71          "upload-fs.qiniup.com"
    72        ]
    73      },
    74      "old_acc": {
    75        "main": [
    76          "upload-z2.qbox.me"
    77        ],
    78        "info": "compatible to non-SNI device"
    79      },
    80      "old_src": {
    81        "main": [
    82          "up-z2.qbox.me"
    83        ],
    84        "info": "compatible to non-SNI device"
    85      },
    86      "src": {
    87        "main": [
    88          "up-z2.qiniup.com"
    89        ],
    90        "backup": [
    91          "up-dg.qiniup.com",
    92          "up-fs.qiniup.com"
    93        ]
    94      }
    95    },
    96    "uc": {
    97      "acc": {
    98        "main": [
    99          "uc.qbox.me"
   100        ]
   101      }
   102    },
   103    "rs": {
   104      "acc": {
   105        "main": [
   106          "rs-z2.qbox.me"
   107        ]
   108      }
   109    },
   110    "rsf": {
   111      "acc": {
   112        "main": [
   113          "rsf-z2.qbox.me"
   114        ]
   115      }
   116    },
   117    "api": {
   118      "acc": {
   119        "main": [
   120          "api-z2.qiniu.com"
   121        ]
   122      }
   123    }
   124  }`
   125  	var ucQueryRet UcQueryRet
   126  	err := json.Unmarshal([]byte(retJson), &ucQueryRet)
   127  	if err != nil {
   128  		t.Fatalf("UcQueryRetUnmarshalJSON error: %v\n", err)
   129  	}
   130  
   131  	if ucQueryRet.Io == nil {
   132  		t.Fatalf("UcQueryRetUnmarshalJSON Io was nil")
   133  	}
   134  
   135  	if ucQueryRet.IoInfo == nil {
   136  		t.Fatalf("UcQueryRetUnmarshalJSON IoInfo was nil")
   137  	}
   138  
   139  	if ucQueryRet.Up == nil {
   140  		t.Fatalf("UcQueryRetUnmarshalJSON Up was nil")
   141  	}
   142  }
   143  
   144  func TestRegionWithSetHost(t *testing.T) {
   145  	SetUcHost("uc.qbox.me", true)
   146  	region1, err := GetRegion(testAK, testBucket)
   147  	if err != nil {
   148  		t.Fatalf("GetRegion error: %v\n", err)
   149  	}
   150  
   151  	if !strings.HasPrefix(region1.IovipHost, "iovip") || !strings.HasSuffix(region1.IovipHost, ".qbox.me") {
   152  		t.Fatalf("region1.IovipHost is wrong: %v\v", region1.IovipHost)
   153  	}
   154  
   155  	region1, err = GetRegionWithOptions(testAK, testBucket, UCApiOptions{
   156  		UseHttps:           true,
   157  		RetryMax:           0,
   158  		Hosts:              []string{"mock.uc.com"},
   159  		HostFreezeDuration: 0,
   160  		Client:             nil,
   161  	})
   162  	if err == nil {
   163  		t.Fatalf("request should be wrong")
   164  	}
   165  }
   166  
   167  func TestRegionV4(t *testing.T) {
   168  	regionGroup, err := getRegionGroup(testAK, testBucket)
   169  	if err != nil {
   170  		t.Fatalf("GetRegion error: %v\n", err)
   171  	}
   172  
   173  	if len(regionGroup.regions) == 0 {
   174  		t.Fatalf("region1.IovipHost is wrong")
   175  	}
   176  
   177  	_, err = getRegionGroupWithOptions(testAK, testBucket, UCApiOptions{
   178  		UseHttps:           true,
   179  		RetryMax:           0,
   180  		Hosts:              []string{"mock.uc.com"},
   181  		HostFreezeDuration: 0,
   182  		Client:             nil,
   183  	})
   184  	if err == nil {
   185  		t.Fatalf("request should be wrong")
   186  	}
   187  }
   188  
   189  func TestRegionV4WithNoProtocol(t *testing.T) {
   190  	client.DebugMode = true
   191  	ucHosts = []string{"aa.qiniu.com", "uc.qbox.me"}
   192  	defer func() {
   193  		client.DebugMode = false
   194  		ucHosts = []string{"uc.qbox.me"}
   195  	}()
   196  	regionV4CacheLoaded = true
   197  	regionGroup, err := getRegionGroup(testAK, testBucket)
   198  	if err != nil {
   199  		t.Fatalf("GetRegion error: %v\n", err)
   200  	}
   201  
   202  	if len(regionGroup.regions) == 0 {
   203  		t.Fatalf("region1.IovipHost is wrong")
   204  	}
   205  }
   206  
   207  func TestRegionV4WithSetHost(t *testing.T) {
   208  	SetUcHost("uc.qbox.me", true)
   209  	regionGroup, err := getRegionGroup(testAK, testBucket)
   210  	if err != nil {
   211  		t.Fatalf("GetRegion error: %v\n", err)
   212  	}
   213  
   214  	if len(regionGroup.regions) == 0 {
   215  		t.Fatalf("region1.IovipHost is wrong")
   216  	}
   217  }