@@ -75,6 +75,7 @@ func TestDiscover(t *testing.T) {
75
75
)
76
76
ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
77
77
w .Header ().Set ("Content-Type" , "application/json" )
78
+ w .Header ().Set ("Replay-Nonce" , "testnonce" )
78
79
fmt .Fprintf (w , `{
79
80
"new-reg": %q,
80
81
"new-authz": %q,
@@ -100,6 +101,9 @@ func TestDiscover(t *testing.T) {
100
101
if dir .RevokeURL != revoke {
101
102
t .Errorf ("dir.RevokeURL = %q; want %q" , dir .RevokeURL , revoke )
102
103
}
104
+ if _ , exist := c .nonces ["testnonce" ]; ! exist {
105
+ t .Errorf ("c.nonces = %q; want 'testnonce' in the map" , c .nonces )
106
+ }
103
107
}
104
108
105
109
func TestRegister (t * testing.T ) {
@@ -147,7 +151,11 @@ func TestRegister(t *testing.T) {
147
151
return false
148
152
}
149
153
150
- c := Client {Key : testKeyEC , dir : & Directory {RegURL : ts .URL }}
154
+ c := Client {
155
+ Key : testKeyEC ,
156
+ DirectoryURL : ts .URL ,
157
+ dir : & Directory {RegURL : ts .URL },
158
+ }
151
159
a := & Account {Contact : contacts }
152
160
var err error
153
161
if a , err = c .Register (context .Background (), a , prompt ); err != nil {
@@ -351,7 +359,11 @@ func TestAuthorize(t *testing.T) {
351
359
auth * Authorization
352
360
err error
353
361
)
354
- cl := Client {Key : testKeyEC , dir : & Directory {AuthzURL : ts .URL }}
362
+ cl := Client {
363
+ Key : testKeyEC ,
364
+ DirectoryURL : ts .URL ,
365
+ dir : & Directory {AuthzURL : ts .URL },
366
+ }
355
367
switch test .typ {
356
368
case "dns" :
357
369
auth , err = cl .Authorize (context .Background (), test .value )
@@ -422,7 +434,11 @@ func TestAuthorizeValid(t *testing.T) {
422
434
w .Write ([]byte (`{"status":"valid"}` ))
423
435
}))
424
436
defer ts .Close ()
425
- client := Client {Key : testKey , dir : & Directory {AuthzURL : ts .URL }}
437
+ client := Client {
438
+ Key : testKey ,
439
+ DirectoryURL : ts .URL ,
440
+ dir : & Directory {AuthzURL : ts .URL },
441
+ }
426
442
_ , err := client .Authorize (context .Background (), "example.com" )
427
443
if err != nil {
428
444
t .Errorf ("err = %v" , err )
@@ -1037,6 +1053,53 @@ func TestNonce_fetchError(t *testing.T) {
1037
1053
}
1038
1054
}
1039
1055
1056
+ func TestNonce_popWhenEmpty (t * testing.T ) {
1057
+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1058
+ if r .Method != "HEAD" {
1059
+ t .Errorf ("r.Method = %q; want HEAD" , r .Method )
1060
+ }
1061
+ switch r .URL .Path {
1062
+ case "/dir-with-nonce" :
1063
+ w .Header ().Set ("Replay-Nonce" , "dirnonce" )
1064
+ case "/new-nonce" :
1065
+ w .Header ().Set ("Replay-Nonce" , "newnonce" )
1066
+ case "/dir-no-nonce" , "/empty" :
1067
+ // No nonce in the header.
1068
+ default :
1069
+ t .Errorf ("Unknown URL: %s" , r .URL )
1070
+ }
1071
+ }))
1072
+ defer ts .Close ()
1073
+ ctx := context .Background ()
1074
+
1075
+ tt := []struct {
1076
+ dirURL , popURL , nonce string
1077
+ wantOK bool
1078
+ }{
1079
+ {ts .URL + "/dir-with-nonce" , ts .URL + "/new-nonce" , "dirnonce" , true },
1080
+ {ts .URL + "/dir-no-nonce" , ts .URL + "/new-nonce" , "newnonce" , true },
1081
+ {ts .URL + "/dir-no-nonce" , ts .URL + "/empty" , "" , false },
1082
+ }
1083
+ for _ , test := range tt {
1084
+ t .Run (fmt .Sprintf ("nonce:%s wantOK:%v" , test .nonce , test .wantOK ), func (t * testing.T ) {
1085
+ c := Client {DirectoryURL : test .dirURL }
1086
+ v , err := c .popNonce (ctx , test .popURL )
1087
+ if ! test .wantOK {
1088
+ if err == nil {
1089
+ t .Fatalf ("c.popNonce(%q) returned nil error" , test .popURL )
1090
+ }
1091
+ return
1092
+ }
1093
+ if err != nil {
1094
+ t .Fatalf ("c.popNonce(%q): %v" , test .popURL , err )
1095
+ }
1096
+ if v != test .nonce {
1097
+ t .Errorf ("c.popNonce(%q) = %q; want %q" , test .popURL , v , test .nonce )
1098
+ }
1099
+ })
1100
+ }
1101
+ }
1102
+
1040
1103
func TestNonce_postJWS (t * testing.T ) {
1041
1104
var count int
1042
1105
seen := make (map [string ]bool )
@@ -1070,7 +1133,11 @@ func TestNonce_postJWS(t *testing.T) {
1070
1133
}))
1071
1134
defer ts .Close ()
1072
1135
1073
- client := Client {Key : testKey , dir : & Directory {AuthzURL : ts .URL }}
1136
+ client := Client {
1137
+ Key : testKey ,
1138
+ DirectoryURL : ts .URL , // nonces are fetched from here first
1139
+ dir : & Directory {AuthzURL : ts .URL },
1140
+ }
1074
1141
if _ , err := client .Authorize (context .Background (), "example.com" ); err != nil {
1075
1142
t .Errorf ("client.Authorize 1: %v" , err )
1076
1143
}
0 commit comments