1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package ec2
4
5import (
6	"context"
7	"github.com/aws/aws-sdk-go-v2/aws"
8	"github.com/aws/aws-sdk-go-v2/internal/awstesting/unit"
9	presignedurlcust "github.com/aws/aws-sdk-go-v2/service/internal/presigned-url"
10	"github.com/aws/smithy-go/middleware"
11	smithyhttp "github.com/aws/smithy-go/transport/http"
12	"net/http"
13	"strings"
14	"testing"
15)
16
17func TestClientCopySnapshot_presignURLCustomization(t *testing.T) {
18	cases := map[string]struct {
19		Input                               *CopySnapshotInput
20		ClientRegion                        string
21		ExpectPresignedURL                  string
22		ExpectPresignedURLDestinationRegion string
23		ExpectRequestURL                    string
24		ExpectErr                           string
25	}{
26		"have presigned URL no auto fill": {
27			Input: &CopySnapshotInput{
28				PresignedUrl: aws.String("https://example.aws/signed-url"),
29			},
30			ClientRegion:       "mock-region",
31			ExpectPresignedURL: "https://example.aws/signed-url",
32			ExpectRequestURL:   "https://service.mock-region.amazonaws.com/",
33		},
34
35		"no source region no auto fill": {
36			Input:            &CopySnapshotInput{},
37			ClientRegion:     "mock-region",
38			ExpectRequestURL: "https://service.mock-region.amazonaws.com/",
39		},
40
41		"auto fill presign URL matching region": {
42			Input: &CopySnapshotInput{
43				SourceRegion: aws.String("mock-region"),
44			},
45			ClientRegion:                        "mock-region",
46			ExpectPresignedURL:                  "https://service.mock-region.amazonaws.com/",
47			ExpectPresignedURLDestinationRegion: "DestinationRegion=mock-region",
48			ExpectRequestURL:                    "https://service.mock-region.amazonaws.com/",
49		},
50
51		"auto fill presign URL different region": {
52			Input: &CopySnapshotInput{
53				SourceRegion: aws.String("mock-other-region"),
54			},
55			ClientRegion:                        "mock-region",
56			ExpectPresignedURL:                  "https://service.mock-other-region.amazonaws.com/",
57			ExpectPresignedURLDestinationRegion: "DestinationRegion=mock-region",
58			ExpectRequestURL:                    "https://service.mock-region.amazonaws.com/",
59		},
60	}
61
62	for name, c := range cases {
63		t.Run(name, func(t *testing.T) {
64			client := New(Options{
65				Region:      c.ClientRegion,
66				Credentials: unit.StubCredentialsProvider{},
67				HTTPClient: smithyhttp.ClientDoFunc(func(r *http.Request) (*http.Response, error) {
68					if e, a := c.ExpectRequestURL, r.URL.String(); !strings.HasPrefix(a, e) {
69						t.Errorf("expect presigned URL to contain %v, got %v", e, a)
70					}
71					return smithyhttp.NopClient{}.Do(r)
72				}),
73				EndpointResolver: EndpointResolverFunc(
74					func(region string, options EndpointResolverOptions) (aws.Endpoint, error) {
75						return aws.Endpoint{
76							URL:           "https://service." + region + ".amazonaws.com",
77							SigningRegion: c.ClientRegion,
78						}, nil
79					}),
80			})
81
82			_, err := client.CopySnapshot(context.Background(), c.Input,
83				func(o *Options) {
84					o.APIOptions = append(o.APIOptions, func(stack *middleware.Stack) (err error) {
85						_, err = stack.Initialize.Remove("OperationInputValidation")
86						if err != nil {
87							return err
88						}
89
90						return stack.Serialize.Add(middleware.SerializeMiddlewareFunc(t.Name(),
91							func(
92								ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
93							) (
94								out middleware.SerializeOutput, metadata middleware.Metadata, err error,
95							) {
96								input, ok := in.Parameters.(*CopySnapshotInput)
97								if !ok {
98									t.Fatalf("expect CopySnapshotInput, got %T", in.Parameters)
99								}
100
101								// Switch based on if presign flow or not
102								if presignedurlcust.GetIsPresigning(ctx) {
103									// Presign Flow
104									if v := input.PresignedUrl; v != nil {
105										t.Errorf("expect no presigned URL, got %v", *v)
106									}
107									if input.destinationRegion == nil {
108										t.Fatalf("expect destination region to be set")
109									}
110									if e, a := c.ClientRegion, *input.destinationRegion; e != a {
111										t.Errorf("expect %v destination region, got %v", e, a)
112									}
113
114								} else {
115									// Operation flow
116									if v := input.destinationRegion; v != nil {
117										t.Errorf("expect no destination region, got %v", *v)
118									}
119
120									if len(c.ExpectPresignedURL) != 0 {
121										if input.PresignedUrl == nil {
122											t.Fatalf("expect presigned URL, got none")
123										}
124
125										if e, a := c.ExpectPresignedURL, *input.PresignedUrl; !strings.HasPrefix(a, e) {
126											t.Errorf("expect presigned URL to contain %v, got %v", e, a)
127										}
128										if e, a := c.ExpectPresignedURLDestinationRegion, *input.PresignedUrl; !strings.Contains(a, e) {
129											t.Errorf("expect presigned URL destination region to contain %v, got %v", e, a)
130										}
131										return next.HandleSerialize(ctx, in)
132									}
133									if v := input.PresignedUrl; v != nil {
134										t.Errorf("expect no presigned url, got %v", *v)
135									}
136								}
137
138								return next.HandleSerialize(ctx, in)
139							},
140						), middleware.After)
141					})
142				},
143			)
144
145			if len(c.ExpectErr) != 0 {
146				if err == nil {
147					t.Fatalf("expect error, got none")
148				}
149				if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
150					t.Fatalf("expect error to contain %v, got %v", e, a)
151				}
152				return
153			}
154			if err != nil {
155				t.Fatalf("expect no error, got %v", err)
156			}
157		})
158	}
159}
160