~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/usso/url_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENSE file for details.
 
3
 
 
4
package usso
 
5
 
 
6
import (
 
7
        "launchpad.net/gocheck"
 
8
        "net/url"
 
9
)
 
10
 
 
11
// When NormalizeURL() is passed a simple URL, it will make no changes
 
12
// to it.
 
13
func (suite *USSOTestSuite) TestNormalizeURLReturnsBasicURL(c *gocheck.C) {
 
14
        output, err := NormalizeURL("http://example.com/path")
 
15
        c.Check(err, gocheck.Equals, nil)
 
16
        c.Check(output, gocheck.Equals, "http://example.com/path")
 
17
}
 
18
 
 
19
// NormalizeURL() strips the ":80" from http:// URLs that contain it.
 
20
func (suite *USSOTestSuite) TestNormalizeURLStripsStandardHTTPPort(
 
21
        c *gocheck.C) {
 
22
        output, err := NormalizeURL("http://example.com:80/path")
 
23
        c.Check(err, gocheck.Equals, nil)
 
24
        c.Check(output, gocheck.Equals, "http://example.com/path")
 
25
}
 
26
 
 
27
// NormalizeURL() strips the ":443" from https:// URLs that contain it.
 
28
func (suite *USSOTestSuite) TestNormalizeURLStripsStandardHTTPSPort(
 
29
        c *gocheck.C) {
 
30
        output, err := NormalizeURL("https://example.com:443/path")
 
31
        c.Check(err, gocheck.Equals, nil)
 
32
        c.Check(output, gocheck.Equals, "https://example.com/path")
 
33
}
 
34
 
 
35
// NormalizeURL() does not remove non-standard ports from the URL.
 
36
func (suite *USSOTestSuite) TestNormalizeURLLeavesNonstandardPort(
 
37
        c *gocheck.C) {
 
38
        output, err := NormalizeURL("http://example.com:8080/")
 
39
        c.Check(err, gocheck.Equals, nil)
 
40
        c.Check(output, gocheck.Equals, "http://example.com:8080/")
 
41
}
 
42
 
 
43
// NormalizeURL() strips the query string from URLs.
 
44
func (suite *USSOTestSuite) TestNormalizeURLStripsParameters(c *gocheck.C) {
 
45
        output, err := NormalizeURL("http://example.com/path?query=value&param=arg")
 
46
        c.Check(err, gocheck.Equals, nil)
 
47
        c.Check(output, gocheck.Equals, "http://example.com/path")
 
48
}
 
49
 
 
50
// NormalizeParameters() takes a url.Values instance and returns an
 
51
// encoded key=value string containing the parameters in that instance.
 
52
func (suite *USSOTestSuite) TestNormalizeParametersReturnsParameters(
 
53
        c *gocheck.C) {
 
54
        output, err := NormalizeParameters(url.Values{"param": []string{"value"}})
 
55
        c.Check(err, gocheck.Equals, nil)
 
56
        c.Check(output, gocheck.Equals, "param=value")
 
57
}
 
58
 
 
59
// NormalizeParameters() encodes multiple key/value parameters as a
 
60
// query string.
 
61
func (suite *USSOTestSuite) TestNormalizeParametersConcatenatesParameters(
 
62
        c *gocheck.C) {
 
63
        output, err := NormalizeParameters(
 
64
                url.Values{"a": []string{"1"}, "b": []string{"2"}})
 
65
        c.Check(err, gocheck.Equals, nil)
 
66
        c.Check(output, gocheck.Matches, "(a=1&b=2)")
 
67
}
 
68
 
 
69
// NormalizeParameters() escapes the parameters correctly when encoding
 
70
// them as a query string.
 
71
func (suite *USSOTestSuite) TestNormalizeParametersEscapesParameters(
 
72
        c *gocheck.C) {
 
73
        output, err := NormalizeParameters(url.Values{"a&b": []string{"1"}})
 
74
        c.Check(err, gocheck.Equals, nil)
 
75
        c.Check(output, gocheck.Equals, "a%26b=1")
 
76
}
 
77
 
 
78
// If oauth_signature appears in the parameters passed to
 
79
// NormalizeParameters(), it is omitted in the returned string as it does not
 
80
// have to be included in the computation of the new oauth_signature.
 
81
func (suite *USSOTestSuite) TestNormalizeParametersOmitsOAuthSignature(
 
82
        c *gocheck.C) {
 
83
        params := url.Values{
 
84
                "a":               []string{"1"},
 
85
                "oauth_signature": []string{"foobarsplatszot"},
 
86
                "z":               []string{"26"},
 
87
        }
 
88
        output, err := NormalizeParameters(params)
 
89
        c.Check(err, gocheck.Equals, nil)
 
90
        c.Check(output, gocheck.Matches, "(a=1&z=26)")
 
91
}
 
