Merge lp:~jtv/gwacl/compose-url into lp:gwacl

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: 74
Merged at revision: 77
Proposed branch: lp:~jtv/gwacl/compose-url
Merge into: lp:gwacl
Diff against target: 163 lines (+47/-20)
3 files modified
managementapi_test.go (+1/-1)
x509session.go (+18/-15)
x509session_test.go (+28/-4)
To merge this branch: bzr merge lp:~jtv/gwacl/compose-url
Reviewer Review Type Date Requested Status
Raphaël Badin (community) Approve
Review via email: mp+156104@code.launchpad.net

Commit message

Make composeURL a method of x509Session, and clean up.

Description of the change

Making composeURL a method helps clarify its name (it composes a URL relative to the session) but also simplifies calls, because they no longer need to extract the session's subscription ID.

Those cleanups I mentioned were:
 * Unit tests. There wasn't time for them earlier.
 * Replaced the somewhat fuzzy "URL" and "URI" nomenclature with "path" for relative paths.
 * Did away with some upper-casing in variable/parameter names that we've been told was undesirable.
 * Checked against absolute paths, which composeURL wouldn't handle properly.

Finally, a few tests used absolute paths. So those now ran into that check I added, and I had to fix them up.

Jeroen

To post a comment you must log in.
Revision history for this message
Raphaël Badin (rvb) :
review: Approve
lp:~jtv/gwacl/compose-url updated
74. By Jeroen T. Vermeulen

