tests: use url.QueryEscape() when dealing with url query params. (#3974)

This is to keep the portability and also avoid errors that
might occur using the functions written for URL resource name
Since query param values have different escaping requirements.
master
Harshavardhana 8 years ago committed by GitHub
parent a4ecd8bca2
commit 565ac4c861
  1. 41
      cmd/test-utils_test.go

@ -845,39 +845,6 @@ func newTestStreamingSignedRequest(method, urlStr string, contentLength, chunkSi
return req, err
}
// Replaces any occurring '/' in string, into its encoded
// representation.
func percentEncodeSlash(s string) string {
return strings.Replace(s, "/", "%2F", -1)
}
// queryEncode - encodes query values in their URL encoded form. In
// addition to the percent encoding performed by getURLEncodedName()
// used here, it also percent encodes '/' (forward slash)
func queryEncode(v url.Values) string {
if v == nil {
return ""
}
var buf bytes.Buffer
keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
vs := v[k]
prefix := percentEncodeSlash(getURLEncodedName(k)) + "="
for _, v := range vs {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(prefix)
buf.WriteString(percentEncodeSlash(getURLEncodedName(v)))
}
}
return buf.String()
}
// preSignV4 presign the request, in accordance with
// http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html.
func preSignV4(req *http.Request, accessKeyID, secretAccessKey string, expires int64) error {
@ -912,7 +879,7 @@ func preSignV4(req *http.Request, accessKeyID, secretAccessKey string, expires i
req.URL.RawQuery = query.Encode()
// Add signature header to RawQuery.
req.URL.RawQuery += "&X-Amz-Signature=" + signature
req.URL.RawQuery += "&X-Amz-Signature=" + url.QueryEscape(signature)
// Construct the final presigned URL.
return nil
@ -963,10 +930,10 @@ func preSignV2(req *http.Request, accessKeyID, secretAccessKey string, expires i
query.Set("Expires", strconv.FormatInt(epochExpires, 10))
// Encode query and save.
req.URL.RawQuery = queryEncode(query)
req.URL.RawQuery = query.Encode()
// Save signature finally.
req.URL.RawQuery += "&Signature=" + getURLEncodedName(signature)
req.URL.RawQuery += "&Signature=" + url.QueryEscape(signature)
// Success.
return nil
@ -1398,7 +1365,7 @@ func makeTestTargetURL(endPoint, bucketName, objectName string, queryValues url.
urlStr = urlStr + getURLEncodedName(objectName)
}
if len(queryValues) > 0 {
urlStr = urlStr + "?" + queryEncode(queryValues)
urlStr = urlStr + "?" + queryValues.Encode()
}
return urlStr
}

Loading…
Cancel
Save