client_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "errors"
  16. "net/http"
  17. "net/http/httptest"
  18. "strings"
  19. "testing"
  20. )
  21. func TestAPIErrorError(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. err *APIError
  25. want string
  26. }{
  27. {
  28. name: "with status",
  29. err: &APIError{StatusCode: 401, Message: "Invalid Auth token"},
  30. want: "Doppler API Client Error (HTTP 401): Invalid Auth token",
  31. },
  32. {
  33. name: "no status stays backward compatible",
  34. err: &APIError{Message: "secret 'FOO' not found"},
  35. want: "Doppler API Client Error: secret 'FOO' not found",
  36. },
  37. {
  38. name: "appends underlying error",
  39. err: &APIError{StatusCode: 500, Message: "unable to load response", Err: errors.New("boom")},
  40. want: "Doppler API Client Error (HTTP 500): unable to load response\nboom",
  41. },
  42. {
  43. name: "appends data",
  44. err: &APIError{Message: "unable to unmarshal secret payload", Data: "{bad json}"},
  45. want: "Doppler API Client Error: unable to unmarshal secret payload\nData: {bad json}",
  46. },
  47. }
  48. for _, tt := range tests {
  49. t.Run(tt.name, func(t *testing.T) {
  50. if got := tt.err.Error(); got != tt.want {
  51. t.Errorf("Error() = %q, want %q", got, tt.want)
  52. }
  53. })
  54. }
  55. }
  56. // TestPerformRequestSurfacesStatus exercises the real request path: a failing
  57. // Doppler API response must yield an error naming the HTTP status, without
  58. // leaking the request endpoint.
  59. func TestPerformRequestSurfacesStatus(t *testing.T) {
  60. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  61. w.Header().Set("content-type", "application/json")
  62. w.WriteHeader(http.StatusUnauthorized)
  63. _, _ = w.Write([]byte(`{"messages":["Invalid Auth token"],"success":false}`))
  64. }))
  65. defer server.Close()
  66. c, err := NewDopplerClient("bad-token")
  67. if err != nil {
  68. t.Fatalf("NewDopplerClient: %v", err)
  69. }
  70. if err := c.SetBaseURL(server.URL); err != nil {
  71. t.Fatalf("SetBaseURL: %v", err)
  72. }
  73. err = c.Authenticate()
  74. if err == nil {
  75. t.Fatal("expected an authentication error, got nil")
  76. }
  77. got := err.Error()
  78. for _, want := range []string{"(HTTP 401)", "Invalid Auth token"} {
  79. if !strings.Contains(got, want) {
  80. t.Errorf("error %q does not contain %q", got, want)
  81. }
  82. }
  83. if strings.Contains(got, "/v3/projects") {
  84. t.Errorf("error %q should not surface the request endpoint", got)
  85. }
  86. }