|
| 1 | +// Copyright 2017 Drone.IO Inc. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package stash |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +func TestError_WithErrorsSlice(t *testing.T) { |
| 16 | + e := &Error{ |
| 17 | + Status: 400, |
| 18 | + Errors: []struct { |
| 19 | + Message string `json:"message"` |
| 20 | + ExceptionName string `json:"exceptionName"` |
| 21 | + CurrentVersion int `json:"currentVersion"` |
| 22 | + ExpectedVersion int `json:"expectedVersion"` |
| 23 | + }{ |
| 24 | + {Message: "field is required"}, |
| 25 | + }, |
| 26 | + } |
| 27 | + |
| 28 | + if got := e.Error(); got != "field is required" { |
| 29 | + t.Errorf("Error() = %q, want %q", got, "field is required") |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestError_WithMessage(t *testing.T) { |
| 34 | + e := &Error{ |
| 35 | + Status: 404, |
| 36 | + Message: "Repository not found", |
| 37 | + } |
| 38 | + |
| 39 | + want := "bitbucket server: status: 404 message: Repository not found" |
| 40 | + if got := e.Error(); got != want { |
| 41 | + t.Errorf("Error() = %q, want %q", got, want) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestError_StatusCodeOnly(t *testing.T) { |
| 46 | + e := &Error{ |
| 47 | + Status: 429, |
| 48 | + } |
| 49 | + |
| 50 | + want := "bitbucket server: http status 429" |
| 51 | + if got := e.Error(); got != want { |
| 52 | + t.Errorf("Error() = %q, want %q", got, want) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestError_UnknownError(t *testing.T) { |
| 57 | + e := &Error{} |
| 58 | + |
| 59 | + want := "bitbucket server: unknown error" |
| 60 | + if got := e.Error(); got != want { |
| 61 | + t.Errorf("Error() = %q, want %q", got, want) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestError_StatusStampedOnEmptyBody(t *testing.T) { |
| 66 | + // Verify that do() stamps the HTTP status on Error.Status |
| 67 | + // when the response body is empty (e.g. 429 rate-limit). |
| 68 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 69 | + w.WriteHeader(429) |
| 70 | + // empty body |
| 71 | + })) |
| 72 | + defer server.Close() |
| 73 | + |
| 74 | + client, _ := New(server.URL) |
| 75 | + wrapper := &wrapper{client} |
| 76 | + |
| 77 | + _, err := wrapper.do(context.Background(), "GET", "rest/api/1.0/repos", nil, nil) |
| 78 | + if err == nil { |
| 79 | + t.Fatal("Expected error for 429 response, got nil") |
| 80 | + } |
| 81 | + |
| 82 | + stashErr, ok := err.(*Error) |
| 83 | + if !ok { |
| 84 | + t.Fatalf("Expected *Error, got %T: %v", err, err) |
| 85 | + } |
| 86 | + |
| 87 | + if stashErr.Status != 429 { |
| 88 | + t.Errorf("Expected Status=429, got %d", stashErr.Status) |
| 89 | + } |
| 90 | + |
| 91 | + want := "bitbucket server: http status 429" |
| 92 | + if got := stashErr.Error(); got != want { |
| 93 | + t.Errorf("Expected error %q, got %q", want, got) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestError_StatusFromJSONPreserved(t *testing.T) { |
| 98 | + // When the JSON body includes a status-code field, it should NOT be overwritten. |
| 99 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 100 | + w.Header().Set("Content-Type", "application/json") |
| 101 | + w.WriteHeader(400) |
| 102 | + json.NewEncoder(w).Encode(map[string]interface{}{ |
| 103 | + "status-code": 400, |
| 104 | + "message": "Bad request from API", |
| 105 | + }) |
| 106 | + })) |
| 107 | + defer server.Close() |
| 108 | + |
| 109 | + client, _ := New(server.URL) |
| 110 | + wrapper := &wrapper{client} |
| 111 | + |
| 112 | + _, err := wrapper.do(context.Background(), "GET", "rest/api/1.0/repos", nil, nil) |
| 113 | + if err == nil { |
| 114 | + t.Fatal("Expected error for 400 response, got nil") |
| 115 | + } |
| 116 | + |
| 117 | + stashErr, ok := err.(*Error) |
| 118 | + if !ok { |
| 119 | + t.Fatalf("Expected *Error, got %T: %v", err, err) |
| 120 | + } |
| 121 | + |
| 122 | + // Status from JSON body should be preserved (not overwritten by HTTP status) |
| 123 | + if stashErr.Status != 400 { |
| 124 | + t.Errorf("Expected Status=400 from JSON, got %d", stashErr.Status) |
| 125 | + } |
| 126 | + |
| 127 | + want := "bitbucket server: status: 400 message: Bad request from API" |
| 128 | + if got := stashErr.Error(); got != want { |
| 129 | + t.Errorf("Expected error %q, got %q", want, got) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestError_StatusFallbackWhenJSONHasNoStatus(t *testing.T) { |
| 134 | + // When JSON body has message but no status-code, HTTP status should be used as fallback. |
| 135 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 136 | + w.Header().Set("Content-Type", "application/json") |
| 137 | + w.WriteHeader(503) |
| 138 | + json.NewEncoder(w).Encode(map[string]interface{}{ |
| 139 | + "message": "Service unavailable", |
| 140 | + }) |
| 141 | + })) |
| 142 | + defer server.Close() |
| 143 | + |
| 144 | + client, _ := New(server.URL) |
| 145 | + wrapper := &wrapper{client} |
| 146 | + |
| 147 | + _, err := wrapper.do(context.Background(), "GET", "rest/api/1.0/repos", nil, nil) |
| 148 | + if err == nil { |
| 149 | + t.Fatal("Expected error for 503 response, got nil") |
| 150 | + } |
| 151 | + |
| 152 | + stashErr, ok := err.(*Error) |
| 153 | + if !ok { |
| 154 | + t.Fatalf("Expected *Error, got %T: %v", err, err) |
| 155 | + } |
| 156 | + |
| 157 | + // HTTP status should be stamped as fallback since JSON has no status-code |
| 158 | + if stashErr.Status != 503 { |
| 159 | + t.Errorf("Expected Status=503 (fallback from HTTP), got %d", stashErr.Status) |
| 160 | + } |
| 161 | + |
| 162 | + want := "bitbucket server: status: 503 message: Service unavailable" |
| 163 | + if got := stashErr.Error(); got != want { |
| 164 | + t.Errorf("Expected error %q, got %q", want, got) |
| 165 | + } |
| 166 | +} |
0 commit comments