github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/xml/xmlutil/unmarshal_test.go (about) 1 package xmlutil 2 3 import ( 4 "encoding/xml" 5 "fmt" 6 "io" 7 "reflect" 8 "strings" 9 "testing" 10 11 "github.com/aavshr/aws-sdk-go/aws" 12 "github.com/aavshr/aws-sdk-go/aws/awsutil" 13 ) 14 15 type mockBody struct { 16 DoneErr error 17 Body io.Reader 18 } 19 20 func (m *mockBody) Read(p []byte) (int, error) { 21 n, err := m.Body.Read(p) 22 if (n == 0 || err == io.EOF) && m.DoneErr != nil { 23 return n, m.DoneErr 24 } 25 26 return n, err 27 } 28 29 type mockOutput struct { 30 _ struct{} `type:"structure"` 31 String *string `type:"string"` 32 Integer *int64 `type:"integer"` 33 Nested *mockNestedStruct `type:"structure"` 34 List []*mockListElem `locationName:"List" locationNameList:"Elem" type:"list"` 35 Closed *mockClosedTags `type:"structure"` 36 } 37 type mockNestedStruct struct { 38 _ struct{} `type:"structure"` 39 NestedString *string `type:"string"` 40 NestedInt *int64 `type:"integer"` 41 } 42 type mockClosedTags struct { 43 _ struct{} `type:"structure" xmlPrefix:"xsi" xmlURI:"http://www.w3.org/2001/XMLSchema-instance"` 44 Attr *string `locationName:"xsi:attrval" type:"string" xmlAttribute:"true"` 45 } 46 type mockListElem struct { 47 _ struct{} `type:"structure" xmlPrefix:"xsi" xmlURI:"http://www.w3.org/2001/XMLSchema-instance"` 48 String *string `type:"string"` 49 NestedElem *mockNestedListElem `type:"structure"` 50 } 51 type mockNestedListElem struct { 52 _ struct{} `type:"structure" xmlPrefix:"xsi" xmlURI:"http://www.w3.org/2001/XMLSchema-instance"` 53 54 String *string `type:"string"` 55 Type *string `locationName:"xsi:type" type:"string" xmlAttribute:"true"` 56 } 57 58 func TestUnmarshal(t *testing.T) { 59 const xmlBodyStr = `<?xml version="1.0" encoding="UTF-8"?> 60 <MockResponse xmlns="http://xmlns.example.com"> 61 <String>string value</String> 62 <Integer>123</Integer> 63 <Closed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:attrval="attr value"/> 64 <Nested> 65 <NestedString>nested string value</NestedString> 66 <NestedInt>321</NestedInt> 67 </Nested> 68 <List> 69 <Elem> 70 <NestedElem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="type"> 71 <String>nested elem string value</String> 72 </NestedElem> 73 <String>elem string value</String> 74 </Elem> 75 </List> 76 </MockResponse>` 77 78 expect := mockOutput{ 79 String: aws.String("string value"), 80 Integer: aws.Int64(123), 81 Closed: &mockClosedTags{ 82 Attr: aws.String("attr value"), 83 }, 84 Nested: &mockNestedStruct{ 85 NestedString: aws.String("nested string value"), 86 NestedInt: aws.Int64(321), 87 }, 88 List: []*mockListElem{ 89 { 90 String: aws.String("elem string value"), 91 NestedElem: &mockNestedListElem{ 92 String: aws.String("nested elem string value"), 93 Type: aws.String("type"), 94 }, 95 }, 96 }, 97 } 98 99 actual := mockOutput{} 100 decoder := xml.NewDecoder(strings.NewReader(xmlBodyStr)) 101 err := UnmarshalXML(&actual, decoder, "") 102 if err != nil { 103 t.Fatalf("expect no error, got %v", err) 104 } 105 106 if !reflect.DeepEqual(expect, actual) { 107 t.Errorf("expect unmarshal to match\nExpect: %s\nActual: %s", 108 awsutil.Prettify(expect), awsutil.Prettify(actual)) 109 } 110 } 111 112 func TestUnmarshal_UnexpectedEOF(t *testing.T) { 113 const partialXMLBody = `<?xml version="1.0" encoding="UTF-8"?> 114 <First>first value</First> 115 <Second>Second val` 116 117 out := struct { 118 First *string `locationName:"First" type:"string"` 119 Second *string `locationName:"Second" type:"string"` 120 }{} 121 122 expect := out 123 expect.First = aws.String("first") 124 expect.Second = aws.String("second") 125 126 expectErr := fmt.Errorf("expected read error") 127 128 body := &mockBody{ 129 DoneErr: expectErr, 130 Body: strings.NewReader(partialXMLBody), 131 } 132 133 decoder := xml.NewDecoder(body) 134 err := UnmarshalXML(&out, decoder, "") 135 136 if err == nil { 137 t.Fatalf("expect error, got none") 138 } 139 if e, a := expectErr, err; e != a { 140 t.Errorf("expect %v error in %v, but was not", e, a) 141 } 142 }