Merge pull request #352 from HuKeping/check-map

Use canonical way to check if a map contains a key
This commit is contained in:
Ying Li 2015-12-10 23:13:27 -08:00
commit 8fd8916b15
3 changed files with 28 additions and 7 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")
}

View File

@ -107,7 +107,7 @@ type NotarySigner struct {
clientConn checkableConnectionState
}
// NewNotarySigner is a convinience method that returns NotarySigner
// NewNotarySigner is a convenience method that returns NotarySigner
func NewNotarySigner(hostname string, port string, tlsConfig *tls.Config) *NotarySigner {
var opts []grpc.DialOption
netAddr := net.JoinHostPort(hostname, port)