MCPcopy
hub / github.com/minio/minio-go / testCors

Function testCors

functional_tests.go:13758–14480  ·  view source on GitHub ↗

testCors is runnable against S3 itself. Just provide the env var MINIO_GO_TEST_BUCKET_CORS with bucket that is public and WILL BE DELETED. Recreate this manually each time. Minio-go SDK does not support calling SetPublicBucket (put-public-access-block) on S3, otherwise we could script the whole thin

()

Source from the content-addressed store, hash-verified

13756// Recreate this manually each time. Minio-go SDK does not support calling
13757// SetPublicBucket (put-public-access-block) on S3, otherwise we could script the whole thing.
13758func testCors() {
13759 ctx := context.Background()
13760 startTime := time.Now()
13761 testName := getFuncName()
13762 function := "SetBucketCors(bucketName, cors)"
13763 args := map[string]interface{}{
13764 "bucketName": "",
13765 "cors": "",
13766 }
13767
13768 c, err := NewClient(ClientConfig{})
13769 if err != nil {
13770 logError(testName, function, args, startTime, "", "MinIO client object creation failed", err)
13771 return
13772 }
13773
13774 // Create or reuse a bucket that will get cors settings applied to it and deleted when done
13775 bucketName := os.Getenv("MINIO_GO_TEST_BUCKET_CORS")
13776 if bucketName == "" {
13777 bucketName = randString(60, rand.NewSource(time.Now().UnixNano()), "minio-go-test-")
13778 err = c.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: "us-east-1"})
13779 if err != nil {
13780 logError(testName, function, args, startTime, "", "MakeBucket failed", err)
13781 return
13782 }
13783 }
13784 args["bucketName"] = bucketName
13785 defer cleanupBucket(bucketName, c)
13786
13787 publicPolicy := `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:*"],"Resource":["arn:aws:s3:::` + bucketName + `", "arn:aws:s3:::` + bucketName + `/*"]}]}`
13788 err = c.SetBucketPolicy(ctx, bucketName, publicPolicy)
13789 if err != nil {
13790 logError(testName, function, args, startTime, "", "SetBucketPolicy failed", err)
13791 return
13792 }
13793
13794 // Upload an object for testing.
13795 objectContents := `some-text-file-contents`
13796 reader := strings.NewReader(objectContents)
13797 bufSize := int64(len(objectContents))
13798
13799 objectName := randString(60, rand.NewSource(time.Now().UnixNano()), "")
13800 args["objectName"] = objectName
13801
13802 _, err = c.PutObject(ctx, bucketName, objectName, reader, int64(bufSize), minio.PutObjectOptions{ContentType: "binary/octet-stream"})
13803 if err != nil {
13804 logError(testName, function, args, startTime, "", "PutObject call failed", err)
13805 return
13806 }
13807 bucketURL := c.EndpointURL().String() + "/" + bucketName + "/"
13808 objectURL := bucketURL + objectName
13809
13810 httpClient := &http.Client{
13811 Timeout: 30 * time.Second,
13812 Transport: createHTTPTransport(),
13813 }
13814
13815 errStrAccessForbidden := `<Error><Code>AccessForbidden</Code><Message>CORSResponse: This CORS request is not allowed. This is usually because the evalution of Origin, request method / Access-Control-Request-Method or Access-Control-Request-Headers are not whitelisted`

Callers 1

mainFunction · 0.85

Calls 15

MakeBucketMethod · 0.95
SetBucketPolicyMethod · 0.95
PutObjectMethod · 0.95
EndpointURLMethod · 0.95
SetBucketCorsMethod · 0.95
getFuncNameFunction · 0.85
NewClientFunction · 0.85
logErrorFunction · 0.85
cleanupBucketFunction · 0.85
createHTTPTransportFunction · 0.85
logSuccessFunction · 0.85
DoMethod · 0.80

Tested by

no test coverage detected