Merge trunk, resolve expected semantic conflict.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'managementapi_test.go'
2--- managementapi_test.go 2013-03-29 12:22:47 +0000
3+++ managementapi_test.go 2013-03-29 12:28:20 +0000
4@@ -227,7 +227,7 @@
5 secondaryKey = "secondarykey"
6 )
7 api := suite.makeAPI(c)
8- url := composeURL("services/storageservices/"+accountName, api.session.subscriptionId)
9+ url := api.session.composeURL("services/storageservices/"+accountName)
10 body := fmt.Sprintf(
11 `<StorageService>
12 <Url>%s</Url>
13
14=== modified file 'x509session.go'
15--- x509session.go 2013-03-27 17:08:58 +0000
16+++ x509session.go 2013-03-29 12:28:20 +0000
17@@ -4,7 +4,9 @@
18 package gwacl
19
20 import (
21+ "fmt"
22 "net/http"
23+ "strings"
24 )
25
26 type x509Session struct {
27@@ -20,9 +22,14 @@
28
29 var AZURE_URL = "https://management.core.windows.net/"
30
31-// TODO: Test
32-func composeURL(URI, subscriptionId string) string {
33- return AZURE_URL + subscriptionId + "/" + URI
34+// composeURL puts together a URL for an item on the Azure API based on
35+// the starting point used by the session, and a given relative path from
36+// there.
37+func (session *x509Session) composeURL(path string) string {
38+ if strings.HasPrefix(path, "/") {
39+ panic(fmt.Errorf("got absolute API path '%s' instead of relative one", path))
40+ }
41+ return AZURE_URL + session.subscriptionId + "/" + path
42 }
43
44 // _X509Dispatcher is the function used to dispatch requests. We call the
45@@ -43,9 +50,8 @@
46 // It returns the response body and/or an error. If the error is a
47 // ServerError, the returned body will be the one received from the server.
48 // For any other kind of error, the returned body will be nil.
49-func (session *x509Session) get(url string) (*x509Response, error) {
50- fullUrl := composeURL(url, session.subscriptionId)
51- request := newX509RequestGET(fullUrl)
52+func (session *x509Session) get(path string) (*x509Response, error) {
53+ request := newX509RequestGET(session.composeURL(path))
54 response, err := _X509Dispatcher(session, request)
55 if err != nil {
56 return nil, err
57@@ -58,9 +64,8 @@
58 // It returns the response body and/or an error. If the error is a
59 // ServerError, the returned body will be the one received from the server.
60 // For any other kind of error, the returned body will be nil.
61-func (session *x509Session) post(url string, body []byte, contentType string) (*x509Response, error) {
62- fullUrl := composeURL(url, session.subscriptionId)
63- request := newX509RequestPOST(fullUrl, body, contentType)
64+func (session *x509Session) post(path string, body []byte, contentType string) (*x509Response, error) {
65+ request := newX509RequestPOST(session.composeURL(path), body, contentType)
66 response, err := _X509Dispatcher(session, request)
67 if err != nil {
68 return nil, err
69@@ -70,9 +75,8 @@
70 }
71
72 // delete performs a DELETE request to the Azure management API.
73-func (session *x509Session) delete(url string) error {
74- fullUrl := composeURL(url, session.subscriptionId)
75- request := newX509RequestDELETE(fullUrl)
76+func (session *x509Session) delete(path string) error {
77+ request := newX509RequestDELETE(session.composeURL(path))
78 response, err := _X509Dispatcher(session, request)
79 if err != nil {
80 return err
81@@ -81,9 +85,8 @@
82 }
83
84 // put performs a PUT request to the Azure management API.
85-func (session *x509Session) put(url string, body []byte) (*x509Response, error) {
86- fullUrl := composeURL(url, session.subscriptionId)
87- request := newX509RequestPUT(fullUrl, body)
88+func (session *x509Session) put(path string, body []byte) (*x509Response, error) {
89+ request := newX509RequestPUT(session.composeURL(path), body)
90 response, err := _X509Dispatcher(session, request)
91 if err != nil {
92 return nil, err
93
94=== modified file 'x509session_test.go'
95--- x509session_test.go 2013-03-27 17:08:58 +0000
96+++ x509session_test.go 2013-03-29 12:28:20 +0000
97@@ -109,6 +109,30 @@
98 c.Assert(err, IsNil)
99 }
100
101+func (suite *x509SessionSuite) TestComposeURLComposesURLWithRelativePath(c *C) {
102+ const subscriptionID = "subscriptionid"
103+ const path = "foo/bar"
104+ session, err := newX509Session(subscriptionID, "cert.pem")
105+ c.Assert(err, IsNil)
106+
107+ url := session.composeURL(path)
108+
109+ c.Check(url, Matches, AZURE_URL+subscriptionID+"/"+path)
110+}
111+
112+func (suite *x509SessionSuite) TestComposeURLRejectsAbsolutePath(c *C) {
113+ defer func() {
114+ err := recover()
115+ c.Assert(err, NotNil)
116+ c.Check(err, ErrorMatches, ".*absolute.*path.*")
117+ }()
118+ session, err := newX509Session("subscriptionid", "cert.pem")
119+ c.Assert(err, IsNil)
120+
121+ // This panics because we're passing an absolute path.
122+ session.composeURL("/foo")
123+}
124+
125 func (suite *x509SessionSuite) TestGetServerErrorProducesServerError(c *C) {
126 msg := "huhwhat"
127 status := http.StatusNotFound
128@@ -183,7 +207,7 @@
129 msg := "could not dispatch request"
130 rigFailingDispatcher(fmt.Errorf(msg))
131
132- body, err := session.get("/flop")
133+ body, err := session.get("flop")
134 c.Assert(err, NotNil)
135
136 c.Check(body, IsNil)
137@@ -198,7 +222,7 @@
138 }
139 rigFixedResponseDispatcher(&fixedResponse)
140
141- response, err := session.get("/fail")
142+ response, err := session.get("fail")
143 c.Assert(err, NotNil)
144
145 serverError := err.(*ServerError)
146@@ -239,7 +263,7 @@
147 msg := "could not dispatch request"
148 rigFailingDispatcher(fmt.Errorf(msg))
149
150- body, err := session.post("/flop", []byte("body"), "contentType")
151+ body, err := session.post("flop", []byte("body"), "contentType")
152 c.Assert(err, NotNil)
153
154 c.Check(body, IsNil)
155@@ -254,7 +278,7 @@
156 }
157 rigFixedResponseDispatcher(&fixedResponse)
158
159- reponse, err := session.post("/fail", []byte("request body"), "contentType")
160+ reponse, err := session.post("fail", []byte("request body"), "contentType")
161 c.Assert(err, NotNil)
162
163 serverError := err.(*ServerError)

Subscribers

People subscribed via source and target branches