| 1 |
package autorest |
| 2 |
|
| 3 |
// Copyright 2017 Microsoft Corporation |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
|
| 17 |
import ( |
| 18 |
"net/http" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/Azure/go-autorest/autorest/mocks" |
| 22 |
) |
| 23 |
|
| 24 |
func TestResponseHasStatusCode(t *testing.T) { |
| 25 |
codes := []int{http.StatusOK, http.StatusAccepted} |
| 26 |
resp := &http.Response{StatusCode: http.StatusAccepted} |
| 27 |
if !ResponseHasStatusCode(resp, codes...) { |
| 28 |
t.Fatalf("autorest: ResponseHasStatusCode failed to find %v in %v", resp.StatusCode, codes) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func TestResponseHasStatusCodeNotPresent(t *testing.T) { |
| 33 |
codes := []int{http.StatusOK, http.StatusAccepted} |
| 34 |
resp := &http.Response{StatusCode: http.StatusInternalServerError} |
| 35 |
if ResponseHasStatusCode(resp, codes...) { |
| 36 |
t.Fatalf("autorest: ResponseHasStatusCode unexpectedly found %v in %v", resp.StatusCode, codes) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestNewPollingRequestDoesNotReturnARequestWhenLocationHeaderIsMissing(t *testing.T) { |
| 41 |
resp := mocks.NewResponseWithStatus("500 InternalServerError", http.StatusInternalServerError) |
| 42 |
|
| 43 |
req, _ := NewPollingRequest(resp, nil) |
| 44 |
if req != nil { |
| 45 |
t.Fatal("autorest: NewPollingRequest returned an http.Request when the Location header was missing") |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestNewPollingRequestReturnsAnErrorWhenPrepareFails(t *testing.T) { |
| 50 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 51 |
mocks.SetAcceptedHeaders(resp) |
| 52 |
resp.Header.Set(http.CanonicalHeaderKey(HeaderLocation), mocks.TestBadURL) |
| 53 |
|
| 54 |
_, err := NewPollingRequest(resp, nil) |
| 55 |
if err == nil { |
| 56 |
t.Fatal("autorest: NewPollingRequest failed to return an error when Prepare fails") |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func TestNewPollingRequestDoesNotReturnARequestWhenPrepareFails(t *testing.T) { |
| 61 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 62 |
mocks.SetAcceptedHeaders(resp) |
| 63 |
resp.Header.Set(http.CanonicalHeaderKey(HeaderLocation), mocks.TestBadURL) |
| 64 |
|
| 65 |
req, _ := NewPollingRequest(resp, nil) |
| 66 |
if req != nil { |
| 67 |
t.Fatal("autorest: NewPollingRequest returned an http.Request when Prepare failed") |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestNewPollingRequestReturnsAGetRequest(t *testing.T) { |
| 72 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 73 |
mocks.SetAcceptedHeaders(resp) |
| 74 |
|
| 75 |
req, _ := NewPollingRequest(resp, nil) |
| 76 |
if req.Method != "GET" { |
| 77 |
t.Fatalf("autorest: NewPollingRequest did not create an HTTP GET request -- actual method %v", req.Method) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestNewPollingRequestProvidesTheURL(t *testing.T) { |
| 82 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 83 |
mocks.SetAcceptedHeaders(resp) |
| 84 |
|
| 85 |
req, _ := NewPollingRequest(resp, nil) |
| 86 |
if req.URL.String() != mocks.TestURL { |
| 87 |
t.Fatalf("autorest: NewPollingRequest did not create an HTTP with the expected URL -- received %v, expected %v", req.URL, mocks.TestURL) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestGetLocation(t *testing.T) { |
| 92 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 93 |
mocks.SetAcceptedHeaders(resp) |
| 94 |
|
| 95 |
l := GetLocation(resp) |
| 96 |
if len(l) == 0 { |
| 97 |
t.Fatalf("autorest: GetLocation failed to return Location header -- expected %v, received %v", mocks.TestURL, l) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestGetLocationReturnsEmptyStringForMissingLocation(t *testing.T) { |
| 102 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 103 |
|
| 104 |
l := GetLocation(resp) |
| 105 |
if len(l) != 0 { |
| 106 |
t.Fatalf("autorest: GetLocation return a value without a Location header -- received %v", l) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestGetRetryAfter(t *testing.T) { |
| 111 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 112 |
mocks.SetAcceptedHeaders(resp) |
| 113 |
|
| 114 |
d := GetRetryAfter(resp, DefaultPollingDelay) |
| 115 |
if d != mocks.TestDelay { |
| 116 |
t.Fatalf("autorest: GetRetryAfter failed to returned the expected delay -- expected %v, received %v", mocks.TestDelay, d) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func TestGetRetryAfterReturnsDefaultDelayIfRetryHeaderIsMissing(t *testing.T) { |
| 121 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 122 |
|
| 123 |
d := GetRetryAfter(resp, DefaultPollingDelay) |
| 124 |
if d != DefaultPollingDelay { |
| 125 |
t.Fatalf("autorest: GetRetryAfter failed to returned the default delay for a missing Retry-After header -- expected %v, received %v", |
| 126 |
DefaultPollingDelay, d) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func TestGetRetryAfterReturnsDefaultDelayIfRetryHeaderIsMalformed(t *testing.T) { |
| 131 |
resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) |
| 132 |
mocks.SetAcceptedHeaders(resp) |
| 133 |
resp.Header.Set(http.CanonicalHeaderKey(HeaderRetryAfter), "a very bad non-integer value") |
| 134 |
|
| 135 |
d := GetRetryAfter(resp, DefaultPollingDelay) |
| 136 |
if d != DefaultPollingDelay { |
| 137 |
t.Fatalf("autorest: GetRetryAfter failed to returned the default delay for a malformed Retry-After header -- expected %v, received %v", |
| 138 |
DefaultPollingDelay, d) |
| 139 |
} |
| 140 |
} |