Merge pull request #486 from endophage/fix_offline

tokenAuth should also 'succeed' if we get a 401
This commit is contained in:
Diogo Mónica 2016-01-21 13:36:49 -08:00
commit e579f101e7
2 changed files with 37 additions and 3 deletions

View File

@ -432,8 +432,9 @@ func tokenAuth(trustServerURL string, baseTransport *http.Transport, gun string,
} }
// non-nil err means we must close body // non-nil err means we must close body
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { if (resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices) &&
// If we didn't get a 2XX range status code, we're not talking to a notary server. resp.StatusCode != http.StatusUnauthorized {
// If we didn't get a 2XX range or 401 status code, we're not talking to a notary server.
// The http client should be configured to handle redirects so at this point, 3XX is // The http client should be configured to handle redirects so at this point, 3XX is
// not a valid status code. // not a valid status code.
logrus.Errorf("could not reach %s: %d", trustServerURL, resp.StatusCode) logrus.Errorf("could not reach %s: %d", trustServerURL, resp.StatusCode)

View File

@ -17,11 +17,44 @@ func TestTokenAuth(t *testing.T) {
require.Nil(t, tokenAuth("https://localhost:9999", baseTransport, gun, readOnly)) require.Nil(t, tokenAuth("https://localhost:9999", baseTransport, gun, readOnly))
} }
func StatusOKTestHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("{}"))
}
func TestTokenAuth200Status(t *testing.T) {
var (
readOnly bool
baseTransport = &http.Transport{}
gun = "test"
)
s := httptest.NewServer(http.HandlerFunc(NotAuthorizedTestHandler))
defer s.Close()
require.NotNil(t, tokenAuth(s.URL, baseTransport, gun, readOnly))
}
func NotAuthorizedTestHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(401)
}
func TestTokenAuth401Status(t *testing.T) {
var (
readOnly bool
baseTransport = &http.Transport{}
gun = "test"
)
s := httptest.NewServer(http.HandlerFunc(NotAuthorizedTestHandler))
defer s.Close()
require.NotNil(t, tokenAuth(s.URL, baseTransport, gun, readOnly))
}
func NotFoundTestHandler(w http.ResponseWriter, r *http.Request) { func NotFoundTestHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404) w.WriteHeader(404)
} }
func TestTokenAuthNon200Status(t *testing.T) { func TestTokenAuthNon200Non401Status(t *testing.T) {
var ( var (
readOnly bool readOnly bool
baseTransport = &http.Transport{} baseTransport = &http.Transport{}