← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jtv/gomaasapi/escape-filenames into lp:gomaasapi

 

Jeroen T. Vermeulen has proposed merging lp:~jtv/gomaasapi/escape-filenames into lp:gomaasapi.

Commit message:
Ensure proper escaping of filenames in the test service's URL composition code.

Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~jtv/gomaasapi/escape-filenames/+merge/151700

This is needed to deal with weird characters in the juju-core provider.


Jeroen
-- 
https://code.launchpad.net/~jtv/gomaasapi/escape-filenames/+merge/151700
Your team MAAS Maintainers is requested to review the proposed merge of lp:~jtv/gomaasapi/escape-filenames into lp:gomaasapi.
=== modified file 'testservice.go'
--- testservice.go	2013-03-04 08:18:13 +0000
+++ testservice.go	2013-03-05 09:01:27 +0000
@@ -66,7 +66,9 @@
 }
 
 func getFileURI(version, filename string) string {
-	return fmt.Sprintf("/api/%s/files/%s/", version, filename)
+	uri := url.URL{}
+	uri.Path = fmt.Sprintf("/api/%s/files/%s/", version, filename)
+	return uri.String()
 }
 
 // Clear clears all the fake data stored and recorded by the test server
@@ -129,7 +131,8 @@
 	// Allocate an arbitrary URL here.  It would be nice if the caller
 	// could do this, but that would change the API and require many
 	// changes.
-	attrs["anon_resource_uri"] = "/maas/1.0/files/?op=get_by_key&key=" + filename + "_key"
+	escapedName := url.QueryEscape(filename)
+	attrs["anon_resource_uri"] = "/maas/1.0/files/?op=get_by_key&key=" + escapedName + "_key"
 
 	obj := newJSONMAASObject(attrs, server.client)
 	server.files[filename] = obj

=== modified file 'testservice_test.go'
--- testservice_test.go	2013-03-04 08:18:13 +0000
+++ testservice_test.go	2013-03-05 09:01:27 +0000
@@ -14,6 +14,7 @@
 	"mime/multipart"
 	"net/http"
 	"net/url"
+	"strings"
 )
 
 type TestServerSuite struct {
@@ -192,6 +193,17 @@
 	c.Check(field, Equals, base64.StdEncoding.EncodeToString(fileContent))
 }
 
+func (suite *TestServerSuite) TestNewFileEscapesName(c *C) {
+	obj := suite.server.NewFile("aa?bb", []byte("bytes"))
+	resourceURI := obj.URI()
+	c.Check(strings.Contains(resourceURI.String(), "aa?bb"), Equals, false)
+	c.Check(strings.Contains(resourceURI.Path, "aa?bb"), Equals, true)
+	anonURI, err := obj.GetField("anon_resource_uri")
+	c.Assert(err, IsNil)
+	c.Check(strings.Contains(anonURI, "aa?bb"), Equals, false)
+	c.Check(strings.Contains(anonURI, "aa%3Fbb"), Equals, true)
+}
+
 func (suite *TestServerSuite) TestHandlesFile(c *C) {
 	const filename = "my-file"
 	const fileContent = "test file content"