2019-03-11 16:56:48 +00:00
|
|
|
package tester
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-11-01 23:52:38 +00:00
|
|
|
"testing"
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/acme"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetupFakeAPI Minimal stub ACME server for validation.
|
2021-11-01 23:52:38 +00:00
|
|
|
func SetupFakeAPI(t *testing.T) (*http.ServeMux, string) {
|
|
|
|
t.Helper()
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
mux := http.NewServeMux()
|
2021-11-01 23:52:38 +00:00
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/dir", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := WriteJSONResponse(w, acme.Directory{
|
2021-11-01 23:52:38 +00:00
|
|
|
NewNonceURL: server.URL + "/nonce",
|
|
|
|
NewAccountURL: server.URL + "/account",
|
|
|
|
NewOrderURL: server.URL + "/newOrder",
|
|
|
|
RevokeCertURL: server.URL + "/revokeCert",
|
|
|
|
KeyChangeURL: server.URL + "/keyChange",
|
2023-05-27 15:05:22 +00:00
|
|
|
RenewalInfo: server.URL + "/renewalInfo",
|
2018-12-06 21:50:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/nonce", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodHead {
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-17 12:51:55 +00:00
|
|
|
w.Header().Set("Replay-Nonce", "12345")
|
|
|
|
w.Header().Set("Retry-After", "0")
|
2018-12-06 21:50:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
return mux, server.URL
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteJSONResponse marshals the body as JSON and writes it to the response.
|
|
|
|
func WriteJSONResponse(w http.ResponseWriter, body interface{}) error {
|
|
|
|
bs, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if _, err := w.Write(bs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|