2019-03-11 16:56:48 +00:00
|
|
|
package webroot
|
2016-03-14 10:49:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-09-24 19:07:20 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-03-14 10:49:02 +00:00
|
|
|
)
|
|
|
|
|
2016-03-16 10:32:09 +00:00
|
|
|
func TestHTTPProvider(t *testing.T) {
|
2016-03-14 10:49:02 +00:00
|
|
|
webroot := "webroot"
|
|
|
|
domain := "domain"
|
|
|
|
token := "token"
|
|
|
|
keyAuth := "keyAuth"
|
|
|
|
challengeFilePath := webroot + "/.well-known/acme-challenge/" + token
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, os.MkdirAll(webroot+"/.well-known/acme-challenge", 0777))
|
2016-03-14 10:49:02 +00:00
|
|
|
defer os.RemoveAll(webroot)
|
|
|
|
|
2016-03-16 10:32:09 +00:00
|
|
|
provider, err := NewHTTPProvider(webroot)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-14 10:49:02 +00:00
|
|
|
|
|
|
|
err = provider.Present(domain, token, keyAuth)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-14 10:49:02 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
if _, err = os.Stat(challengeFilePath); os.IsNotExist(err) {
|
2016-03-14 10:49:02 +00:00
|
|
|
t.Error("Challenge file was not created in webroot")
|
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
var data []byte
|
|
|
|
data, err = ioutil.ReadFile(challengeFilePath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-03-14 10:49:02 +00:00
|
|
|
dataStr := string(data)
|
2018-09-24 19:07:20 +00:00
|
|
|
assert.Equal(t, keyAuth, dataStr)
|
2016-03-14 10:49:02 +00:00
|
|
|
|
|
|
|
err = provider.CleanUp(domain, token, keyAuth)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-14 10:49:02 +00:00
|
|
|
}
|