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:
HuKeping
2015-12-10 16:47:54 +08:00
parent c77bbee0ef
commit f147a7ac68
2 changed files with 27 additions and 6 deletions

View File

@@ -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

View File

@@ -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")
}