File: unmarshal_error_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.44.133-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 245,296 kB
  • sloc: makefile: 120
file content (114 lines) | stat: -rw-r--r-- 3,029 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//go:build go1.8
// +build go1.8

package route53

import (
	"io/ioutil"
	"net/http"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/request"
)

func TestUnmarshalInvalidChangeBatch(t *testing.T) {
	const errorMessage = `
Tried to create resource record set duplicate.example.com. type A,
but it already exists
`

	type batchError struct {
		Code, Message string
	}

	cases := map[string]struct {
		Request                  *request.Request
		Code, Message, RequestID string
		StatusCode               int
		BatchErrors              []batchError
	}{
		"standard error": {
			Request: &request.Request{
				HTTPResponse: &http.Response{
					StatusCode: 400,
					Header:     http.Header{},
					Body: ioutil.NopCloser(strings.NewReader(
						`<?xml version="1.0" encoding="UTF-8"?>
<ErrorResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
  <Error>
	<Code>InvalidDomainName</Code>
	<Message>The domain name is invalid</Message>
  </Error>
  <RequestId>12345</RequestId>
</ErrorResponse>`)),
				},
			},
			Code: "InvalidDomainName", Message: "The domain name is invalid",
			StatusCode: 400, RequestID: "12345",
		},
		"batched error": {
			Request: &request.Request{
				HTTPResponse: &http.Response{
					StatusCode: 400,
					Header:     http.Header{},
					Body: ioutil.NopCloser(strings.NewReader(
						`<?xml version="1.0" encoding="UTF-8"?>
<InvalidChangeBatch xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
  <Messages>
	<Message>` + errorMessage + `</Message>
  </Messages>
  <RequestId>12345</RequestId>
</InvalidChangeBatch>`)),
				},
			},
			Code: "InvalidChangeBatch", Message: "ChangeBatch errors occurred",
			StatusCode: 400, RequestID: "12345",
			BatchErrors: []batchError{
				{Code: "InvalidChangeBatch", Message: errorMessage},
			},
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			unmarshalChangeResourceRecordSetsError(c.Request)
			err := c.Request.Error
			if err == nil {
				t.Error("expected error, but received none")
			}

			reqErr := err.(awserr.RequestFailure)
			if e, a := c.StatusCode, reqErr.StatusCode(); e != a {
				t.Errorf("expected %d status, got %d", e, a)
			}
			if e, a := c.Code, reqErr.Code(); e != a {
				t.Errorf("expected %v code, got %v", e, a)
			}
			if e, a := c.Message, reqErr.Message(); e != a {
				t.Errorf("expected %q message, got %q", e, a)
			}
			if e, a := c.RequestID, reqErr.RequestID(); e != a {
				t.Errorf("expected %v request ID, got %v", e, a)
			}

			batchErr := err.(awserr.BatchedErrors)
			batchedErrs := batchErr.OrigErrs()

			if e, a := len(c.BatchErrors), len(batchedErrs); e != a {
				t.Fatalf("expect %v batch errors, got %v", e, a)
			}

			for i, ee := range c.BatchErrors {
				bErr := batchedErrs[i].(awserr.Error)
				if e, a := ee.Code, bErr.Code(); e != a {
					t.Errorf("expect %v code, got %v", e, a)
				}
				if e, a := ee.Message, bErr.Message(); e != a {
					t.Errorf("expect %v message, got %v", e, a)
				}
			}
		})
	}
}