github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/xml/xmlutil/build_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package xmlutil
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/xml"
     9  	"testing"
    10  
    11  	"github.com/aavshr/aws-sdk-go/aws"
    12  )
    13  
    14  type implicitPayload struct {
    15  	_ struct{} `type:"structure"`
    16  
    17  	StrVal *string     `type:"string"`
    18  	Second *nestedType `type:"structure"`
    19  	Third  *nestedType `type:"structure"`
    20  }
    21  
    22  type namedImplicitPayload struct {
    23  	_ struct{} `type:"structure" locationName:"namedPayload"`
    24  
    25  	StrVal *string     `type:"string"`
    26  	Second *nestedType `type:"structure"`
    27  	Third  *nestedType `type:"structure"`
    28  }
    29  
    30  type explicitPayload struct {
    31  	_ struct{} `type:"structure" payload:"Second"`
    32  
    33  	Second *nestedType `type:"structure" locationName:"Second"`
    34  }
    35  
    36  type useEmptyNested struct {
    37  	_ struct{} `type:"structure" locationName:"useEmptyNested"`
    38  
    39  	StrVal *string    `type:"string"`
    40  	Empty  *emptyType `type:"structure"`
    41  }
    42  
    43  type useIgnoreNested struct {
    44  	_      struct{}      `type:"structure" locationName:"useIgnoreNested"`
    45  	StrVal *string       `type:"string"`
    46  	Ignore *ignoreNested `type:"structure"`
    47  }
    48  
    49  type skipNonPayload struct {
    50  	_     struct{} `type:"structure" locationName:"skipNonPayload"`
    51  	Field *string  `type:"string" location:"header"`
    52  }
    53  type namedEmptyPayload struct {
    54  	_ struct{} `type:"structure" locationName:"namedEmptyPayload"`
    55  }
    56  
    57  type nestedType struct {
    58  	_ struct{} `type:"structure"`
    59  
    60  	IntVal *int64  `type:"integer"`
    61  	StrVal *string `type:"string"`
    62  }
    63  
    64  type emptyType struct {
    65  	_ struct{} `type:"structure"`
    66  }
    67  
    68  type ignoreNested struct {
    69  	_ struct{} `type:"structure"`
    70  
    71  	IgnoreMe *string `type:"string" ignore:"true"`
    72  }
    73  
    74  func TestBuildXML(t *testing.T) {
    75  	cases := map[string]struct {
    76  		Input  interface{}
    77  		Expect string
    78  	}{
    79  		"explicit payload": {
    80  			Input: &explicitPayload{
    81  				Second: &nestedType{
    82  					IntVal: aws.Int64(1234),
    83  					StrVal: aws.String("string value"),
    84  				},
    85  			},
    86  			Expect: `<Second><IntVal>1234</IntVal><StrVal>string value</StrVal></Second>`,
    87  		},
    88  		"implicit payload": {
    89  			Input: &implicitPayload{
    90  				StrVal: aws.String("string value"),
    91  				Second: &nestedType{
    92  					IntVal: aws.Int64(1111),
    93  					StrVal: aws.String("second string"),
    94  				},
    95  				Third: &nestedType{
    96  					IntVal: aws.Int64(2222),
    97  					StrVal: aws.String("third string"),
    98  				},
    99  			},
   100  			Expect: `<Second><IntVal>1111</IntVal><StrVal>second string</StrVal></Second><StrVal>string value</StrVal><Third><IntVal>2222</IntVal><StrVal>third string</StrVal></Third>`,
   101  		},
   102  		"named implicit payload": {
   103  			Input: &namedImplicitPayload{
   104  				StrVal: aws.String("string value"),
   105  				Second: &nestedType{
   106  					IntVal: aws.Int64(1111),
   107  					StrVal: aws.String("second string"),
   108  				},
   109  				Third: &nestedType{
   110  					IntVal: aws.Int64(2222),
   111  					StrVal: aws.String("third string"),
   112  				},
   113  			},
   114  			Expect: `<namedPayload><Second><IntVal>1111</IntVal><StrVal>second string</StrVal></Second><StrVal>string value</StrVal><Third><IntVal>2222</IntVal><StrVal>third string</StrVal></Third></namedPayload>`,
   115  		},
   116  		"empty with fields nested type": {
   117  			Input: &namedImplicitPayload{
   118  				StrVal: aws.String("string value"),
   119  				Second: &nestedType{},
   120  				Third: &nestedType{
   121  					IntVal: aws.Int64(2222),
   122  					StrVal: aws.String("third string"),
   123  				},
   124  			},
   125  			Expect: `<namedPayload><Second></Second><StrVal>string value</StrVal><Third><IntVal>2222</IntVal><StrVal>third string</StrVal></Third></namedPayload>`,
   126  		},
   127  		"empty no fields nested type": {
   128  			Input: &useEmptyNested{
   129  				StrVal: aws.String("string value"),
   130  				Empty:  &emptyType{},
   131  			},
   132  			Expect: `<useEmptyNested><Empty></Empty><StrVal>string value</StrVal></useEmptyNested>`,
   133  		},
   134  		"ignored nested field": {
   135  			Input: &useIgnoreNested{
   136  				StrVal: aws.String("string value"),
   137  				Ignore: &ignoreNested{
   138  					IgnoreMe: aws.String("abc123"),
   139  				},
   140  			},
   141  			Expect: `<useIgnoreNested><Ignore></Ignore><StrVal>string value</StrVal></useIgnoreNested>`,
   142  		},
   143  		"skip non payload root": {
   144  			Input: &skipNonPayload{
   145  				Field: aws.String("value"),
   146  			},
   147  			Expect: "",
   148  		},
   149  		"skip empty root": {
   150  			Input:  &emptyType{},
   151  			Expect: "",
   152  		},
   153  		"named empty payload": {
   154  			Input:  &namedEmptyPayload{},
   155  			Expect: "<namedEmptyPayload></namedEmptyPayload>",
   156  		},
   157  		"escape line feed and carriage return": {
   158  			Input: &implicitPayload{
   159  				StrVal: aws.String("this\nstring\rhas\r\nescapable\n\rcharacters"),
   160  			},
   161  			Expect: "<StrVal>this&#xA;string&#xD;has&#xD;&#xA;escapable&#xA;&#xD;characters</StrVal>",
   162  		},
   163  	}
   164  
   165  	for name, c := range cases {
   166  		t.Run(name, func(t *testing.T) {
   167  			var w bytes.Buffer
   168  			if err := buildXML(c.Input, xml.NewEncoder(&w), true); err != nil {
   169  				t.Fatalf("expect no error, %v", err)
   170  			}
   171  
   172  			if e, a := c.Expect, w.String(); e != a {
   173  				t.Errorf("expect:\n%s\nactual:\n%s\n", e, a)
   174  			}
   175  		})
   176  	}
   177  }