4.1.1
by Julian Edwards
Beginnings of an x509 session object |
1 |
// Copyright 2013 Canonical Ltd. This software is licensed under the
|
2 |
// GNU Lesser General Public License version 3 (see the file COPYING).
|
|
3 |
||
4 |
package gwacl |
|
5 |
||
6 |
import ( |
|
4.1.3
by Julian Edwards
move MakeRandomString to its own file |
7 |
"crypto/rand"
|
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
8 |
"crypto/rsa"
|
9 |
"crypto/x509"
|
|
10 |
"crypto/x509/pkix"
|
|
11 |
"encoding/pem"
|
|
12 |
"fmt"
|
|
4.1.2
by Julian Edwards
gofmt |
13 |
. "launchpad.net/gocheck" |
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
14 |
"math/big"
|
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
15 |
"net/http"
|
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
16 |
"os"
|
17 |
"time"
|
|
18 |
)
|
|
19 |
||
60.4.1
by Jeroen Vermeulen
Extract x509DispatcherFixture from X509SessionSuite so we don't re-run the session tests wherever we use the fixture. |
20 |
// x509DispatcherFixture records the current x509 dispatcher before a test,
|
21 |
// and restores it after. This gives your test the freedom to replace the
|
|
22 |
// dispatcher with test doubles, using any of the rig*Dispatcher functions.
|
|
23 |
// Call the fixture's SetUpTest/TearDownTest methods before/after your test,
|
|
24 |
// or if you have no other setup/teardown methods, just embed the fixture in
|
|
25 |
// your test suite.
|
|
26 |
type x509DispatcherFixture struct { |
|
136.1.1
by Raphael Badin
Expose testing utility. |
27 |
oldDispatcher func(*x509Session, *X509Request) (*x509Response, error) |
60.4.1
by Jeroen Vermeulen
Extract x509DispatcherFixture from X509SessionSuite so we don't re-run the session tests wherever we use the fixture. |
28 |
}
|
29 |
||
30 |
func (suite *x509DispatcherFixture) SetUpTest(c *C) { |
|
31 |
// Record the original X509 dispatcher. Will be restored at the end of
|
|
32 |
// each test.
|
|
33 |
suite.oldDispatcher = _X509Dispatcher |
|
34 |
}
|
|
35 |
||
36 |
func (suite *x509DispatcherFixture) TearDownTest(c *C) { |
|
37 |
// Restore old dispatcher.
|
|
38 |
_X509Dispatcher = suite.oldDispatcher |
|
39 |
}
|
|
40 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
41 |
type x509SessionSuite struct { |
60.4.1
by Jeroen Vermeulen
Extract x509DispatcherFixture from X509SessionSuite so we don't re-run the session tests wherever we use the fixture. |
42 |
x509DispatcherFixture
|
18.2.2
by Raphael Badin
Fixes as per review. |
43 |
}
|
5.1.13
by Julian Edwards
Separate out test suites |
44 |
|
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
45 |
var _ = Suite(&x509SessionSuite{}) |
5.1.13
by Julian Edwards
Separate out test suites |
46 |
|
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
47 |
// Create a cert and pem file in a temporary dir in /tmp and return the
|
48 |
// names of the files. The caller is responsible for cleaning up the files.
|
|
49 |
func makeX509Certificate() (string, string) { |
|
50 |
// Code is shamelessly stolen from
|
|
51 |
// http://golang.org/src/pkg/crypto/tls/generate_cert.go
|
|
4.1.3
by Julian Edwards
move MakeRandomString to its own file |
52 |
priv, err := rsa.GenerateKey(rand.Reader, 1024) |
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
53 |
if err != nil { |
54 |
panic(fmt.Errorf("Failed to generate rsa key: %v", err)) |
|
55 |
}
|
|
56 |
||
4.1.5
by Julian Edwards
add comments into large function to split it up a bit |
57 |
// Create a template for x509.CreateCertificate.
|
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
58 |
now := time.Now() |
59 |
template := x509.Certificate{ |
|
60 |
SerialNumber: new(big.Int).SetInt64(0), |
|
61 |
Subject: pkix.Name{ |
|
62 |
CommonName: "localhost", |
|
63 |
Organization: []string{"Bogocorp"}, |
|
64 |
},
|
|
65 |
NotBefore: now.Add(-5 * time.Minute).UTC(), |
|
66 |
NotAfter: now.AddDate(1, 0, 0).UTC(), // valid for 1 year. |
|
67 |
SubjectKeyId: []byte{1, 2, 3, 4}, |
|
4.1.2
by Julian Edwards
gofmt |
68 |
KeyUsage: x509.KeyUsageKeyEncipherment | |
69 |
x509.KeyUsageDigitalSignature, |
|
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
70 |
}
|
71 |
||
4.1.5
by Julian Edwards
add comments into large function to split it up a bit |
72 |
// Create the certificate itself.
|
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
73 |
derBytes, err := x509.CreateCertificate( |
4.1.3
by Julian Edwards
move MakeRandomString to its own file |
74 |
rand.Reader, &template, &template, &priv.PublicKey, priv) |
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
75 |
if err != nil { |
76 |
panic(fmt.Errorf("Failed to generate x509 certificate: %v", err)) |
|
77 |
}
|
|
78 |
||
4.1.5
by Julian Edwards
add comments into large function to split it up a bit |
79 |
// Write the certificate file out.
|
4.1.3
by Julian Edwards
move MakeRandomString to its own file |
80 |
dirname := os.TempDir() + "/" + MakeRandomString(10) |
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
81 |
os.Mkdir(dirname, 0700) |
82 |
certFile := dirname + "/cert.pem" |
|
83 |
certOut, err := os.Create(certFile) |
|
84 |
if err != nil { |
|
85 |
panic(fmt.Errorf("Failed to create %s: %v", certFile, err)) |
|
86 |
}
|
|
87 |
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) |
|
88 |
certOut.Close() |
|
89 |
||
4.1.5
by Julian Edwards
add comments into large function to split it up a bit |
90 |
// Write the key file out.
|
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
91 |
keyFile := dirname + "/key.pem" |
92 |
keyOut, err := os.OpenFile( |
|
93 |
keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) |
|
94 |
if err != nil { |
|
95 |
panic(fmt.Errorf("Failed to create %s: %v", keyFile, err)) |
|
96 |
}
|
|
97 |
pem.Encode( |
|
4.1.7
by Julian Edwards
some reformatting for ease of reading |
98 |
keyOut, |
99 |
&pem.Block{ |
|
100 |
Type: "RSA PRIVATE KEY", |
|
4.1.9
by Julian Edwards
gofmt |
101 |
Bytes: x509.MarshalPKCS1PrivateKey(priv)}) |
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
102 |
keyOut.Close() |
4.1.5
by Julian Edwards
add comments into large function to split it up a bit |
103 |
|
4.1.1
by Julian Edwards
Beginnings of an x509 session object |
104 |
return certFile, keyFile |
105 |
}
|
|
106 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
107 |
func (suite *x509SessionSuite) TestNewX509SessionCreation(c *C) { |
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
108 |
_, err := newX509Session("subscriptionid", "azure.pem") |
14.3.4
by Jeroen Vermeulen
Fix session tests. By deleting them. Everything Has Changed. |
109 |
c.Assert(err, IsNil) |
4.1.6
by Julian Edwards
clean up func comment strings and add a missing test |
110 |
}
|
18.2.1
by Raphael Badin
Add test dispatcher. |
111 |
|
72.4.1
by Jeroen Vermeulen
Make composeURL a method of x509Session, unit-test it, replace 'URI' and 'URL' as names for relative paths with 'path,' add a check against absolute paths, and fix up some tests that used absolute paths. |
112 |
func (suite *x509SessionSuite) TestComposeURLComposesURLWithRelativePath(c *C) { |
113 |
const subscriptionID = "subscriptionid" |
|
114 |
const path = "foo/bar" |
|
115 |
session, err := newX509Session(subscriptionID, "cert.pem") |
|
116 |
c.Assert(err, IsNil) |
|
117 |
||
118 |
url := session.composeURL(path) |
|
119 |
||
120 |
c.Check(url, Matches, AZURE_URL+subscriptionID+"/"+path) |
|
121 |
}
|
|
122 |
||
123 |
func (suite *x509SessionSuite) TestComposeURLRejectsAbsolutePath(c *C) { |
|
124 |
defer func() { |
|
125 |
err := recover() |
|
126 |
c.Assert(err, NotNil) |
|
127 |
c.Check(err, ErrorMatches, ".*absolute.*path.*") |
|
128 |
}()
|
|
129 |
session, err := newX509Session("subscriptionid", "cert.pem") |
|
130 |
c.Assert(err, IsNil) |
|
131 |
||
132 |
// This panics because we're passing an absolute path.
|
|
133 |
session.composeURL("/foo") |
|
134 |
}
|
|
135 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
136 |
func (suite *x509SessionSuite) TestGetServerErrorProducesServerError(c *C) { |
21.1.2
by Jeroen Vermeulen
Unit-test server error handling. |
137 |
msg := "huhwhat" |
138 |
status := http.StatusNotFound |
|
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
139 |
session, err := newX509Session("subscriptionid", "azure.pem") |
21.1.2
by Jeroen Vermeulen
Unit-test server error handling. |
140 |
c.Assert(err, IsNil) |
141 |
||
65.1.1
by Jeroen Vermeulen
Unify Error and ServerError as HTTPError, with different implementations based on what information is available at runtime. |
142 |
err = session.getServerError(status, []byte{}, msg) |
21.1.2
by Jeroen Vermeulen
Unit-test server error handling. |
143 |
c.Assert(err, NotNil) |
144 |
||
145 |
c.Check(err, ErrorMatches, ".*"+msg+".*") |
|
146 |
serverError := err.(*ServerError) |
|
65.1.4
by Jeroen Vermeulen
Review suggestion: renamed Status() to StatusCode(). |
147 |
c.Check(serverError.StatusCode(), Equals, status) |
21.1.2
by Jeroen Vermeulen
Unit-test server error handling. |
148 |
}
|
149 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
150 |
func (suite *x509SessionSuite) TestGetServerErrorLikes20x(c *C) { |
21.1.8
by Jeroen Vermeulen
Review suggestions. |
151 |
goodCodes := []int{ |
152 |
http.StatusOK, |
|
153 |
http.StatusNoContent, |
|
154 |
}
|
|
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
155 |
session, err := newX509Session("subscriptionid", "azure.pem") |
21.1.2
by Jeroen Vermeulen
Unit-test server error handling. |
156 |
c.Assert(err, IsNil) |
157 |
||
21.1.8
by Jeroen Vermeulen
Review suggestions. |
158 |
for _, status := range goodCodes { |
65.1.1
by Jeroen Vermeulen
Unify Error and ServerError as HTTPError, with different implementations based on what information is available at runtime. |
159 |
c.Check(session.getServerError(status, []byte{}, ""), IsNil) |
21.1.8
by Jeroen Vermeulen
Review suggestions. |
160 |
}
|
21.1.2
by Jeroen Vermeulen
Unit-test server error handling. |
161 |
}
|
162 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
163 |
func (suite *x509SessionSuite) TestGetServerReturnsErrorsForFailures(c *C) { |
21.1.8
by Jeroen Vermeulen
Review suggestions. |
164 |
badCodes := []int{ |
165 |
http.StatusSwitchingProtocols, |
|
166 |
http.StatusBadRequest, |
|
167 |
http.StatusPaymentRequired, |
|
168 |
http.StatusForbidden, |
|
169 |
http.StatusGone, |
|
170 |
http.StatusInternalServerError, |
|
171 |
http.StatusNotImplemented, |
|
172 |
}
|
|
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
173 |
session, err := newX509Session("subscriptionid", "azure.pem") |
21.1.2
by Jeroen Vermeulen
Unit-test server error handling. |
174 |
c.Assert(err, IsNil) |
175 |
||
21.1.8
by Jeroen Vermeulen
Review suggestions. |
176 |
for _, status := range badCodes { |
65.1.1
by Jeroen Vermeulen
Unify Error and ServerError as HTTPError, with different implementations based on what information is available at runtime. |
177 |
c.Check(session.getServerError(status, []byte{}, ""), NotNil) |
21.1.8
by Jeroen Vermeulen
Review suggestions. |
178 |
}
|
21.1.2
by Jeroen Vermeulen
Unit-test server error handling. |
179 |
}
|
180 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
181 |
func (suite *x509SessionSuite) TestGetIssuesRequest(c *C) { |
21.1.8
by Jeroen Vermeulen
Review suggestions. |
182 |
subscriptionID := "subscriptionID" |
183 |
uri := "resource" |
|
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
184 |
session, err := newX509Session(subscriptionID, "cert.pem") |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
185 |
c.Assert(err, IsNil) |
186 |
// Record incoming requests, and have them return a given reply.
|
|
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
187 |
fixedResponse := x509Response{ |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
188 |
StatusCode: http.StatusOK, |
189 |
Body: []byte("Response body"), |
|
190 |
}
|
|
191 |
rigFixedResponseDispatcher(&fixedResponse) |
|
136.1.1
by Raphael Badin
Expose testing utility. |
192 |
recordedRequests := make([]*X509Request, 0) |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
193 |
rigRecordingDispatcher(&recordedRequests) |
194 |
||
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
195 |
version := "test-version" |
180.1.1
by Gavin Panella
Format. |
196 |
receivedResponse, err := session.get(uri, version) |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
197 |
c.Assert(err, IsNil) |
198 |
||
199 |
c.Assert(len(recordedRequests), Equals, 1) |
|
200 |
request := recordedRequests[0] |
|
201 |
c.Check(request.URL, Equals, AZURE_URL+subscriptionID+"/"+uri) |
|
202 |
c.Check(request.Method, Equals, "GET") |
|
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
203 |
c.Check(request.APIVersion, Equals, version) |
57.3.1
by Raphael Badin
Add poller. |
204 |
c.Check(*receivedResponse, DeepEquals, fixedResponse) |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
205 |
}
|
206 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
207 |
func (suite *x509SessionSuite) TestGetReportsClientSideError(c *C) { |
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
208 |
session, err := newX509Session("subscriptionid", "cert.pem") |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
209 |
msg := "could not dispatch request" |
210 |
rigFailingDispatcher(fmt.Errorf(msg)) |
|
211 |
||
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
212 |
body, err := session.get("flop", "version") |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
213 |
c.Assert(err, NotNil) |
214 |
||
21.1.4
by Jeroen Vermeulen
More tests for post(): 2 failures, 1 panic. |
215 |
c.Check(body, IsNil) |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
216 |
c.Check(err, ErrorMatches, ".*"+msg+".*") |
217 |
}
|
|
218 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
219 |
func (suite *x509SessionSuite) TestGetReportsServerSideError(c *C) { |
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
220 |
session, err := newX509Session("subscriptionid", "cert.pem") |
221 |
fixedResponse := x509Response{ |
|
21.1.5
by Jeroen Vermeulen
Reformat. |
222 |
StatusCode: http.StatusForbidden, |
223 |
Body: []byte("Body"), |
|
21.1.4
by Jeroen Vermeulen
More tests for post(): 2 failures, 1 panic. |
224 |
}
|
225 |
rigFixedResponseDispatcher(&fixedResponse) |
|
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
226 |
|
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
227 |
response, err := session.get("fail", "version") |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
228 |
c.Assert(err, NotNil) |
229 |
||
230 |
serverError := err.(*ServerError) |
|
65.1.4
by Jeroen Vermeulen
Review suggestion: renamed Status() to StatusCode(). |
231 |
c.Check(serverError.StatusCode(), Equals, fixedResponse.StatusCode) |
57.3.1
by Raphael Badin
Add poller. |
232 |
c.Check(*response, DeepEquals, fixedResponse) |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
233 |
}
|
234 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
235 |
func (suite *x509SessionSuite) TestPostIssuesRequest(c *C) { |
21.1.8
by Jeroen Vermeulen
Review suggestions. |
236 |
subscriptionID := "subscriptionID" |
237 |
uri := "resource" |
|
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
238 |
version := "test-version" |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
239 |
requestBody := []byte("Request body") |
28.1.1
by Raphael Badin
Add method to create hosted service. |
240 |
requestContentType := "bogusContentType" |
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
241 |
session, err := newX509Session(subscriptionID, "cert.pem") |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
242 |
c.Assert(err, IsNil) |
243 |
// Record incoming requests, and have them return a given reply.
|
|
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
244 |
fixedResponse := x509Response{ |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
245 |
StatusCode: http.StatusOK, |
246 |
Body: []byte("Response body"), |
|
247 |
}
|
|
248 |
rigFixedResponseDispatcher(&fixedResponse) |
|
136.1.1
by Raphael Badin
Expose testing utility. |
249 |
recordedRequests := make([]*X509Request, 0) |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
250 |
rigRecordingDispatcher(&recordedRequests) |
251 |
||
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
252 |
receivedResponse, err := session.post(uri, version, requestBody, requestContentType) |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
253 |
c.Assert(err, IsNil) |
254 |
||
255 |
c.Assert(len(recordedRequests), Equals, 1) |
|
256 |
request := recordedRequests[0] |
|
257 |
c.Check(request.URL, Equals, AZURE_URL+subscriptionID+"/"+uri) |
|
258 |
c.Check(request.Method, Equals, "POST") |
|
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
259 |
c.Check(request.APIVersion, Equals, version) |
28.1.1
by Raphael Badin
Add method to create hosted service. |
260 |
c.Check(request.ContentType, Equals, requestContentType) |
21.1.1
by Jeroen Vermeulen
Outline a POST and test, test GET, de-export Get, refactor dispatcher test fittings, prepare for returning errors. Tests: 2 failures. |
261 |
c.Check(request.Payload, DeepEquals, requestBody) |
57.3.1
by Raphael Badin
Add poller. |
262 |
c.Check(*receivedResponse, DeepEquals, fixedResponse) |
18.2.1
by Raphael Badin
Add test dispatcher. |
263 |
}
|
21.1.4
by Jeroen Vermeulen
More tests for post(): 2 failures, 1 panic. |
264 |
|
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
265 |
func (suite *x509SessionSuite) TestPostReportsClientSideError(c *C) { |
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
266 |
session, err := newX509Session("subscriptionid", "cert.pem") |
21.1.4
by Jeroen Vermeulen
More tests for post(): 2 failures, 1 panic. |
267 |
msg := "could not dispatch request" |
268 |
rigFailingDispatcher(fmt.Errorf(msg)) |
|
269 |
||
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
270 |
body, err := session.post("flop", "version", []byte("body"), "contentType") |
21.1.4
by Jeroen Vermeulen
More tests for post(): 2 failures, 1 panic. |
271 |
c.Assert(err, NotNil) |
272 |
||
273 |
c.Check(body, IsNil) |
|
274 |
c.Check(err, ErrorMatches, ".*"+msg+".*") |
|
275 |
}
|
|
276 |
||
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
277 |
func (suite *x509SessionSuite) TestPostReportsServerSideError(c *C) { |
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
278 |
session, err := newX509Session("subscriptionid", "cert.pem") |
279 |
fixedResponse := x509Response{ |
|
21.1.5
by Jeroen Vermeulen
Reformat. |
280 |
StatusCode: http.StatusForbidden, |
281 |
Body: []byte("Body"), |
|
21.1.4
by Jeroen Vermeulen
More tests for post(): 2 failures, 1 panic. |
282 |
}
|
283 |
rigFixedResponseDispatcher(&fixedResponse) |
|
284 |
||
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
285 |
reponse, err := session.post("fail", "version", []byte("request body"), "contentType") |
21.1.4
by Jeroen Vermeulen
More tests for post(): 2 failures, 1 panic. |
286 |
c.Assert(err, NotNil) |
287 |
||
288 |
serverError := err.(*ServerError) |
|
65.1.4
by Jeroen Vermeulen
Review suggestion: renamed Status() to StatusCode(). |
289 |
c.Check(serverError.StatusCode(), Equals, fixedResponse.StatusCode) |
57.3.1
by Raphael Badin
Add poller. |
290 |
c.Check(*reponse, DeepEquals, fixedResponse) |
21.1.4
by Jeroen Vermeulen
More tests for post(): 2 failures, 1 panic. |
291 |
}
|
30.2.1
by Jeroen Vermeulen
Tests and skeleton: DELETE support. Tests: 1 panic. |
292 |
|
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
293 |
func (suite *x509SessionSuite) TestDeleteIssuesRequest(c *C) { |
30.2.1
by Jeroen Vermeulen
Tests and skeleton: DELETE support. Tests: 1 panic. |
294 |
subscriptionID := "subscriptionID" |
295 |
uri := "resource" |
|
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
296 |
version := "test-version" |
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
297 |
session, err := newX509Session(subscriptionID, "cert.pem") |
30.2.1
by Jeroen Vermeulen
Tests and skeleton: DELETE support. Tests: 1 panic. |
298 |
c.Assert(err, IsNil) |
299 |
// Record incoming requests, and have them return a given reply.
|
|
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
300 |
fixedResponse := x509Response{StatusCode: http.StatusOK} |
30.2.1
by Jeroen Vermeulen
Tests and skeleton: DELETE support. Tests: 1 panic. |
301 |
rigFixedResponseDispatcher(&fixedResponse) |
136.1.1
by Raphael Badin
Expose testing utility. |
302 |
recordedRequests := make([]*X509Request, 0) |
30.2.1
by Jeroen Vermeulen
Tests and skeleton: DELETE support. Tests: 1 panic. |
303 |
rigRecordingDispatcher(&recordedRequests) |
304 |
||
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
305 |
response, err := session.delete(uri, version) |
30.2.1
by Jeroen Vermeulen
Tests and skeleton: DELETE support. Tests: 1 panic. |
306 |
c.Assert(err, IsNil) |
307 |
||
80.2.2
by Jeroen Vermeulen
Make x509session.delete() return the response. |
308 |
c.Check(*response, DeepEquals, fixedResponse) |
30.2.1
by Jeroen Vermeulen
Tests and skeleton: DELETE support. Tests: 1 panic. |
309 |
c.Assert(len(recordedRequests), Equals, 1) |
310 |
request := recordedRequests[0] |
|
311 |
c.Check(request.URL, Equals, AZURE_URL+subscriptionID+"/"+uri) |
|
312 |
c.Check(request.Method, Equals, "DELETE") |
|
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
313 |
c.Check(request.APIVersion, Equals, version) |
30.2.1
by Jeroen Vermeulen
Tests and skeleton: DELETE support. Tests: 1 panic. |
314 |
}
|
32.1.1
by Jeroen Vermeulen
Support PUT. |
315 |
|
64.1.1
by Jeroen Vermeulen
Unexport remaining test suites. |
316 |
func (suite *x509SessionSuite) TestPutIssuesRequest(c *C) { |
32.1.1
by Jeroen Vermeulen
Support PUT. |
317 |
subscriptionID := "subscriptionID" |
318 |
uri := "resource" |
|
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
319 |
version := "test-version" |
32.1.1
by Jeroen Vermeulen
Support PUT. |
320 |
requestBody := []byte("Request body") |
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
321 |
session, err := newX509Session(subscriptionID, "cert.pem") |
32.1.1
by Jeroen Vermeulen
Support PUT. |
322 |
c.Assert(err, IsNil) |
323 |
// Record incoming requests, and have them return a given reply.
|
|
60.3.1
by Jeroen Vermeulen
Unexport the X509 classes. |
324 |
fixedResponse := x509Response{ |
32.1.1
by Jeroen Vermeulen
Support PUT. |
325 |
StatusCode: http.StatusOK, |
326 |
}
|
|
327 |
rigFixedResponseDispatcher(&fixedResponse) |
|
136.1.1
by Raphael Badin
Expose testing utility. |
328 |
recordedRequests := make([]*X509Request, 0) |
32.1.1
by Jeroen Vermeulen
Support PUT. |
329 |
rigRecordingDispatcher(&recordedRequests) |
330 |
||
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
331 |
_, err = session.put(uri, version, requestBody, "text/plain") |
32.1.1
by Jeroen Vermeulen
Support PUT. |
332 |
c.Assert(err, IsNil) |
333 |
||
334 |
c.Assert(len(recordedRequests), Equals, 1) |
|
335 |
request := recordedRequests[0] |
|
336 |
c.Check(request.URL, Equals, AZURE_URL+subscriptionID+"/"+uri) |
|
337 |
c.Check(request.Method, Equals, "PUT") |
|
178.1.4
by Julian Edwards
Fix dispatcher etc tests |
338 |
c.Check(request.APIVersion, Equals, version) |
32.1.1
by Jeroen Vermeulen
Support PUT. |
339 |
c.Check(request.Payload, DeepEquals, requestBody) |
340 |
}
|