From 823a03a417d4d38b31083a7b236610d1e29e7eb8 Mon Sep 17 00:00:00 2001 From: mikepulaski Date: Thu, 19 Apr 2018 21:02:13 +0200 Subject: [PATCH] ResolveAccountByKey no longer returns an EOF error on success. (#518) * ResolveAccountByKey no longer returns an EOF error on success. * Added test for ResolveAccountByKey. --- acmev2/client.go | 2 +- acmev2/client_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/acmev2/client.go b/acmev2/client.go index 3698f0ec..b2840576 100644 --- a/acmev2/client.go +++ b/acmev2/client.go @@ -189,7 +189,7 @@ func (c *Client) ResolveAccountByKey() (*RegistrationResource, error) { logf("[INFO] acme: Trying to resolve account by key") acc := accountMessage{OnlyReturnExisting: true} - hdr, err := postJSON(c.jws, c.directory.NewAccountURL, acc, &acc) + hdr, err := postJSON(c.jws, c.directory.NewAccountURL, acc, nil) if err != nil { return nil, err } diff --git a/acmev2/client_test.go b/acmev2/client_test.go index 71b212c2..d30dd3ba 100644 --- a/acmev2/client_test.go +++ b/acmev2/client_test.go @@ -252,6 +252,54 @@ func TestGetChallenges(t *testing.T) { } } +func TestResolveAccountByKey(t *testing.T) { + keyBits := 512 + keyType := RSA2048 + key, err := rsa.GenerateKey(rand.Reader, keyBits) + if err != nil { + t.Fatal("Could not generate test key:", err) + } + user := mockUser{ + email: "test@test.com", + regres: new(RegistrationResource), + privatekey: key, + } + + var ts *httptest.Server + ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.RequestURI { + case "/directory": + writeJSONResponse(w, directory{ + NewNonceURL: ts.URL + "/nonce", + NewAccountURL: ts.URL + "/account", + NewOrderURL: ts.URL + "/newOrder", + RevokeCertURL: ts.URL + "/revokeCert", + KeyChangeURL: ts.URL + "/keyChange", + }) + case "/nonce": + w.Header().Add("Replay-Nonce", "12345") + w.Header().Add("Retry-After", "0") + case "/account": + w.Header().Set("Location", ts.URL+"/account_recovery") + case "/account_recovery": + writeJSONResponse(w, accountMessage{ + Status: "valid", + }) + } + })) + + client, err := NewClient(ts.URL+"/directory", user, keyType) + if err != nil { + t.Fatalf("Could not create client: %v", err) + } + + if res, err := client.ResolveAccountByKey(); err != nil { + t.Fatalf("Unexpected error resolving account by key: %v", err) + } else if res.Body.Status != "valid" { + t.Errorf("Unexpected account status: %v", res.Body.Status) + } +} + // writeJSONResponse marshals the body as JSON and writes it to the response. func writeJSONResponse(w http.ResponseWriter, body interface{}) { bs, err := json.Marshal(body)