yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/client_test.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ecloud
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  type signerMock struct {
    22  	SRamRoleSigner
    23  }
    24  
    25  func (s *signerMock) GetNonce() string {
    26  	return "9d81ffbeaaf7477390db5df577bb3299"
    27  }
    28  
    29  type requestMock struct {
    30  	SBaseRequest
    31  }
    32  
    33  func (r *requestMock) GetTimestamp() string {
    34  	return "2017-01-11T15:15:11Z"
    35  }
    36  
    37  func TestBuildStringToSing(t *testing.T) {
    38  	signer := &signerMock{SRamRoleSigner: *NewRamRoleSigner("testid", "testsecret")}
    39  	client, _ := NewEcloudClient(NewEcloudClientConfig(signer))
    40  	request := &requestMock{
    41  		SBaseRequest: SBaseRequest{
    42  			Method:      "GET",
    43  			ServerPath:  "/api/keypair",
    44  			QueryParams: map[string]string{},
    45  			Headers:     map[string]string{},
    46  			Content:     []byte{},
    47  		},
    48  	}
    49  	client.completeSingParams(request)
    50  	stringToSign := client.buildStringToSign(request)
    51  	want := `GET
    52  %2Fapi%2Fkeypair
    53  25fb697a06bc8a16a0a2549460f5cba35aded3818e15f088e4e17cd394aa07af`
    54  	if stringToSign != want {
    55  		t.Fatalf("want:\n%s, get:\n%s", want, stringToSign)
    56  	}
    57  }