github.com/optim-corp/cios-golang-sdk@v0.5.1/sdk/service_filestorage_file_test.go (about)

     1  package ciossdk
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	srvfilestorage "github.com/optim-corp/cios-golang-sdk/sdk/service/filestorage"
    10  
    11  	sdkmodel "github.com/optim-corp/cios-golang-sdk/model"
    12  )
    13  
    14  func TestFileStorage_DownloadFile(t *testing.T) {
    15  	requested := false
    16  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    17  		requested = true
    18  		w.Header().Set("Content-Type", "application/json")
    19  		if r.URL.Path != "/v2/file_storage/buckets/bucketid/download" || r.URL.Query().Get("node_id") != "node" {
    20  			t.Fatal(r.URL.Path)
    21  		}
    22  		if r.Method != "GET" {
    23  			t.Fatal(r.Method)
    24  		}
    25  	}))
    26  	defer ts.Close()
    27  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{StorageUrl: ts.URL}})
    28  	client.FileStorage().DownloadFile(context.Background(), "bucketid", "node")
    29  	if !requested {
    30  		t.Fatal(ts.URL)
    31  	}
    32  }
    33  
    34  func TestFileStorage_DownloadFileByKey(t *testing.T) {
    35  	requested := false
    36  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    37  		requested = true
    38  		w.Header().Set("Content-Type", "application/json")
    39  		if r.URL.Path != "/v2/file_storage/buckets/bucketid/download" || r.URL.Query().Get("key") != "node" {
    40  			t.Fatal(r.URL.Path)
    41  		}
    42  		if r.Method != "GET" {
    43  			t.Fatal(r.Method)
    44  		}
    45  	}))
    46  	defer ts.Close()
    47  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{StorageUrl: ts.URL}})
    48  	client.FileStorage().DownloadFileByKey(context.Background(), "bucketid", "node")
    49  	if !requested {
    50  		t.Fatal(ts.URL)
    51  	}
    52  }
    53  func TestFileStorage_UploadFile(t *testing.T) {
    54  	key, nodeId := "", ""
    55  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    56  		w.Header().Set("Content-Type", "application/json")
    57  		key = r.URL.Query().Get("key")
    58  		nodeId = r.URL.Query().Get("node_id")
    59  		if r.URL.Query().Get("checksum") != "NsRTaZbKVhXc+ZEfBoeG3A==" {
    60  			t.Fatal(r.URL.Query().Get("checksum"))
    61  		}
    62  		if r.URL.Path != "/v2/file_storage/buckets/bucketid/upload" {
    63  			t.Fatal(r.URL.Path)
    64  		}
    65  		if r.Method != "PUT" {
    66  			t.Fatal(r.Method)
    67  		}
    68  	}))
    69  	defer ts.Close()
    70  	client := NewCiosClient(CiosClientConfig{Urls: sdkmodel.CIOSUrl{StorageUrl: ts.URL}})
    71  	client.FileStorage().UploadFile(context.Background(), "bucketid", []byte("node"),
    72  		srvfilestorage.MakeUploadFileOpts().Key("").NodeId(""))
    73  	if nodeId != "" || key != "" {
    74  		t.Fatal(nodeId, key)
    75  	}
    76  	client.FileStorage().UploadFile(context.Background(), "bucketid", []byte("node"),
    77  		srvfilestorage.MakeUploadFileOpts().Key("test").NodeId("test"))
    78  	if nodeId != "test" || key != "test" {
    79  		t.Fatal(nodeId, key)
    80  	}
    81  }