...

Source file src/github.com/aws/smithy-go/encoding/httpbinding/uri_test.go

Documentation: github.com/aws/smithy-go/encoding/httpbinding

     1  package httpbinding
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"reflect"
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestURIValue(t *testing.T) {
    12  	const uriKey = "someKey"
    13  	const path = "/some/{someKey}/{path+}"
    14  
    15  	type expected struct {
    16  		path string
    17  		raw  string
    18  	}
    19  
    20  	cases := map[string]struct {
    21  		path     string
    22  		args     []interface{}
    23  		expected expected
    24  	}{
    25  		"bool": {
    26  			path: path,
    27  			args: []interface{}{true},
    28  			expected: expected{
    29  				path: "/some/true/{path+}",
    30  				raw:  "/some/true/{path+}",
    31  			},
    32  		},
    33  		"string": {
    34  			path: path,
    35  			args: []interface{}{"someValue"},
    36  			expected: expected{
    37  				path: "/some/someValue/{path+}",
    38  				raw:  "/some/someValue/{path+}",
    39  			},
    40  		},
    41  		"byte": {
    42  			path: path,
    43  			args: []interface{}{int8(127)},
    44  			expected: expected{
    45  				path: "/some/127/{path+}",
    46  				raw:  "/some/127/{path+}",
    47  			},
    48  		},
    49  		"short": {
    50  			path: path,
    51  			args: []interface{}{int16(32767)},
    52  			expected: expected{
    53  				path: "/some/32767/{path+}",
    54  				raw:  "/some/32767/{path+}",
    55  			},
    56  		},
    57  		"integer": {
    58  			path: path,
    59  			args: []interface{}{int32(2147483647)},
    60  			expected: expected{
    61  				path: "/some/2147483647/{path+}",
    62  				raw:  "/some/2147483647/{path+}",
    63  			},
    64  		},
    65  		"long": {
    66  			path: path,
    67  			args: []interface{}{int64(9223372036854775807)},
    68  			expected: expected{
    69  				path: "/some/9223372036854775807/{path+}",
    70  				raw:  "/some/9223372036854775807/{path+}",
    71  			},
    72  		},
    73  		"float32": {
    74  			path: path,
    75  			args: []interface{}{float32(3.14159)},
    76  			expected: expected{
    77  				path: "/some/3.14159/{path+}",
    78  				raw:  "/some/3.14159/{path+}",
    79  			},
    80  		},
    81  		"float64": {
    82  			path: path,
    83  			args: []interface{}{float64(3.14159)},
    84  			expected: expected{
    85  				path: "/some/3.14159/{path+}",
    86  				raw:  "/some/3.14159/{path+}",
    87  			},
    88  		},
    89  		"bigInteger": {
    90  			path: path,
    91  			args: []interface{}{new(big.Int).SetInt64(1)},
    92  			expected: expected{
    93  				path: "/some/1/{path+}",
    94  				raw:  "/some/1/{path+}",
    95  			},
    96  		},
    97  		"bigDecimal": {
    98  			path: path,
    99  			args: []interface{}{new(big.Float).SetFloat64(1024.10241024)},
   100  			expected: expected{
   101  				path: "/some/1.02410241024e+03/{path+}",
   102  				raw:  "/some/1.02410241024e%2B03/{path+}",
   103  			},
   104  		},
   105  	}
   106  
   107  	buffer := make([]byte, 1024)
   108  
   109  	for name, tt := range cases {
   110  		t.Run(name, func(t *testing.T) {
   111  			pBytes, rBytes := []byte(tt.path), []byte(tt.path)
   112  
   113  			uv := newURIValue(&pBytes, &rBytes, &buffer, uriKey)
   114  
   115  			if err := setURI(uv, tt.args); err != nil {
   116  				t.Fatalf("expected no error, %v", err)
   117  			}
   118  
   119  			if e, a := tt.expected.path, string(pBytes); e != a {
   120  				t.Errorf("expected %v, got %v", e, a)
   121  			}
   122  
   123  			if e, a := tt.expected.raw, string(rBytes); e != a {
   124  				t.Errorf("expected %v, got %v", e, a)
   125  			}
   126  		})
   127  	}
   128  }
   129  
   130  func setURI(uv URIValue, args []interface{}) error {
   131  	value := args[0]
   132  
   133  	switch value.(type) {
   134  	case bool:
   135  		return reflectCall(reflect.ValueOf(uv.Boolean), args)
   136  	case string:
   137  		return reflectCall(reflect.ValueOf(uv.String), args)
   138  	case int8:
   139  		return reflectCall(reflect.ValueOf(uv.Byte), args)
   140  	case int16:
   141  		return reflectCall(reflect.ValueOf(uv.Short), args)
   142  	case int32:
   143  		return reflectCall(reflect.ValueOf(uv.Integer), args)
   144  	case int64:
   145  		return reflectCall(reflect.ValueOf(uv.Long), args)
   146  	case float32:
   147  		return reflectCall(reflect.ValueOf(uv.Float), args)
   148  	case float64:
   149  		return reflectCall(reflect.ValueOf(uv.Double), args)
   150  	case *big.Int:
   151  		return reflectCall(reflect.ValueOf(uv.BigInteger), args)
   152  	case *big.Float:
   153  		return reflectCall(reflect.ValueOf(uv.BigDecimal), args)
   154  	default:
   155  		return fmt.Errorf("unhandled value type")
   156  	}
   157  }
   158  
   159  func TestParseURI(t *testing.T) {
   160  	cases := []struct {
   161  		Value string
   162  		Path  string
   163  		Query string
   164  	}{
   165  		{
   166  			Value: "/my/uri/foo/bar/baz",
   167  			Path:  "/my/uri/foo/bar/baz",
   168  			Query: "",
   169  		},
   170  		{
   171  			Value: "/path?requiredKey",
   172  			Path:  "/path",
   173  			Query: "requiredKey",
   174  		},
   175  		{
   176  			Value: "/path?",
   177  			Path:  "/path",
   178  			Query: "",
   179  		},
   180  		{
   181  			Value: "?",
   182  			Path:  "",
   183  			Query: "",
   184  		},
   185  	}
   186  
   187  	for i, tt := range cases {
   188  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   189  			path, query := SplitURI(tt.Value)
   190  			if e, a := tt.Path, path; e != a {
   191  				t.Errorf("expected %v, got %v", e, a)
   192  			}
   193  			if e, a := tt.Query, query; e != a {
   194  				t.Errorf("expected %v, got %v", e, a)
   195  			}
   196  		})
   197  	}
   198  }
   199  

View as plain text