72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package mockid
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"testing"
|
|
//keypairs "github.com/big-squid/go-keypairs"
|
|
//"github.com/big-squid/go-keypairs/keyfetch/uncached"
|
|
)
|
|
|
|
var srv *httptest.Server
|
|
|
|
func TestMain(m *testing.M) {
|
|
os.Setenv("SALT", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
|
|
jwksPrefix := "public-jwks"
|
|
err := os.MkdirAll(jwksPrefix, 0755)
|
|
if nil != err {
|
|
fmt.Fprintf(os.Stderr, "couldn't write %q: %s", jwksPrefix, err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
privkey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
mux := Route(jwksPrefix, privkey)
|
|
|
|
srv = httptest.NewServer(mux)
|
|
|
|
//fs := http.FileServer(http.Dir("public"))
|
|
//http.Handle("/", fs)
|
|
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func TestTest(t *testing.T) {
|
|
client := srv.Client()
|
|
urlstr, _ := url.Parse(srv.URL + "/private.jwk.json")
|
|
//fmt.Println("URL:", srv.URL, urlstr)
|
|
res, err := client.Do(&http.Request{
|
|
Method: "POST",
|
|
URL: urlstr,
|
|
})
|
|
if nil != err {
|
|
//t.Fatal(err)
|
|
t.Error(err)
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(res.Body)
|
|
if nil != err {
|
|
//t.Fatal(err)
|
|
t.Error(err)
|
|
}
|
|
|
|
jwk := map[string]string{}
|
|
err = json.Unmarshal(data, &jwk)
|
|
if nil != err {
|
|
//t.Fatal(err)
|
|
t.Error(err)
|
|
}
|
|
|
|
if "" == jwk["d"] {
|
|
t.Fatal("Missing key 'd' from supposed private key")
|
|
}
|
|
//fmt.Printf("%#v\n", jwk)
|
|
}
|