github.com/minio/console@v1.3.0/integration/inspect_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func Inspect(volume string, file string, enc bool) (*http.Response, error) {
    14  	requestURL := fmt.Sprintf("http://localhost:9090/api/v1/admin/inspect?volume=%s&file=%s&encrypt=%t", volume, file, enc)
    15  	request, err := http.NewRequest(
    16  		"GET", requestURL, nil)
    17  	if err != nil {
    18  		log.Println(err)
    19  	}
    20  	request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
    21  	request.Header.Add("Content-Type", "application/json")
    22  	client := &http.Client{
    23  		Timeout: 2 * time.Second,
    24  	}
    25  	response, err := client.Do(request)
    26  	return response, err
    27  }
    28  
    29  func TestInspect(t *testing.T) {
    30  	assert := assert.New(t)
    31  
    32  	type args struct {
    33  		volume  string
    34  		file    string
    35  		encrypt bool
    36  	}
    37  
    38  	// Inspect returns successful response always
    39  	tests := []struct {
    40  		name          string
    41  		args          args
    42  		expStatusCode int
    43  		expectedError bool
    44  	}{
    45  		{
    46  			name: "Test Invalid Path",
    47  			args: args{
    48  				volume:  "/test-with-slash",
    49  				file:    "/test-with-slash",
    50  				encrypt: false,
    51  			},
    52  			expStatusCode: 200,
    53  			expectedError: false,
    54  		},
    55  
    56  		{
    57  			name: "Test Invalid characters in Path",
    58  			args: args{
    59  				volume:  "//test",
    60  				file:    "//bucket",
    61  				encrypt: false,
    62  			},
    63  			expStatusCode: 200,
    64  			expectedError: true,
    65  		},
    66  		{
    67  			name: "Test valid bucket",
    68  			args: args{
    69  				volume:  "test-bucket",
    70  				file:    "test.txt",
    71  				encrypt: true,
    72  			},
    73  			expStatusCode: 200,
    74  			expectedError: false,
    75  		},
    76  		{
    77  			name: "Test Empty Path", // Un processable entity error
    78  			args: args{
    79  				volume:  "",
    80  				file:    "",
    81  				encrypt: false,
    82  			},
    83  			expStatusCode: 422,
    84  			expectedError: false,
    85  		},
    86  	}
    87  
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(_ *testing.T) {
    90  			resp, err := Inspect(tt.args.volume, tt.args.file, tt.args.encrypt)
    91  			if tt.expectedError {
    92  				assert.Nil(err)
    93  				if err != nil {
    94  					log.Println(err)
    95  					return
    96  				}
    97  			}
    98  			if resp != nil {
    99  				assert.Equal(
   100  					tt.expStatusCode,
   101  					resp.StatusCode,
   102  				)
   103  			}
   104  		})
   105  	}
   106  }