MCPcopy Create free account
hub / github.com/aws/smithy-go / TestXMLNodeDecoder_TokenExample

Function TestXMLNodeDecoder_TokenExample

encoding/xml/xml_decoder_test.go:116–176  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

114}
115
116func TestXMLNodeDecoder_TokenExample(t *testing.T) {
117 responseBody := bytes.NewReader([]byte(`<Struct><Response>abc</Response></Struct>`))
118
119 xmlDecoder := xml.NewDecoder(responseBody)
120 // Fetches <Struct> tag as start element.
121 st, err := FetchRootElement(xmlDecoder)
122 if err != nil {
123 t.Fatalf("Expected no error, got %v", err)
124 }
125
126 // nodeDecoder will track <Struct> tag as root node of the document
127 nodeDecoder := WrapNodeDecoder(xmlDecoder, st)
128
129 // Retrieves <Response> tag
130 token, done, err := nodeDecoder.Token()
131 if err != nil {
132 t.Fatalf("Expected no error, got %v", err)
133
134 }
135
136 expect := xml.StartElement{Name: xml.Name{Local: "Response"}, Attr: []xml.Attr{}}
137 if !reflect.DeepEqual(expect, token) {
138 t.Fatalf("Found diff : %v != %v", expect, token)
139 }
140 if done {
141 t.Fatalf("expected decoding to not be done yet")
142 }
143
144 // Skips the value and gets </Response> that is the end token of previously retrieved <Response> tag.
145 // The way node decoder works it only keeps track of the root start tag using which it was initialized.
146 // Here <Struct> is used to initialize, while</Response> is end element corresponding to already read
147 // <Response> tag. We won't be done until we receive </Struct>
148 token, done, err = nodeDecoder.Token()
149 if err != nil {
150 t.Fatalf("Expected no error, got %v", err)
151
152 }
153
154 expect = xml.StartElement{Name: xml.Name{Local: ""}, Attr: nil}
155 if !reflect.DeepEqual(expect, token) {
156 t.Fatalf("Found diff : %v != %v", expect, token)
157 }
158 if done {
159 t.Fatalf("expected decoding to not be done yet")
160 }
161
162 // Retrieves </Struct> end element tag corresponding to <Struct> tag.
163 // Since we got the end element that corresponds to the start element being track, we are done decoding.
164 token, done, err = nodeDecoder.Token()
165 if err != nil {
166 t.Fatalf("Expected no error, got %v", err)
167
168 }
169
170 if !reflect.DeepEqual(expect, token) {
171 t.Fatalf("%v != %v", expect, token)
172 }
173 if !done {

Callers

nothing calls this directly

Calls 3

FetchRootElementFunction · 0.85
WrapNodeDecoderFunction · 0.85
TokenMethod · 0.80

Tested by

no test coverage detected