Skip to content

Instantly share code, notes, and snippets.

@bmizerany
Created December 12, 2011 22:09
Show Gist options
  • Save bmizerany/1469359 to your computer and use it in GitHub Desktop.
Save bmizerany/1469359 to your computer and use it in GitHub Desktop.
Experimenting with Go's encoding/xml package.
--- FAIL: aws.TestUnmarshalError (0.01 seconds)
/Users/blake/src/aws.go/aws_test.go:55
! "AuthFailure" != ""

I'm trying to get a handle on how the XML package interprets and unmarshals XML and the structs tags.

I have a test that passes the first assert on RequestId, but fails on Code and Message; it's here: https://gist.github.com/1469359

I'm trying to only get the first error message in <Errors>.

I've tried to use a slice with no success either.

If anyone has experience with this package, I'd greatly appreciate some insight.

func TestUnmarshalError(t *testing.T) {
body := strings.NewReader(`
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Errors>
<Error>
<Code>AuthFailure</Code>
<Message>AWS was not able to validate the provided access credentials</Message>
</Error>
</Errors>
<RequestID>afc00dc9-0c19-46db-a987-f7de2a12a361</RequestID>
</Response>
`)
type Error struct {
Code string
Message string
}
type Response struct {
RequestId string
Errors []Error
}
got := new(Response)
err := xml.Unmarshal(body, got)
if err != nil {
t.Fatal(err)
}
exp := &Response{
RequestId: "afc00dc9-0c19-46db-a987-f7de2a12a361",
Errors: []Error{
{Code: "AuthFailure", Message: "AWS was not able to validate the provided access credentials"},
},
}
assert.Equal(t, exp, got)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment