|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package auth |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +func TestParseAuthHeader(t *testing.T) { |
| 27 | + headerTemplate := `Bearer realm="%s",service="%s",scope="%s"` |
| 28 | + |
| 29 | + for _, tc := range []struct { |
| 30 | + name string |
| 31 | + realm string |
| 32 | + service string |
| 33 | + scope string |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "SingleScope", |
| 37 | + realm: "https://auth.docker.io/token", |
| 38 | + service: "registry.docker.io", |
| 39 | + scope: "repository:foo/bar:pull,push", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "MultipleScopes", |
| 43 | + realm: "https://auth.docker.io/token", |
| 44 | + service: "registry.docker.io", |
| 45 | + scope: "repository:foo/bar:pull,push repository:foo/baz:pull repository:foo/foo:push", |
| 46 | + }, |
| 47 | + } { |
| 48 | + t.Run(tc.name, func(t *testing.T) { |
| 49 | + expected := []Challenge{ |
| 50 | + { |
| 51 | + Scheme: BearerAuth, |
| 52 | + Parameters: map[string]string{ |
| 53 | + "realm": tc.realm, |
| 54 | + "service": tc.service, |
| 55 | + "scope": tc.scope, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + hdr := http.Header{ |
| 61 | + http.CanonicalHeaderKey("WWW-Authenticate"): []string{fmt.Sprintf( |
| 62 | + headerTemplate, tc.realm, tc.service, tc.scope, |
| 63 | + )}, |
| 64 | + } |
| 65 | + actual := ParseAuthHeader(hdr) |
| 66 | + if !reflect.DeepEqual(expected, actual) { |
| 67 | + t.Fatalf("expected %v, but got %v", expected, actual) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
0 commit comments