File: unmarshal_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 (116 lines) | stat: -rw-r--r-- 2,719 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
115
116
//go:build go1.8
// +build go1.8

package query

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

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

func TestUnmarshalError(t *testing.T) {
	cases := map[string]struct {
		Request   *request.Request
		Code, Msg string
		ReqID     string
		Status    int
	}{
		"ErrorResponse": {
			Request: &request.Request{
				HTTPResponse: &http.Response{
					StatusCode: 400,
					Header:     http.Header{},
					Body: ioutil.NopCloser(strings.NewReader(
						`<ErrorResponse>
							<Error>
								<Code>codeAbc</Code><Message>msg123</Message>
							</Error>
							<RequestId>reqID123</RequestId>
						</ErrorResponse>`)),
				},
			},
			Code: "codeAbc", Msg: "msg123",
			Status: 400, ReqID: "reqID123",
		},
		"ErrorResponse with spaces": {
			Request: &request.Request{
				HTTPResponse: &http.Response{
					StatusCode: 400,
					Header:     http.Header{},
					Body: ioutil.NopCloser(strings.NewReader(
						`<ErrorResponse>
							<Error>
								<Code>
								codeAbc
								</Code><Message>
								msg123
								</Message>
							</Error>
							<RequestId>reqID123</RequestId>
						</ErrorResponse>`)),
				},
			},
			Code: "codeAbc", Msg: "msg123",
			Status: 400, ReqID: "reqID123",
		},
		"ServiceUnavailableException": {
			Request: &request.Request{
				HTTPResponse: &http.Response{
					StatusCode: 502,
					Header:     http.Header{},
					Body: ioutil.NopCloser(strings.NewReader(
						`<ServiceUnavailableException>
							<Something>else</Something>
						</ServiceUnavailableException>`)),
				},
			},
			Code:   "ServiceUnavailableException",
			Msg:    "service is unavailable",
			Status: 502,
		},
		"unknown tag": {
			Request: &request.Request{
				HTTPResponse: &http.Response{
					StatusCode: 400,
					Header:     http.Header{},
					Body: ioutil.NopCloser(strings.NewReader(
						`<Hello>
							<World>.</World>
						</Hello>`)),
				},
			},
			Code:   request.ErrCodeSerialization,
			Msg:    "failed to unmarshal error message",
			Status: 400,
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			r := c.Request
			UnmarshalError(r)
			if r.Error == nil {
				t.Fatalf("expect error, got none")
			}

			aerr := r.Error.(awserr.RequestFailure)
			if e, a := c.Code, aerr.Code(); e != a {
				t.Errorf("expect %v code, got %v", e, a)
			}
			if e, a := c.Msg, aerr.Message(); e != a {
				t.Errorf("expect %q message, got %q", e, a)
			}
			if e, a := c.ReqID, aerr.RequestID(); e != a {
				t.Errorf("expect %v request ID, got %v", e, a)
			}
			if e, a := c.Status, aerr.StatusCode(); e != a {
				t.Errorf("expect %v status code, got %v", e, a)
			}
		})
	}
}