|
8 | 8 | "context" |
9 | 9 | "encoding/json" |
10 | 10 | "fmt" |
| 11 | + "io" |
11 | 12 | "net/http" |
12 | 13 | "net/http/httptest" |
13 | 14 | "os" |
@@ -124,6 +125,82 @@ func Test_unmarshalResponse(t *testing.T) { |
124 | 125 | }) |
125 | 126 | } |
126 | 127 |
|
| 128 | +func Test_decodeErrorPayload(t *testing.T) { |
| 129 | + t.Parallel() |
| 130 | + |
| 131 | + t.Run("with jsonapi errors payload", func(t *testing.T) { |
| 132 | + resp := &http.Response{ |
| 133 | + Status: "400 Bad Request", |
| 134 | + StatusCode: http.StatusBadRequest, |
| 135 | + Body: io.NopCloser(bytes.NewBufferString( |
| 136 | + `{"errors":[{"title":"org name invalid","detail":"Org name is invalid"}]}`, |
| 137 | + )), |
| 138 | + } |
| 139 | + |
| 140 | + errs, err := decodeErrorPayload(resp) |
| 141 | + require.NoError(t, err) |
| 142 | + require.Len(t, errs, 1) |
| 143 | + assert.Equal(t, "org name invalid\n\nOrg name is invalid", errs[0]) |
| 144 | + }) |
| 145 | + |
| 146 | + t.Run("with regular json errors payload", func(t *testing.T) { |
| 147 | + resp := &http.Response{ |
| 148 | + Status: "400 Bad Request", |
| 149 | + StatusCode: http.StatusBadRequest, |
| 150 | + Body: io.NopCloser(bytes.NewBufferString( |
| 151 | + `{"errors":["Unsupported GPG Key algorithm. Supported key algorithms are [RSA, DSA]"]}`, |
| 152 | + )), |
| 153 | + } |
| 154 | + |
| 155 | + errs, err := decodeErrorPayload(resp) |
| 156 | + require.NoError(t, err) |
| 157 | + require.Len(t, errs, 1) |
| 158 | + assert.Equal(t, "Unsupported GPG Key algorithm. Supported key algorithms are [RSA, DSA]", errs[0]) |
| 159 | + }) |
| 160 | + |
| 161 | + t.Run("with non-json error body", func(t *testing.T) { |
| 162 | + resp := &http.Response{ |
| 163 | + Status: "400 Bad Request", |
| 164 | + StatusCode: http.StatusBadRequest, |
| 165 | + Body: io.NopCloser(bytes.NewBufferString("this is not json")), |
| 166 | + } |
| 167 | + |
| 168 | + errs, err := decodeErrorPayload(resp) |
| 169 | + require.EqualError(t, err, "400 Bad Request") |
| 170 | + assert.Empty(t, errs) |
| 171 | + }) |
| 172 | +} |
| 173 | + |
| 174 | +func Test_checkResponseCode(t *testing.T) { |
| 175 | + t.Parallel() |
| 176 | + |
| 177 | + t.Run("returns regular json error message", func(t *testing.T) { |
| 178 | + resp := &http.Response{ |
| 179 | + Status: "400 Bad Request", |
| 180 | + StatusCode: http.StatusBadRequest, |
| 181 | + Body: io.NopCloser(bytes.NewBufferString( |
| 182 | + `{"errors":["Unsupported GPG Key algorithm. Supported key algorithms are [RSA, DSA]"]}`, |
| 183 | + )), |
| 184 | + } |
| 185 | + |
| 186 | + err := checkResponseCode(resp) |
| 187 | + require.EqualError(t, err, "Unsupported GPG Key algorithm. Supported key algorithms are [RSA, DSA]") |
| 188 | + }) |
| 189 | + |
| 190 | + t.Run("still maps invalid include message", func(t *testing.T) { |
| 191 | + resp := &http.Response{ |
| 192 | + Status: "400 Bad Request", |
| 193 | + StatusCode: http.StatusBadRequest, |
| 194 | + Body: io.NopCloser(bytes.NewBufferString( |
| 195 | + `{"errors":["include parameter is invalid"]}`, |
| 196 | + )), |
| 197 | + } |
| 198 | + |
| 199 | + err := checkResponseCode(resp) |
| 200 | + assert.ErrorIs(t, err, ErrInvalidIncludeValue) |
| 201 | + }) |
| 202 | +} |
| 203 | + |
127 | 204 | func Test_BaseURL(t *testing.T) { |
128 | 205 | t.Parallel() |
129 | 206 | client, err := NewClient(&Config{ |
|
0 commit comments