github.com/aavshr/aws-sdk-go@v1.41.3/aws/request/request_1_8_test.go (about)

     1  //go:build go1.8
     2  // +build go1.8
     3  
     4  package request_test
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/aavshr/aws-sdk-go/aws"
    15  	"github.com/aavshr/aws-sdk-go/aws/request"
    16  	"github.com/aavshr/aws-sdk-go/awstesting"
    17  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    18  )
    19  
    20  func TestResetBody_WithEmptyBody(t *testing.T) {
    21  	r := request.Request{
    22  		HTTPRequest: &http.Request{},
    23  	}
    24  
    25  	reader := strings.NewReader("")
    26  	r.Body = reader
    27  
    28  	r.ResetBody()
    29  
    30  	if a, e := r.HTTPRequest.Body, http.NoBody; a != e {
    31  		t.Errorf("expected request body to be set to reader, got %#v",
    32  			r.HTTPRequest.Body)
    33  	}
    34  }
    35  
    36  func TestRequest_FollowPUTRedirects(t *testing.T) {
    37  	const bodySize = 1024
    38  
    39  	redirectHit := 0
    40  	endpointHit := 0
    41  
    42  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    43  		switch r.URL.Path {
    44  		case "/redirect-me":
    45  			u := *r.URL
    46  			u.Path = "/endpoint"
    47  			w.Header().Set("Location", u.String())
    48  			w.WriteHeader(307)
    49  			redirectHit++
    50  		case "/endpoint":
    51  			b := bytes.Buffer{}
    52  			io.Copy(&b, r.Body)
    53  			r.Body.Close()
    54  			if e, a := bodySize, b.Len(); e != a {
    55  				t.Fatalf("expect %d body size, got %d", e, a)
    56  			}
    57  			endpointHit++
    58  		default:
    59  			t.Fatalf("unexpected endpoint used, %q", r.URL.String())
    60  		}
    61  	}))
    62  	defer server.Close()
    63  
    64  	svc := awstesting.NewClient(&aws.Config{
    65  		Region:     unit.Session.Config.Region,
    66  		DisableSSL: aws.Bool(true),
    67  		Endpoint:   aws.String(server.URL),
    68  	})
    69  
    70  	req := svc.NewRequest(&request.Operation{
    71  		Name:       "Operation",
    72  		HTTPMethod: "PUT",
    73  		HTTPPath:   "/redirect-me",
    74  	}, &struct{}{}, &struct{}{})
    75  	req.SetReaderBody(bytes.NewReader(make([]byte, bodySize)))
    76  
    77  	err := req.Send()
    78  	if err != nil {
    79  		t.Errorf("expect no error, got %v", err)
    80  	}
    81  	if e, a := 1, redirectHit; e != a {
    82  		t.Errorf("expect %d redirect hits, got %d", e, a)
    83  	}
    84  	if e, a := 1, endpointHit; e != a {
    85  		t.Errorf("expect %d endpoint hits, got %d", e, a)
    86  	}
    87  }
    88  
    89  func TestNewRequest_JoinEndpointWithOperationPathQuery(t *testing.T) {
    90  	cases := map[string]struct {
    91  		HTTPPath    string
    92  		Endpoint    *string
    93  		ExpectQuery string
    94  		ExpectPath  string
    95  	}{
    96  		"no op HTTP Path": {
    97  			HTTPPath:    "",
    98  			Endpoint:    aws.String("https://foo.bar.aws/foo?bar=Baz"),
    99  			ExpectPath:  "/foo",
   100  			ExpectQuery: "bar=Baz",
   101  		},
   102  		"no trailing slash": {
   103  			HTTPPath:    "/",
   104  			Endpoint:    aws.String("https://foo.bar.aws"),
   105  			ExpectPath:  "/",
   106  			ExpectQuery: "",
   107  		},
   108  		"set query": {
   109  			HTTPPath:    "/?Foo=bar",
   110  			Endpoint:    aws.String("https://foo.bar.aws"),
   111  			ExpectPath:  "/",
   112  			ExpectQuery: "Foo=bar",
   113  		},
   114  		"squash query": {
   115  			HTTPPath:    "/?Foo=bar",
   116  			Endpoint:    aws.String("https://foo.bar.aws/?bar=Foo"),
   117  			ExpectPath:  "/",
   118  			ExpectQuery: "Foo=bar",
   119  		},
   120  		"trailing slash": {
   121  			HTTPPath:    "/",
   122  			Endpoint:    aws.String("https://foo.bar.aws/"),
   123  			ExpectPath:  "/",
   124  			ExpectQuery: "",
   125  		},
   126  		"trailing slash set query": {
   127  			HTTPPath:    "/?Foo=bar",
   128  			Endpoint:    aws.String("https://foo.bar.aws/"),
   129  			ExpectPath:  "/",
   130  			ExpectQuery: "Foo=bar",
   131  		},
   132  	}
   133  
   134  	for name, c := range cases {
   135  		t.Run(name, func(t *testing.T) {
   136  			client := awstesting.NewClient(&aws.Config{
   137  				Endpoint: c.Endpoint,
   138  			})
   139  
   140  			client.Handlers.Clear()
   141  			r := client.NewRequest(&request.Operation{
   142  				Name:       "FooBar",
   143  				HTTPMethod: "GET",
   144  				HTTPPath:   c.HTTPPath,
   145  			}, nil, nil)
   146  
   147  			if e, a := c.ExpectPath, r.HTTPRequest.URL.Path; e != a {
   148  				t.Errorf("expect %v path, got %v", e, a)
   149  			}
   150  			if e, a := c.ExpectQuery, r.HTTPRequest.URL.RawQuery; e != a {
   151  				t.Errorf("expect %v query, got %v", e, a)
   152  			}
   153  		})
   154  	}
   155  }