92
 
 
93
var escapeTests = map[string]string{
 
94
        "\x00": "%00",
 
95
        "\x01": "%01",
 
96
        "\x02": "%02",
 
97
        "\x03": "%03",
 
98
        "\x04": "%04",
 
99
        "\x05": "%05",
 
100
        "\x06": "%06",
 
101
        "\x07": "%07",
 
102
        "\x08": "%08",
 
103
        "\x09": "%09",
 
104
        "\x0a": "%0A",
 
105
        "\x0b": "%0B",
 
106
        "\x0c": "%0C",
 
107
        "\x0d": "%0D",
 
108
        "\x0e": "%0E",
 
109
        "\x0f": "%0F",
 
110
        "\x10": "%10",
 
111
        "\x11": "%11",
 
112
        "\x12": "%12",
 
113
        "\x13": "%13",
 
114
        "\x14": "%14",
 
115
        "\x15": "%15",
 
116
        "\x16": "%16",
 
117
        "\x17": "%17",
 
118
        "\x18": "%18",
 
119
        "\x19": "%19",
 
120
        "\x1a": "%1A",
 
121
        "\x1b": "%1B",
 
122
        "\x1c": "%1C",
 
123
        "\x1d": "%1D",
 
124
        "\x1e": "%1E",
 
125
        "\x1f": "%1F",
 
126
        "\x20": "%20",
 
127
        "\x21": "%21",
 
128
        "\x22": "%22",
 
129
        "\x23": "%23",
 
130
        "\x24": "%24",
 
131
        "\x25": "%25",
 
132
        "\x26": "%26",
 
133
        "\x27": "%27",
 
134
        "\x28": "%28",
 
135
        "\x29": "%29",
 
136
        "\x2a": "%2A",
 
137
        "\x2b": "%2B",
 
138
        "\x2c": "%2C",
 
139
        "\x2d": "-",
 
140
        "\x2e": ".",
 
141
        "\x2f": "%2F",
 
142
        "\x30": "0",
 
143
        "\x31": "1",
 
144
        "\x32": "2",
 
145
        "\x33": "3",
 
146
        "\x34": "4",
 
147
        "\x35": "5",
 
148
        "\x36": "6",
 
149
        "\x37": "7",
 
150
        "\x38": "8",
 
151
        "\x39": "9",
 
152
        "\x3a": "%3A",
 
153
        "\x3b": "%3B",
 
154
        "\x3c": "%3C",
 
155
        "\x3d": "%3D",
 
156
        "\x3e": "%3E",
 
157
        "\x3f": "%3F",
 
158
        "\x40": "%40",
 
159
        "\x41": "A",
 
160
        "\x42": "B",
 
161
        "\x43": "C",
 
162
        "\x44": "D",
 
163
        "\x45": "E",
 
164
        "\x46": "F",
 
165
        "\x47": "G",
 
166
        "\x48": "H",
 
167
        "\x49": "I",
 
168
        "\x4a": "J",
 
169
        "\x4b": "K",
 
170
        "\x4c": "L",
 
171
        "\x4d": "M",
 
172
        "\x4e": "N",
 
173
        "\x4f": "O",
 
174
        "\x50": "P",
 
175
        "\x51": "Q",
 
176
        "\x52": "R",
 
177
        "\x53": "S",
 
178
        "\x54": "T",
 
179
        "\x55": "U",
 
180
        "\x56": "V",
 
181
        "\x57": "W",
 
182
        "\x58": "X",
 
183
        "\x59": "Y",
 
184
        "\x5a": "Z",
 
185
        "\x5b": "%5B",
 
186
        "\x5c": "%5C",
 
187
        "\x5d": "%5D",
 
188
        "\x5e": "%5E",
 
189
        "\x5f": "_",
 
190
        "\x60": "%60",
 
191
        "\x61": "a",
 
192
        "\x62": "b",
 
193
        "\x63": "c",
 
194
        "\x64": "d",
 
195
        "\x65": "e",
 
196
        "\x66": "f",
 
197
        "\x67": "g",
 
198
        "\x68": "h",
 
199
        "\x69": "i",
 
200
        "\x6a": "j",
 
201
        "\x6b": "k",
 
202
        "\x6c": "l",
 
203
        "\x6d": "m",
 
204
        "\x6e": "n",
 
205
        "\x6f": "o",
 
206
        "\x70": "p",
 
207
        "\x71": "q",
 
208
        "\x72": "r",
 
209
        "\x73": "s",
 
210
        "\x74": "t",
 
211
        "\x75": "u",
 
212
        "\x76": "v",
 
213
        "\x77": "w",
 
214
        "\x78": "x",
 
215
        "\x79": "y",
 
216
        "\x7a": "z",
 
217
        "\x7b": "%7B",
 
218
        "\x7c": "%7C",
 
219
        "\x7d": "%7D",
 
220
        "\x7e": "~",
 
221
        "\x7f": "%7F",
 
222
        "\x80": "%80",
 
223
        "\x81": "%81",
 
224
        "\x82": "%82",
 
225
        "\x83": "%83",
 
226
        "\x84": "%84",
 
227
        "\x85": "%85",
 
228
        "\x86": "%86",
 
229
        "\x87": "%87",
 
230
        "\x88": "%88",
 
231
        "\x89": "%89",
 
232
        "\x8a": "%8A",
 
233
        "\x8b": "%8B",
 
234
        "\x8c": "%8C",
 
235
        "\x8d": "%8D",
 
236
        "\x8e": "%8E",
 
237
        "\x8f": "%8F",
 
238
        "\x90": "%90",
 
239
        "\x91": "%91",
 
240
        "\x92": "%92",
 
241
        "\x93": "%93",
 
242
        "\x94": "%94",
 
243
        "\x95": "%95",
 
244
        "\x96": "%96",
 
245
        "\x97": "%97",
 
246
        "\x98": "%98",
 
247
        "\x99": "%99",
 
248
        "\x9a": "%9A",
 
249
        "\x9b": "%9B",
 
250
        "\x9c": "%9C",
 
251
        "\x9d": "%9D",
 
252
        "\x9e": "%9E",
 
253
        "\x9f": "%9F",
 
254
        "\xa0": "%A0",
 
255
        "\xa1": "%A1",
 
256
        "\xa2": "%A2",
 
257
        "\xa3": "%A3",
 
258
        "\xa4": "%A4",
 
259
        "\xa5": "%A5",
 
260
        "\xa6": "%A6",
 
261
        "\xa7": "%A7",
 
262
        "\xa8": "%A8",
 
263
        "\xa9": "%A9",
 
264
        "\xaa": "%AA",
 
265
        "\xab": "%AB",
 
266
        "\xac": "%AC",
 
267
        "\xad": "%AD",
 
268
        "\xae": "%AE",
 
269
        "\xaf": "%AF",
 
270
        "\xb0": "%B0",
 
271
        "\xb1": "%B1",
 
272
        "\xb2": "%B2",
 
273
        "\xb3": "%B3",
 
274
        "\xb4": "%B4",
 
275
        "\xb5": "%B5",
 
276
        "\xb6": "%B6",
 
277
        "\xb7": "%B7",
 
278
        "\xb8": "%B8",
 
279
        "\xb9": "%B9",
 
280
        "\xba": "%BA",
 
281
        "\xbb": "%BB",
 
282
        "\xbc": "%BC",
 
283
        "\xbd": "%BD",
 
284
        "\xbe": "%BE",
 
285
        "\xbf": "%BF",
 
286
        "\xc0": "%C0",
 
287
        "\xc1": "%C1",
 
288
        "\xc2": "%C2",
 
289
        "\xc3": "%C3",
 
290
        "\xc4": "%C4",
 
291
        "\xc5": "%C5",
 
292
        "\xc6": "%C6",
 
293
        "\xc7": "%C7",
 
294
        "\xc8": "%C8",
 
295
        "\xc9": "%C9",
 
296
        "\xca": "%CA",
 
297
        "\xcb": "%CB",
 
298
        "\xcc": "%CC",
 
299
        "\xcd": "%CD",
 
300
        "\xce": "%CE",
 
301
        "\xcf": "%CF",
 
302
        "\xd0": "%D0",
 
303
        "\xd1": "%D1",
 
304
        "\xd2": "%D2",
 
305
        "\xd3": "%D3",
 
306
        "\xd4": "%D4",
 
307
        "\xd5": "%D5",
 
308
        "\xd6": "%D6",
 
309
        "\xd7": "%D7",
 
310
        "\xd8": "%D8",
 
311
        "\xd9": "%D9",
 
312
        "\xda": "%DA",
 
313
        "\xdb": "%DB",
 
314
        "\xdc": "%DC",
 
315
        "\xdd": "%DD",
 
316
        "\xde": "%DE",
 
317
        "\xdf": "%DF",
 
318
        "\xe0": "%E0",
 
319
        "\xe1": "%E1",
 
320
        "\xe2": "%E2",
 
321
        "\xe3": "%E3",
 
322
        "\xe4": "%E4",
 
323
        "\xe5": "%E5",
 
324
        "\xe6": "%E6",
 
325
        "\xe7": "%E7",
 
326
        "\xe8": "%E8",
 
327
        "\xe9": "%E9",
 
328
        "\xea": "%EA",
 
329
        "\xeb": "%EB",
 
330
        "\xec": "%EC",
 
331
        "\xed": "%ED",
 
332
        "\xee": "%EE",
 
333
        "\xef": "%EF",
 
334
        "\xf0": "%F0",
 
335
        "\xf1": "%F1",
 
336
        "\xf2": "%F2",
 
337
        "\xf3": "%F3",
 
338
        "\xf4": "%F4",
 
339
        "\xf5": "%F5",
 
340
        "\xf6": "%F6",
 
341
        "\xf7": "%F7",
 
342
        "\xf8": "%F8",
 
343
        "\xf9": "%F9",
 
344
        "\xfa": "%FA",
 
345
        "\xfb": "%FB",
 
346
        "\xfc": "%FC",
 
347
        "\xfd": "%FD",
 
348
        "\xfe": "%FE",
 
349
        "\xff": "%FF",
 
350
}
 
351
 
 
352
func (suite *USSOTestSuite) TestEscape(c *gocheck.C) {
 
353
        for in, expected := range escapeTests {
 
354
                c.Assert(escape(in), gocheck.Equals, expected)
 
355
        }
 
356
}