@@ -11,6 +11,7 @@ import (
11
11
"io"
12
12
"net"
13
13
"net/http"
14
+ "net/url"
14
15
"strconv"
15
16
"strings"
16
17
"sync"
@@ -39,7 +40,7 @@ type testDataServer struct {
39
40
TestData
40
41
41
42
// Synchronizes access to test data
42
- mutex sync.Mutex
43
+ lock sync.Mutex
43
44
}
44
45
45
46
// Type used to marshal/unmarshal a set of test keys for transmission over http.
@@ -63,8 +64,8 @@ func (s *testDataServer) allocateKeys(w http.ResponseWriter, r *http.Request) {
63
64
}
64
65
65
66
// Ensure a key will be allocated at most once
66
- s .mutex .Lock ()
67
- defer s .mutex .Unlock ()
67
+ s .lock .Lock ()
68
+ defer s .lock .Unlock ()
68
69
69
70
// Only fulfill requests for available keys
70
71
if keyCount > len (s .FundedKeys ) {
@@ -111,6 +112,7 @@ func ServeTestData(testData TestData) (string, error) {
111
112
}
112
113
113
114
go func () {
115
+ // Serve always returns a non-nil error and closes l.
114
116
if err := httpServer .Serve (listener ); err != http .ErrServerClosed {
115
117
panic (fmt .Sprintf ("unexpected error closing test data server: %v" , err ))
116
118
}
@@ -121,12 +123,19 @@ func ServeTestData(testData TestData) (string, error) {
121
123
122
124
// Retrieve the specified number of funded test keys from the provided URI. A given
123
125
// key is allocated at most once during the life of the test data server.
124
- func AllocateFundedKeys (uri string , count int ) ([]* secp256k1.PrivateKey , error ) {
126
+ func AllocateFundedKeys (baseURI string , count int ) ([]* secp256k1.PrivateKey , error ) {
125
127
if count <= 0 {
126
128
return nil , errInvalidKeyCount
127
129
}
128
- query := fmt .Sprintf ("%s%s?%s=%d" , uri , allocateKeysPath , keyCountParameterName , count )
129
- req , err := http .NewRequestWithContext (context .Background (), http .MethodGet , query , nil )
130
+ uri , err := url .Parse (baseURI )
131
+ if err != nil {
132
+ return nil , fmt .Errorf ("failed to parse uri: %w" , err )
133
+ }
134
+ uri .RawQuery = url.Values {
135
+ keyCountParameterName : {strconv .Itoa (count )},
136
+ }.Encode ()
137
+ uri .Path = allocateKeysPath
138
+ req , err := http .NewRequestWithContext (context .Background (), http .MethodGet , uri .String (), nil )
130
139
if err != nil {
131
140
return nil , fmt .Errorf ("failed to construct request: %w" , err )
132
141
}
0 commit comments