lego/acme/http_challenge_test.go

58 lines
1.7 KiB
Go
Raw Normal View History

2015-11-12 22:51:07 +00:00
package acme
import (
"crypto/rsa"
"io/ioutil"
"net/http"
"strings"
"testing"
)
func TestHTTPChallenge(t *testing.T) {
2015-11-12 22:51:07 +00:00
privKey, _ := generatePrivateKey(rsakey, 512)
j := &jws{privKey: privKey.(*rsa.PrivateKey)}
clientChallenge := challenge{Type: "http-01", Token: "http1"}
mockValidate := func(_ *jws, _ string, chlng challenge) error {
uri := "http://localhost:23457/.well-known/acme-challenge/" + chlng.Token
resp, err := http.Get(uri)
if err != nil {
return err
2015-11-12 22:51:07 +00:00
}
defer resp.Body.Close()
2015-11-12 22:51:07 +00:00
if want := "text/plain"; resp.Header.Get("Content-Type") != want {
t.Errorf("Get(%q) Content-Type: got %q, want %q", uri, resp.Header.Get("Content-Type"), want)
2015-11-12 22:51:07 +00:00
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
2015-11-12 22:51:07 +00:00
}
bodyStr := string(body)
2015-11-12 22:51:07 +00:00
if bodyStr != chlng.KeyAuthorization {
t.Errorf("Get(%q) Body: got %q, want %q", uri, bodyStr, chlng.KeyAuthorization)
2015-11-12 22:51:07 +00:00
}
return nil
2015-11-12 22:51:07 +00:00
}
solver := &httpChallenge{jws: j, validate: mockValidate, optPort: "23457"}
2015-11-12 22:51:07 +00:00
if err := solver.Solve(clientChallenge, "localhost:23457"); err != nil {
t.Errorf("Solve error: got %v, want nil", err)
2015-11-12 22:51:07 +00:00
}
}
func TestHTTPChallengeInvalidPort(t *testing.T) {
privKey, _ := generatePrivateKey(rsakey, 128)
j := &jws{privKey: privKey.(*rsa.PrivateKey)}
clientChallenge := challenge{Type: "http-01", Token: "http2"}
solver := &httpChallenge{jws: j, validate: stubValidate, optPort: "123456"}
if err := solver.Solve(clientChallenge, "localhost:123456"); err == nil {
t.Error("Solve error: got %v, want error", err)
} else if want := "invalid port 123456"; !strings.HasSuffix(err.Error(), want) {
t.Errorf("Solve error: got %q, want suffix %q", err.Error(), want)
2015-11-12 22:51:07 +00:00
}
}