1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
package acme
import (
"encoding/json"
"net/http"
"reflect"
"testing"
)
func TestNewClient(t *testing.T) {
if _, err := NewClient("http://fake"); err == nil {
t.Fatal("expected error, got none")
}
}
func TestFetchLink(t *testing.T) {
linkTests := []struct {
Name string
LinkHeaders []string
WantedLink string
ExpectedURL string
}{
{
Name: "no links",
WantedLink: "fail",
ExpectedURL: "",
},
{Name: "joined links",
LinkHeaders: []string{`<https://url/path>; rel="next", <http://url/path?query>; rel="up"`},
WantedLink: "up",
ExpectedURL: "http://url/path?query",
},
{
Name: "separate links",
LinkHeaders: []string{`<https://url/path>; rel="next"`, `<http://url/path?query>; rel="up"`},
WantedLink: "up",
ExpectedURL: "http://url/path?query",
},
}
for _, currentTest := range linkTests {
linkURL := fetchLink(&http.Response{Header: http.Header{"Link": currentTest.LinkHeaders}}, currentTest.WantedLink)
if linkURL != currentTest.ExpectedURL {
t.Fatalf("%s: links not equal, expected: %s, got: %s", currentTest.Name, currentTest.ExpectedURL, linkURL)
}
}
}
func stringSliceEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func TestFetchLinks(t *testing.T) {
linkTests := []struct {
Name string
LinkHeaders []string
WantedLink string
ExpectedURLs []string
}{
{
Name: "no links",
WantedLink: "fail",
ExpectedURLs: nil,
},
{Name: "joined links",
LinkHeaders: []string{`<https://url/path>; rel="next", <http://url/path?query>; rel="up"`},
WantedLink: "up",
ExpectedURLs: []string{"http://url/path?query"},
},
{
Name: "separate links",
LinkHeaders: []string{`<https://url/path>; rel="next"`, `<http://url/path?query>; rel="up"`},
WantedLink: "up",
ExpectedURLs: []string{"http://url/path?query"},
},
{
Name: "multiple links",
LinkHeaders: []string{`<https://url/path>; rel="up"`, `<http://url/path?query>; rel="up"`},
WantedLink: "up",
ExpectedURLs: []string{"https://url/path", "http://url/path?query"},
},
}
for _, currentTest := range linkTests {
linkURLs := fetchLinks(&http.Response{Header: http.Header{"Link": currentTest.LinkHeaders}}, currentTest.WantedLink)
if !stringSliceEqual(linkURLs, currentTest.ExpectedURLs) {
t.Fatalf("%s: links not equal, expected: %s, got: %s", currentTest.Name, currentTest.ExpectedURLs, linkURLs)
}
}
}
func TestClient_Directory(t *testing.T) {
if !reflect.DeepEqual(testClient.dir, testClient.Directory()) {
t.Fatalf("directory mismatch, expected: %+v, got: %+v", testClient.dir, testClient.Directory())
}
}
func TestClient_Fetch(t *testing.T) {
_, account1order, _ := makeOrderFinalised(t, []string{ChallengeTypeDNS01}, Identifier{"dns", randString() + ".com"})
account2 := makeAccount(t)
err := testClient.Fetch(account2, account1order.URL, &Account{})
if err == nil {
t.Error("expected error accessing different account post-as-get, got none")
}
account := makeAccount(t)
b := json.RawMessage{}
if err := testClient.Fetch(account, testClient.Directory().URL, &b); err != nil {
t.Errorf("error post-as-get directory url: %v", err)
}
if err := testClient.Fetch(account, testClient.Directory().NewNonce, &b, http.StatusNoContent, http.StatusOK); err != nil {
t.Errorf("error post-as-get newnonce url: %v", err)
}
}
|