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

Function testCors

functional_tests.go:13654–14376  ·  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

13652// Recreate this manually each time. Minio-go SDK does not support calling
13653// SetPublicBucket (put-public-access-block) on S3, otherwise we could script the whole thing.
13654func testCors() {
13655 ctx := context.Background()
13656 startTime := time.Now()
13657 testName := getFuncName()
13658 function := "SetBucketCors(bucketName, cors)"
13659 args := map[string]interface{}{
13660 "bucketName": "",
13661 "cors": "",
13662 }
13663
13664 c, err := NewClient(ClientConfig{})
13665 if err != nil {
13666 logError(testName, function, args, startTime, "", "MinIO client object creation failed", err)
13667 return
13668 }
13669
13670 // Create or reuse a bucket that will get cors settings applied to it and deleted when done
13671 bucketName := os.Getenv("MINIO_GO_TEST_BUCKET_CORS")
13672 if bucketName == "" {
13673 bucketName = randString(60, rand.NewSource(time.Now().UnixNano()), "minio-go-test-")
13674 err = c.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: "us-east-1"})
13675 if err != nil {
13676 logError(testName, function, args, startTime, "", "MakeBucket failed", err)
13677 return
13678 }
13679 }
13680 args["bucketName"] = bucketName
13681 defer cleanupBucket(bucketName, c)
13682
13683 publicPolicy := `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:*"],"Resource":["arn:aws:s3:::` + bucketName + `", "arn:aws:s3:::` + bucketName + `/*"]}]}`
13684 err = c.SetBucketPolicy(ctx, bucketName, publicPolicy)
13685 if err != nil {
13686 logError(testName, function, args, startTime, "", "SetBucketPolicy failed", err)
13687 return
13688 }
13689
13690 // Upload an object for testing.
13691 objectContents := `some-text-file-contents`
13692 reader := strings.NewReader(objectContents)
13693 bufSize := int64(len(objectContents))
13694
13695 objectName := randString(60, rand.NewSource(time.Now().UnixNano()), "")
13696 args["objectName"] = objectName
13697
13698 _, err = c.PutObject(ctx, bucketName, objectName, reader, int64(bufSize), minio.PutObjectOptions{ContentType: "binary/octet-stream"})
13699 if err != nil {
13700 logError(testName, function, args, startTime, "", "PutObject call failed", err)
13701 return
13702 }
13703 bucketURL := c.EndpointURL().String() + "/" + bucketName + "/"
13704 objectURL := bucketURL + objectName
13705
13706 httpClient := &http.Client{
13707 Timeout: 30 * time.Second,
13708 Transport: createHTTPTransport(),
13709 }
13710
13711 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