~nskaggs/+junk/juju-packaging-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/httprequest/checkisjson_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-27 20:23:11 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161027202311-sux4jk2o73p1d6rg
Re-add src

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package httprequest_test
 
2
 
 
3
import (
 
4
        "net/http"
 
5
        "strings"
 
6
 
 
7
        gc "gopkg.in/check.v1"
 
8
 
 
9
        "github.com/juju/httprequest"
 
10
)
 
11
 
 
12
type checkIsJSONSuite struct{}
 
13
 
 
14
var _ = gc.Suite(&checkIsJSONSuite{})
 
15
 
 
16
var checkIsJSONTests = []struct {
 
17
        about       string
 
18
        contentType string
 
19
        body        string
 
20
        expectError string
 
21
}{{
 
22
        about:       "simple json",
 
23
        contentType: "application/json",
 
24
        body:        "not json but unread",
 
25
}, {
 
26
        about:       "simple json with charset",
 
27
        contentType: "application/json; charset=UTF-8",
 
28
        body:        "not json but unread",
 
29
}, {
 
30
        about:       "plain text",
 
31
        contentType: "text/plain; charset=UTF-8",
 
32
        body:        "   some\n   text\t\n",
 
33
        expectError: `unexpected content type text/plain; want application/json; content: some; text`,
 
34
}, {
 
35
        about:       "plain text with leading newline",
 
36
        contentType: "text/plain; charset=UTF-8",
 
37
        body:        "\nsome text",
 
38
        expectError: `unexpected content type text/plain; want application/json; content: some text`,
 
39
}, {
 
40
        about:       "unknown content type",
 
41
        contentType: "something",
 
42
        body:        "some  \nstuff",
 
43
        expectError: `unexpected content type something; want application/json; content: "some  \\nstuff"`,
 
44
}, {
 
45
        about:       "bad content type",
 
46
        contentType: "/; charset=foo",
 
47
        body:        `some stuff`,
 
48
        expectError: `unexpected content type "/; charset=foo"; want application/json; content: "some stuff"`,
 
49
}, {
 
50
        about:       "large text body",
 
51
        contentType: "text/plain",
 
52
        body:        strings.Repeat("x", 1024+300),
 
53
        expectError: `unexpected content type text/plain; want application/json; content: ` + strings.Repeat("x", 1024) + ` \.\.\. \[300 bytes omitted]`,
 
54
}, {
 
55
        about:       "html with no text",
 
56
        contentType: "text/html",
 
57
        body:        "<html><a href=\"foo\">\n</a></html>",
 
58
        expectError: `unexpected content type text/html; want application/json; content: "<html><a href=\\"foo\\">\\n</a></html>"`,
 
59
}, {
 
60
        about:       "non-utf8 text",
 
61
        contentType: "text/plain; charset=iso8859-1",
 
62
        body:        "Pepp\xe9\n",
 
63
        // It would be nice to make this better, but we don't
 
64
        // really want to drag in all the charsets for this.
 
65
        expectError: "unexpected content type text/plain; want application/json; content: Pepp\uFFFD",
 
66
}, {
 
67
        about:       "actual html error message from proxy",
 
68
        contentType: "text/html; charset=UTF-8",
 
69
        body: `<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 
70
<html><head>
 
71
<title>502 Proxy Error</title>
 
72
</head><body>
 
73
<h1>Proxy Error</h1>
 
74
<p>The proxy server received an invalid
 
75
response from an upstream server.<br />
 
76
The proxy server could not handle the request <em><a href="/identity/v1/wait">GET&nbsp;/identity/v1/wait</a></em>.<p>
 
77
Reason: <strong>Error reading from remote server</strong></p></p>
 
78
<hr>
 
79
<address>Apache/2.4.7 (Ubuntu) Server at api.jujucharms.com Port 443</address>
 
80
</body></html>`,
 
81
        expectError: `unexpected content type text/html; want application/json; content: 502 Proxy Error; Proxy Error; The proxy server received an invalid response from an upstream server; The proxy server could not handle the request; GET /identity/v1/wait; Reason:; Error reading from remote server; Apache/2\.4\.7 \(Ubuntu\) Server at api.jujucharms.com Port 443`,
 
82
}, {
 
83
        about:       "actual html error message web page",
 
84
        contentType: "text/html; charset=UTF-8",
 
85
        body: `<!doctype html>
 
86
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en" dir="ltr"> <![endif]-->
 
87
<!--[if IE 7]>    <html class="lt-ie9 lt-ie8" lang="en" dir="ltr"> <![endif]-->
 
88
<!--[if IE 8]>    <html class="lt-ie9" lang="en" dir="ltr"> <![endif]-->
 
89
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" dir="ltr" itemscope itemtype="http://schema.org/Product"> <!--<![endif]-->
 
90
<head>
 
91
 
 
92
<title>Page not found | Juju</title>
 
93
 
 
94
<meta charset="UTF-8" />
 
95
<meta name="description" content="Deploy, manage and scale your environments on any cloud." />
 
96
<meta name="keywords" content="juju, charms, services, service orchestration, cloud, deployment, puppet, chef, fuel, cloud tools, service management, cloud management, configuration management, linux tool, openstack tool, ubuntu, cloud design, cloud orchestration" />
 
97
 
 
98
 
 
99
<meta name="author" content="Canonical" />
 
100
<meta name="viewport" content="width=device-width, initial-scale=1" />
 
101
 
 
102
<!--[if IE]>
 
103
<meta http-equiv="X-UA-Compatible" content="IE=8">
 
104
<![endif]-->
 
105
 
 
106
<link rel="shortcut icon" href="/static/img/favicon.ico" type="image/x-icon" />
 
107
 
 
108
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="/static/img/icons/apple-touch-icon-57x57-precomposed.png"/>
 
109
<link rel="apple-touch-icon-precomposed" sizes="60x60" href="/static/img/icons/apple-touch-icon-60x60-precomposed.png"/>
 
110
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/icons/apple-touch-icon-72x72-precomposed.png"/>
 
111
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="/static/img/icons/apple-touch-icon-76x76-precomposed.png"/>
 
112
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/icons/apple-touch-icon-114x114-precomposed.png"/>
 
113
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="/static/img/icons/apple-touch-icon-120x120-precomposed.png"/>
 
114
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/icons/apple-touch-icon-144x144-precomposed.png"/>
 
115
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="/static/img/icons/apple-touch-icon-152x152-precomposed.png"/>
 
116
<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/icons/apple-touch-icon-180x180-precomposed.png"/>
 
117
<link rel="apple-touch-icon-precomposed" href="/static/img/icons/apple-touch-icon-precomposed.png"/>
 
118
 
 
119
 
 
120
<!-- google fonts -->
 
121
<link href='https://fonts.googleapis.com/css?family=Ubuntu:400,300,300italic,400italic,700,700italic%7CUbuntu+Mono' rel='stylesheet' type='text/css' />
 
122
 
 
123
<!-- stylesheets -->
 
124
<link rel="stylesheet" type="text/css" media="screen" href="//assets.ubuntu.com/sites/guidelines/css/responsive/latest/ubuntu-styles.css" />
 
125
<link rel="stylesheet" type="text/css" media="screen" href="/static/css/build.css" />
 
126
 
 
127
<!-- load basic yui, our modules file, the loader and sub to set up modules with
 
128
combo load -->
 
129
<script type="text/javascript"
 
130
    
 
131
    src="/prod/combo?yui/yui/yui-min.js&amp;app/modules-min.js&amp;yui/loader/loader-min.js&amp;yui/substitute/substitute-min.js&amp;plugins/respond.min.js&amp;plugins/modernizr.2.7.1.js&amp;plugins/handlebars.runtime-v2.0.0.js&amp;plugins/highlight.pack.js&amp;templates/templates.js">
 
132
    
 
133
</script>
 
134
 
 
135
<script type="text/javascript">
 
136
      YUI.GlobalConfig = {
 
137
          combine: true,
 
138
          base: '/prod/combo?yui/',
 
139
          comboBase: '/prod/combo?',
 
140
          maxURLLength: 1300,
 
141
          root: 'yui/',
 
142
            groups: {
 
143
              app: {
 
144
                  combine: true,
 
145
                  base: '/prod/combo?app',
 
146
                  comboBase: '/prod/combo?',
 
147
                  root: 'app/',
 
148
                  
 
149
                  filter: 'min',
 
150
                  
 
151
                  // From modules.js
 
152
                  modules: YUI_MODULES,
 
153
              }
 
154
          },
 
155
          static_root:'/static/'
 
156
    };
 
157
 </script>
 
158
 
 
159
<!-- provide charmstore url -->
 
160
<script type="text/javascript">
 
161
    window.csUrl = "https://api.jujucharms.com/charmstore/v4";
 
162
</script>
 
163
 
 
164
 
 
165
<meta name="twitter:card" content="summary">
 
166
<meta name="twitter:site" content="@ubuntucloud">
 
167
<meta name="twitter:creator" content="@ubuntucloud">
 
168
<meta name="twitter:domain" content="jujucharms.com">
 
169
<meta name="twitter:title" content="Search results for foo in Juju">
 
170
<meta name="twitter:description" content="Deploy, manage, and scale your environments on any cloud.">
 
171
<meta name="twitter:image" content="https://jujucharms.com/static/img/juju-twitter.png">
 
172
 
 
173
 
 
174
</head>
 
175
 
 
176
<body class="
 
177
no-results no-match">
 
178
 
 
179
<header class="banner global" role="banner">
 
180
 
 
181
    <nav role="navigation" class="nav-primary nav-right">
 
182
    
 
183
 
 
184
    <span class="accessibility-aid">
 
185
        <a accesskey="s" href="#main-content">Jump to content</a>
 
186
    </span>
 
187
    <ul>
 
188
        <li>
 
189
    <a
 
190
        class=""
 
191
        href="/store"
 
192
    >
 
193
       Store
 
194
    </a>
 
195
    </li>
 
196
        <li><a href="https://demo.jujucharms.com" target="_blank" >Demo</a></li>
 
197
        <li>
 
198
    <a
 
199
        class=""
 
200
        href="/about"
 
201
    >
 
202
       About
 
203
    </a>
 
204
    </li>
 
205
        <li>
 
206
    <a
 
207
        class=""
 
208
        href="/about/features"
 
209
    >
 
210
       Features
 
211
    </a>
 
212
    </li>
 
213
        <li>
 
214
    <a
 
215
        class=""
 
216
        href="/community"
 
217
    >
 
218
       Community
 
219
    </a>
 
220
    </li>
 
221
        <li>
 
222
    <a
 
223
        class=""
 
224
        href="/docs/"
 
225
    >
 
226
       Docs
 
227
    </a>
 
228
    </li>
 
229
        <li>
 
230
    <a
 
231
        class=""
 
232
        href="/get-started"
 
233
    >
 
234
       Get started
 
235
    </a>
 
236
    </li>
 
237
    </ul>
 
238
    <ul class="user-nav">
 
239
        <li class="user-dropdown"><span id="user-dropdown"></span></li>
 
240
    </ul>
 
241
    <a href="#canonlist" class="nav-toggle no-script">☰</a>
 
242
</nav>
 
243
 
 
244
</header>
 
245
 
 
246
 
 
247
 
 
248
<div class="contextual-bar">
 
249
    
 
250
    <div class="logo">
 
251
        <a class="logo-ubuntu" href="/">
 
252
            <img width="73" height="30" src="/static/img/logos/logo.png" alt="" />
 
253
        </a>
 
254
    </div>
 
255
    <ul class="directory-path">
 
256
        <li><a href="https://demo.jujucharms.com" class="btn__see--more">Create <span>+</span></a></li>
 
257
    </ul>
 
258
 
 
259
    <form class="search-form" action="/q" method="GET">
 
260
    <input
 
261
        type="search" name="text"
 
262
        class="form-text" placeholder="Search the store"
 
263
        value="foo"
 
264
    />
 
265
    <button type="submit">
 
266
        <img
 
267
            src="/static/img/icons/search_16_active.svg"
 
268
            alt="Search" height="28"
 
269
        />
 
270
    </button>
 
271
    <a href="" class="search-close"><img src="/static/img/icons/close_16.svg" alt="" /></a>
 
272
</form>
 
273
 
 
274
 
 
275
</div>
 
276
 
 
277
<div class="wrapper">
 
278
<div id="main-content">
 
279
 
 
280
 
 
281
 
 
282
 
 
283
 
 
284
    
 
285
    <div class="row">
 
286
        <div class="inner-wrapper">
 
287
            <h2 class="error-title">404: Sorry, we couldn&rsquo;t find the page.</h2>
 
288
            
 
289
            <p class="cta">Try a different URL, try searching for solutions or learn how to <a href="/docs/authors-charm-writing">create your own solution</a>.</p>
 
290
            <nav class="error-nav">
 
291
                <ul>
 
292
                    <li><a href="/store" class="link-cta-positive">Browse the store</a></li>
 
293
                    <li><a href="/q/?type=bundle" class="link-cta-negative">All bundles</a></li>
 
294
                    <li><a href="/q/?type=charm" class="link-cta-negative">All charms</a></li>
 
295
                    <li><a href="https://github.com/CanonicalLtd/jujucharms.com/issues">Submit a bug</a></li>
 
296
                </ul>
 
297
            </nav>
 
298
            
 
299
        </div>
 
300
    </div>
 
301
 
 
302
 
 
303
 
 
304
</div><!-- /.inner-wrapper -->
 
305
 
 
306
</div><!-- /.wrapper -->
 
307
 
 
308
 
 
309
<div class="footer-wrapper strip-light">
 
310
<div class="store-cta">
 
311
    <a href="/store">Browse the store&nbsp;&rsaquo;</a>
 
312
</div>
 
313
<footer class="global clearfix">
 
314
    <p class="top-link">
 
315
        <a href="#">Back to the top</a>
 
316
    </p>
 
317
    <nav role="navigation" class="inner-wrapper">
 
318
        <div class="row">
 
319
            <div class="seven-col">
 
320
                <ul class="no-bullets">
 
321
                    <li><a class="external" href="https://demo.jujucharms.com">Demo</a></li>
 
322
                    <li><a href="/about">About</a></li>
 
323
                    <li><a href="/about/features">Features</a></li>
 
324
                    <li><a href="/docs">Docs</a></li>
 
325
                    <li><a href="/get-started">Get Started</a></li>
 
326
                </ul>
 
327
            </div>
 
328
            <div class="five-col last-col">
 
329
                <ul class="social--right">
 
330
                    <li class="social__item">
 
331
                        <a href="https://plus.google.com/106305712267007929608/posts" class="social__google"><span class="accessibility-aid">Juju on Google+</span></a>
 
332
                    </li>
 
333
                    <li class="social__item">
 
334
                        <a href="http://www.twitter.com/ubuntucloud" class="social__twitter"><span class="accessibility-aid">Ubuntu Cloud on Twitter</span></a>
 
335
                    </li>
 
336
                    <li class="social__item">
 
337
                        <a href="http://www.facebook.com/ubuntucloud" class="social__facebook"><span class="accessibility-aid">Ubuntu Cloud on Facebook</span></a>
 
338
                    </li>
 
339
                </ul>
 
340
            </div>
 
341
        </div>
 
342
    </nav>
 
343
    <div class="legal clearfix">
 
344
        <div class="legal-inner">
 
345
            <p class="twelve-col">
 
346
                &copy; 2015 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd.
 
347
            </p>
 
348
            <ul class="inline bullets clear">
 
349
                <li><a href="http://www.ubuntu.com/legal">Legal information</a></li>
 
350
                <li><a href="https://github.com/CanonicalLtd/jujucharms.com/issues">Report a bug on this site</a></li>
 
351
            </ul>
 
352
            <span class="accessibility-aid">
 
353
                <a href="#">Got to the top of the page</a>
 
354
            </span>
 
355
        </div>
 
356
    </div>
 
357
</footer>
 
358
</div>
 
359
 
 
360
<script>
 
361
    var isOperaMini = (navigator.userAgent.indexOf('Opera Mini') > -1);
 
362
    if(isOperaMini) {
 
363
        var root = document.documentElement;
 
364
        root.className += " opera-mini";
 
365
    }
 
366
</script>
 
367
 
 
368
<script>
 
369
YUI().use('storefront-cookie', 'storefront-utils', 'user-dropdown',
 
370
          function (Y) {
 
371
    Y.on('domready', function() {
 
372
        var inSession = false;
 
373
        var cookie = new Y.storefront.CookiePolicy();
 
374
        var utils = Y.storefront.utils;
 
375
 
 
376
        cookie.render();
 
377
        utils.mobileNav(Y.one('header.banner'));
 
378
        utils.setupSearch();
 
379
        var userDropdown = new Y.storefront.UserDropdownView({
 
380
            container: Y.one('#user-dropdown')
 
381
        });
 
382
        userDropdown.set('authenticated', inSession);
 
383
        
 
384
        if (inSession) {
 
385
            userDropdown.set('username', '');
 
386
        }
 
387
        userDropdown.render();
 
388
    });
 
389
});
 
390
</script>
 
391
 
 
392
 
 
393
<script type="text/template" id="cookie-warning-template">
 
394
    <div class="cookie-policy">
 
395
        <div class="inner-wrapper">
 
396
            <a href="?cp=close" class="link-cta">Close</a>
 
397
            <p>We use cookies to improve your experience. By your continued use of this site you accept such use. To change your settings please <a href="http://www.ubuntu.com/legal/terms-and-policies/privacy-policy#cookies">see our policy</a>.</p>
 
398
        </div>
 
399
    </div>
 
400
</script>
 
401
 
 
402
 
 
403
<script>
 
404
    YUI().use('node', 'search-view', function (Y) {
 
405
        Y.on('domready', function() {
 
406
            var view = new Y.storefront.SearchView({
 
407
                container: Y.one('#main-content')
 
408
            });
 
409
            view.render();
 
410
        });
 
411
    });
 
412
</script>
 
413
 
 
414
 
 
415
<!-- google analytics -->
 
416
<script>
 
417
  var _gaq = _gaq || [];
 
418
  _gaq.push(['_setAccount', 'UA-1018242-44']);
 
419
  _gaq.push(['_trackPageview']);
 
420
 
 
421
  (function() {
 
422
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 
423
  ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
 
424
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 
425
  })();
 
426
</script>
 
427
 
 
428
 
 
429
<!-- {version: ['0.1.29', '']} -->
 
430
 
 
431
</body>
 
432
</html>
 
433
`,
 
434
        expectError: `unexpected content type text/html; want application/json; content: Page not found | Juju; Jump to content; Store; Demo; About; Features; Community; Docs; Get started; ☰; Create; \+; 404: Sorry, we couldn’t find the page; Try a different URL, try searching for solutions or learn how to; create your own solution; Browse the store; All bundles; All charms; Submit a bug; Browse the store ›; Back to the top; Demo; About; Features; Docs; Get Started; Juju on Google+; Ubuntu Cloud on Twitter; Ubuntu Cloud on Facebook; © 2015 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd; Legal information; Report a bug on this site; Got to the top of the page`,
 
435
}}
 
436
 
 
437
func (checkIsJSONSuite) TestCheckIsJSON(c *gc.C) {
 
438
        *httprequest.MaxErrorBodySize = 16 * 1024
 
439
        for i, test := range checkIsJSONTests {
 
440
                c.Logf("test %d: %s", i, test.about)
 
441
                r := strings.NewReader(test.body)
 
442
                err := httprequest.CheckIsJSON(http.Header{
 
443
                        "Content-Type": {test.contentType},
 
444
                }, r)
 
445
                if test.expectError == "" {
 
446
                        c.Assert(err, gc.IsNil)
 
447
                        c.Assert(r.Len(), gc.Equals, len(test.body))
 
448
                        continue
 
449
                }
 
450
                c.Assert(err, gc.ErrorMatches, test.expectError)
 
451
                if len(test.body) > *httprequest.MaxErrorBodySize {
 
452
                        c.Assert(r.Len(), gc.Equals, *httprequest.MaxErrorBodySize-len(test.body))
 
453
                }
 
454
        }
 
455
}