(t *testing.T)
| 101 | } |
| 102 | |
| 103 | func (reg *testRegistry) start(t *testing.T) *httptest.Server { |
| 104 | t.Helper() |
| 105 | |
| 106 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 107 | reg.reqHits.Add(1) |
| 108 | if r.URL.Path == "/v2/" { |
| 109 | reg.pingHits.Add(1) |
| 110 | w.WriteHeader(http.StatusOK) |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | rejectStatus := reg.anonReject |
| 115 | |
| 116 | _, _, hasAuth := r.BasicAuth() |
| 117 | if rejectStatus != 0 { |
| 118 | if !hasAuth { |
| 119 | reg.anonHits.Add(1) |
| 120 | if rejectStatus == http.StatusUnauthorized { |
| 121 | w.Header().Set("WWW-Authenticate", `Basic realm="test"`) |
| 122 | } |
| 123 | w.WriteHeader(rejectStatus) |
| 124 | return |
| 125 | } |
| 126 | u, p, _ := r.BasicAuth() |
| 127 | if u != reg.user || p != reg.pass { |
| 128 | w.WriteHeader(http.StatusForbidden) |
| 129 | return |
| 130 | } |
| 131 | reg.authHits.Add(1) |
| 132 | } else { |
| 133 | reg.anonHits.Add(1) |
| 134 | } |
| 135 | |
| 136 | switch { |
| 137 | case strings.Contains(r.URL.Path, "/manifests/"): |
| 138 | w.Header().Set("Docker-Content-Digest", reg.digest) |
| 139 | w.Header().Set("Content-Type", string(reg.mediaType)) |
| 140 | if r.Method == http.MethodGet { |
| 141 | _, _ = w.Write(reg.manifest) |
| 142 | } |
| 143 | case strings.Contains(r.URL.Path, "/blobs/"): |
| 144 | layers, _ := reg.img.Layers() |
| 145 | if len(layers) == 0 { |
| 146 | w.WriteHeader(http.StatusNotFound) |
| 147 | return |
| 148 | } |
| 149 | rc, err := layers[0].Compressed() |
| 150 | if err != nil { |
| 151 | w.WriteHeader(http.StatusInternalServerError) |
| 152 | return |
| 153 | } |
| 154 | defer rc.Close() |
| 155 | _, _ = io.Copy(w, rc) |
| 156 | default: |
| 157 | w.WriteHeader(http.StatusNotFound) |
| 158 | } |
| 159 | }) |
| 160 |
no test coverage detected