storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/web-handler-context_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"bytes"
    21  	"net/http"
    22  	"testing"
    23  
    24  	"storj.io/minio/cmd/logger"
    25  )
    26  
    27  func TestKeyValueMap(t *testing.T) {
    28  	bucket := "bucket"
    29  	object := "object"
    30  	prefix := "prefix"
    31  	username := "username"
    32  	policy := "policy"
    33  	host := "min.io"
    34  	objects := []string{object, object}
    35  
    36  	km := KeyValueMap{}
    37  	km.SetBucket(bucket)
    38  	km.SetPrefix(prefix)
    39  	km.SetUsername(username)
    40  	km.SetHostname(host)
    41  	km.SetObject(object)
    42  	km.SetObjects(objects)
    43  	km.SetPolicy(policy)
    44  
    45  	if got := km.Bucket(); got != bucket {
    46  		t.Errorf("Expected %s but got %s", bucket, got)
    47  	}
    48  
    49  	if got := km.Object(); got != object {
    50  		t.Errorf("Expected %s but got %s", object, got)
    51  	}
    52  
    53  	areEqualObjects := func(as, bs []string) bool {
    54  		if len(as) != len(bs) {
    55  			return false
    56  		}
    57  
    58  		for i, a := range as {
    59  			b := bs[i]
    60  			if a != b {
    61  				return false
    62  			}
    63  		}
    64  		return true
    65  	}
    66  
    67  	if got := km.Objects(); !areEqualObjects(got, objects) {
    68  		t.Errorf("Expected %s but got %s", objects, got)
    69  	}
    70  
    71  	if got := km.Policy(); got != policy {
    72  		t.Errorf("Expected %s but got %s", policy, got)
    73  	}
    74  
    75  	if got := km.Prefix(); got != prefix {
    76  		t.Errorf("Expected %s but got %s", prefix, got)
    77  	}
    78  
    79  	if got := km.Username(); got != username {
    80  		t.Errorf("Expected %s but got %s", username, got)
    81  	}
    82  
    83  	if got := km.Hostname(); got != host {
    84  		t.Errorf("Expected %s but got %s", host, got)
    85  	}
    86  }
    87  
    88  func TestNewWebContext(t *testing.T) {
    89  	api := "Test API"
    90  	args := ListObjectsArgs{
    91  		BucketName: "bucket",
    92  		Prefix:     "prefix",
    93  		Marker:     "marker",
    94  	}
    95  
    96  	req, err := http.NewRequest(http.MethodPost, "http://min.io", bytes.NewReader([]byte("nothing")))
    97  	if err != nil {
    98  		t.Fatal("Unexpected failure while creating a test request")
    99  	}
   100  
   101  	ctx := newWebContext(req, &args, api)
   102  	reqInfo := logger.GetReqInfo(ctx)
   103  
   104  	if reqInfo.API != api {
   105  		t.Errorf("Expected %s got %s", api, reqInfo.API)
   106  	}
   107  
   108  	if reqInfo.BucketName != args.BucketName {
   109  		t.Errorf("Expected %s got %s", args.BucketName, reqInfo.BucketName)
   110  	}
   111  }