mirror of
https://github.com/docker/docs.git
synced 2026-04-12 06:19:22 +07:00
Use canonical way to check if a map contains a key
As the language spec: https://golang.org/ref/spec#Index_expressions Signed-off-by: Hu Keping <hukeping@huawei.com>
This commit is contained in:
@@ -34,14 +34,12 @@ func getCryptoService(w http.ResponseWriter, algorithm string, cryptoServices si
|
||||
return nil
|
||||
}
|
||||
|
||||
service := cryptoServices[algorithm]
|
||||
|
||||
if service == nil {
|
||||
http.Error(w, "algorithm "+algorithm+" not supported", http.StatusBadRequest)
|
||||
return nil
|
||||
if service, ok := cryptoServices[algorithm]; ok {
|
||||
return service
|
||||
}
|
||||
|
||||
return service
|
||||
http.Error(w, "algorithm "+algorithm+" not supported", http.StatusBadRequest)
|
||||
return nil
|
||||
}
|
||||
|
||||
// KeyInfo returns a Handler that given a specific Key ID param, returns the public key bits of that key
|
||||
|
||||
@@ -225,3 +225,26 @@ func TestSignHandlerReturns404WithNonexistentKey(t *testing.T) {
|
||||
|
||||
assert.Equal(t, 404, res.StatusCode)
|
||||
}
|
||||
|
||||
func TestCreateKeyHandlerWithInvalidAlgorithm(t *testing.T) {
|
||||
keyStore := trustmanager.NewKeyMemoryStore(passphraseRetriever)
|
||||
cryptoService := cryptoservice.NewCryptoService("", keyStore)
|
||||
setup(signer.CryptoServiceIndex{data.ED25519Key: cryptoService, data.RSAKey: cryptoService, data.ECDSAKey: cryptoService})
|
||||
|
||||
// The `rbtree-algorithm` is expected as not supported
|
||||
createKeyURL := fmt.Sprintf("%s/%s", createKeyBaseURL, "rbtree-algorithm")
|
||||
|
||||
request, err := http.NewRequest("POST", createKeyURL, nil)
|
||||
assert.Nil(t, err)
|
||||
|
||||
res, err := http.DefaultClient.Do(request)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// The body may contains some `\r\n`, so we use assert.Contains not assert.Equals
|
||||
assert.Contains(t, string(body), "algorithm rbtree-algorithm not supported")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user