File: unmarshall_error_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (153 lines) | stat: -rw-r--r-- 4,161 bytes parent folder | download | duplicates (4)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package simpledb_test

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

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/awstesting/unit"
	"github.com/aws/aws-sdk-go/service/simpledb"
)

var statusCodeErrorTests = []struct {
	scode   int
	status  string
	code    string
	message string
}{
	{301, "Moved Permanently", "MovedPermanently", "Moved Permanently"},
	{403, "Forbidden", "Forbidden", "Forbidden"},
	{400, "Bad Request", "BadRequest", "Bad Request"},
	{404, "Not Found", "NotFound", "Not Found"},
	{500, "Internal Error", "InternalError", "Internal Error"},
}

func TestStatusCodeError(t *testing.T) {
	for _, test := range statusCodeErrorTests {
		s := simpledb.New(unit.Session)
		s.Handlers.Send.Clear()
		s.Handlers.Send.PushBack(func(r *request.Request) {
			body := ioutil.NopCloser(bytes.NewReader([]byte{}))
			r.HTTPResponse = &http.Response{
				ContentLength: 0,
				StatusCode:    test.scode,
				Status:        test.status,
				Body:          body,
			}
		})
		_, err := s.CreateDomain(&simpledb.CreateDomainInput{
			DomainName: aws.String("test-domain"),
		})

		if err == nil {
			t.Fatalf("expect error, got nil")
		}
		if e, a := test.code, err.(awserr.Error).Code(); e != a {
			t.Errorf("expect %v, got %v", e, a)
		}
		if e, a := test.message, err.(awserr.Error).Message(); e != a {
			t.Errorf("expect %v, got %v", e, a)
		}
	}
}

var responseErrorTests = []struct {
	scode     int
	status    string
	code      string
	message   string
	requestID string
	errors    []struct {
		code    string
		message string
	}
}{
	{
		scode:     400,
		status:    "Bad Request",
		code:      "MissingError",
		message:   "missing error code in SimpleDB XML error response",
		requestID: "101",
		errors:    []struct{ code, message string }{},
	},
	{
		scode:     403,
		status:    "Forbidden",
		code:      "AuthFailure",
		message:   "AWS was not able to validate the provided access keys.",
		requestID: "1111",
		errors: []struct{ code, message string }{
			{"AuthFailure", "AWS was not able to validate the provided access keys."},
		},
	},
	{
		scode:     500,
		status:    "Internal Error",
		code:      "MissingParameter",
		message:   "Message #1",
		requestID: "8756",
		errors: []struct{ code, message string }{
			{"MissingParameter", "Message #1"},
			{"InternalError", "Message #2"},
		},
	},
}

func TestResponseError(t *testing.T) {
	for _, test := range responseErrorTests {
		s := simpledb.New(unit.Session)
		s.Handlers.Send.Clear()
		s.Handlers.Send.PushBack(func(r *request.Request) {
			xml := createXMLResponse(test.requestID, test.errors)
			body := ioutil.NopCloser(bytes.NewReader([]byte(xml)))
			r.HTTPResponse = &http.Response{
				ContentLength: int64(len(xml)),
				StatusCode:    test.scode,
				Status:        test.status,
				Body:          body,
			}
		})
		_, err := s.CreateDomain(&simpledb.CreateDomainInput{
			DomainName: aws.String("test-domain"),
		})

		if err == nil {
			t.Fatalf("expect error, got none")
		}
		if e, a := test.code, err.(awserr.Error).Code(); e != a {
			t.Errorf("expect %v, got %v", e, a)
		}
		if e, a := test.message, err.(awserr.Error).Message(); e != a {
			t.Errorf("expect %v, got %v", e, a)
		}
		if len(test.errors) > 0 {
			if e, a := test.requestID, err.(awserr.RequestFailure).RequestID(); e != a {
				t.Errorf("expect %v, got %v", e, a)
			}
			if e, a := test.scode, err.(awserr.RequestFailure).StatusCode(); e != a {
				t.Errorf("expect %v, got %v", e, a)
			}
		}
	}
}

// createXMLResponse constructs an XML string that has one or more error messages in it.
func createXMLResponse(requestID string, errors []struct{ code, message string }) []byte {
	var buf bytes.Buffer
	buf.WriteString(`<?xml version="1.0"?><Response><Errors>`)
	for _, e := range errors {
		buf.WriteString(`<Error><Code>`)
		buf.WriteString(e.code)
		buf.WriteString(`</Code><Message>`)
		buf.WriteString(e.message)
		buf.WriteString(`</Message></Error>`)
	}
	buf.WriteString(`</Errors><RequestID>`)
	buf.WriteString(requestID)
	buf.WriteString(`</RequestID></Response>`)
	return buf.Bytes()
}