1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-16 14:53:36 +02:00
lego/acme/http_challenge_test.go

58 lines
1.8 KiB
Go
Raw Normal View History

2015-11-13 00:51:07 +02:00
package acme
import (
2016-01-27 03:01:58 +02:00
"crypto/rand"
2015-11-13 00:51:07 +02:00
"crypto/rsa"
"io/ioutil"
"strings"
"testing"
)
func TestHTTPChallenge(t *testing.T) {
2016-01-27 03:01:58 +02:00
privKey, _ := rsa.GenerateKey(rand.Reader, 512)
j := &jws{privKey: privKey}
clientChallenge := challenge{Type: HTTP01, Token: "http1"}
2015-12-27 20:26:47 +02:00
mockValidate := func(_ *jws, _, _ string, chlng challenge) error {
uri := "http://localhost:23457/.well-known/acme-challenge/" + chlng.Token
resp, err := httpGet(uri)
if err != nil {
return err
2015-11-13 00:51:07 +02:00
}
defer resp.Body.Close()
2015-11-13 00:51:07 +02: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-13 00:51:07 +02:00
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
2015-11-13 00:51:07 +02:00
}
bodyStr := string(body)
2015-11-13 00:51:07 +02:00
if bodyStr != chlng.KeyAuthorization {
t.Errorf("Get(%q) Body: got %q, want %q", uri, bodyStr, chlng.KeyAuthorization)
2015-11-13 00:51:07 +02:00
}
return nil
2015-11-13 00:51:07 +02:00
}
2016-02-14 23:07:27 +02:00
solver := &httpChallenge{jws: j, validate: mockValidate, provider: &HTTPProviderServer{port: "23457"}}
2015-11-13 00:51:07 +02:00
if err := solver.Solve(clientChallenge, "localhost:23457"); err != nil {
t.Errorf("Solve error: got %v, want nil", err)
2015-11-13 00:51:07 +02:00
}
}
func TestHTTPChallengeInvalidPort(t *testing.T) {
2016-01-27 03:01:58 +02:00
privKey, _ := rsa.GenerateKey(rand.Reader, 128)
j := &jws{privKey: privKey}
clientChallenge := challenge{Type: HTTP01, Token: "http2"}
2016-02-14 23:07:27 +02:00
solver := &httpChallenge{jws: j, validate: stubValidate, provider: &HTTPProviderServer{port: "123456"}}
if err := solver.Solve(clientChallenge, "localhost:123456"); err == nil {
t.Errorf("Solve error: got %v, want error", err)
} else if want, want18 := "invalid port 123456", "123456: invalid port"; !strings.HasSuffix(err.Error(), want) && !strings.HasSuffix(err.Error(), want18) {
t.Errorf("Solve error: got %q, want suffix %q", err.Error(), want)
2015-11-13 00:51:07 +02:00
}
}