...

Source file src/github.com/aws/aws-sdk-go-v2/feature/ec2/imds/api_op_GetDynamicData_test.go

Documentation: github.com/aws/aws-sdk-go-v2/feature/ec2/imds

     1  package imds
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/hex"
     7  	"io/ioutil"
     8  	"net/http/httptest"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestGetDynamicData(t *testing.T) {
    14  	cases := map[string]struct {
    15  		Path          string
    16  		ExpectPath    string
    17  		ExpectContent []byte
    18  		ExpectTrace   []string
    19  	}{
    20  		"empty path": {
    21  			ExpectPath:    getDynamicDataPath,
    22  			ExpectContent: []byte("success"),
    23  			ExpectTrace: []string{
    24  				getTokenPath,
    25  				getDynamicDataPath,
    26  			},
    27  		},
    28  		"with path no leading slash": {
    29  			Path:          "abc",
    30  			ExpectPath:    getDynamicDataPath + "/abc",
    31  			ExpectContent: []byte("success"),
    32  			ExpectTrace: []string{
    33  				getTokenPath,
    34  				getDynamicDataPath + "/abc",
    35  			},
    36  		},
    37  		"with path": {
    38  			Path:          "/abc",
    39  			ExpectPath:    getDynamicDataPath + "/abc",
    40  			ExpectContent: []byte("success"),
    41  			ExpectTrace: []string{
    42  				getTokenPath,
    43  				getDynamicDataPath + "/abc",
    44  			},
    45  		},
    46  		"with path trailing slash": {
    47  			Path:          "/abc/",
    48  			ExpectPath:    getDynamicDataPath + "/abc/",
    49  			ExpectContent: []byte("success"),
    50  			ExpectTrace: []string{
    51  				getTokenPath,
    52  				getDynamicDataPath + "/abc/",
    53  			},
    54  		},
    55  	}
    56  
    57  	ctx := context.Background()
    58  
    59  	for name, c := range cases {
    60  		t.Run(name, func(t *testing.T) {
    61  			trace := newRequestTrace()
    62  			server := httptest.NewServer(trace.WrapHandler(
    63  				newTestServeMux(t,
    64  					newSecureAPIHandler(t,
    65  						[]string{"tokenA"},
    66  						5*time.Minute,
    67  						&successAPIResponseHandler{t: t,
    68  							path:   c.ExpectPath,
    69  							method: "GET",
    70  							body:   append([]byte{}, c.ExpectContent...),
    71  						},
    72  					))))
    73  			defer server.Close()
    74  
    75  			// Asserts
    76  			client := New(Options{
    77  				Endpoint: server.URL,
    78  			})
    79  
    80  			resp, err := client.GetDynamicData(ctx, &GetDynamicDataInput{
    81  				Path: c.Path,
    82  			})
    83  			if err != nil {
    84  				t.Fatalf("expect no error, got %v", err)
    85  			}
    86  			if resp == nil {
    87  				t.Fatalf("expect resp, got none")
    88  			}
    89  
    90  			actualContent, err := ioutil.ReadAll(resp.Content)
    91  			if err != nil {
    92  				t.Fatalf("expect to read content, got %v", err)
    93  			}
    94  
    95  			if e, a := c.ExpectContent, actualContent; !bytes.Equal(e, a) {
    96  				t.Errorf("expect content to be equal\nexpect:\n%s\nactual:\n%s",
    97  					hex.Dump(e), hex.Dump(a))
    98  			}
    99  
   100  			if diff := cmpDiff(c.ExpectTrace, trace.requests); len(diff) != 0 {
   101  				t.Errorf("expect trace to match\n%s", diff)
   102  			}
   103  		})
   104  	}
   105  }
   106  

View as plain text