violet/servers/http_test.go

49 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-06-04 22:28:48 +01:00
package servers
import (
"bytes"
2023-07-22 01:11:47 +01:00
"github.com/1f349/violet/servers/conf"
"github.com/1f349/violet/utils"
"github.com/1f349/violet/utils/fake"
2023-06-04 22:28:48 +01:00
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewHttpServer_AcmeChallenge(t *testing.T) {
httpConf := &conf.Conf{
Domains: &fake.Domains{},
2023-06-04 22:28:48 +01:00
Acme: utils.NewAcmeChallenge(),
Signer: fake.SnakeOilProv,
2023-06-04 22:28:48 +01:00
}
2024-02-16 01:41:42 +00:00
srv := NewHttpServer(httpConf, nil)
2023-06-04 22:28:48 +01:00
httpConf.Acme.Put("example.com", "456", "456def")
req, err := http.NewRequest(http.MethodGet, "https://example.com/.well-known/acme-challenge/456", nil)
assert.NoError(t, err)
rec := httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res := rec.Result()
assert.Equal(t, http.StatusOK, res.StatusCode)
all, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, 0, bytes.Compare([]byte("456def"), all))
// Invalid key
req, err = http.NewRequest(http.MethodGet, "https://example.com/.well-known/acme-challenge/789", nil)
assert.NoError(t, err)
rec = httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res = rec.Result()
assert.Equal(t, http.StatusNotFound, res.StatusCode)
all, err = io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, 0, bytes.Compare([]byte(""), all))
}