~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/net/textproto/reader_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
import (
8
8
        "bufio"
 
9
        "bytes"
9
10
        "io"
10
11
        "reflect"
11
12
        "strings"
23
24
        {"uSER-aGENT", "User-Agent"},
24
25
        {"user-agent", "User-Agent"},
25
26
        {"USER-AGENT", "User-Agent"},
 
27
        {"üser-agenT", "üser-Agent"}, // non-ASCII unchanged
26
28
}
27
29
 
28
30
func TestCanonicalMIMEHeaderKey(t *testing.T) {
239
241
                }
240
242
        }
241
243
}
 
244
 
 
245
func TestCommonHeaders(t *testing.T) {
 
246
        // need to disable the commonHeaders-based optimization
 
247
        // during this check, or we'd not be testing anything
 
248
        oldch := commonHeaders
 
249
        commonHeaders = []string{}
 
250
        defer func() { commonHeaders = oldch }()
 
251
 
 
252
        last := ""
 
253
        for _, h := range oldch {
 
254
                if last > h {
 
255
                        t.Errorf("%v is out of order", h)
 
256
                }
 
257
                if last == h {
 
258
                        t.Errorf("%v is duplicated", h)
 
259
                }
 
260
                if canon := CanonicalMIMEHeaderKey(h); h != canon {
 
261
                        t.Errorf("%v is not canonical", h)
 
262
                }
 
263
                last = h
 
264
        }
 
265
}
 
266
 
 
267
var clientHeaders = strings.Replace(`Host: golang.org
 
268
Connection: keep-alive
 
269
Cache-Control: max-age=0
 
270
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
 
271
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3
 
272
Accept-Encoding: gzip,deflate,sdch
 
273
Accept-Language: en-US,en;q=0.8,fr-CH;q=0.6
 
274
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
 
275
COOKIE: __utma=000000000.0000000000.0000000000.0000000000.0000000000.00; __utmb=000000000.0.00.0000000000; __utmc=000000000; __utmz=000000000.0000000000.00.0.utmcsr=code.google.com|utmccn=(referral)|utmcmd=referral|utmcct=/p/go/issues/detail
 
276
Non-Interned: test
 
277
 
 
278
`, "\n", "\r\n", -1)
 
279
 
 
280
var serverHeaders = strings.Replace(`Content-Type: text/html; charset=utf-8
 
281
Content-Encoding: gzip
 
282
Date: Thu, 27 Sep 2012 09:03:33 GMT
 
283
Server: Google Frontend
 
284
Cache-Control: private
 
285
Content-Length: 2298
 
286
VIA: 1.1 proxy.example.com:80 (XXX/n.n.n-nnn)
 
287
Connection: Close
 
288
Non-Interned: test
 
289
 
 
290
`, "\n", "\r\n", -1)
 
291
 
 
292
func BenchmarkReadMIMEHeader(b *testing.B) {
 
293
        b.ReportAllocs()
 
294
        var buf bytes.Buffer
 
295
        br := bufio.NewReader(&buf)
 
296
        r := NewReader(br)
 
297
        for i := 0; i < b.N; i++ {
 
298
                var want int
 
299
                var find string
 
300
                if (i & 1) == 1 {
 
301
                        buf.WriteString(clientHeaders)
 
302
                        want = 10
 
303
                        find = "Cookie"
 
304
                } else {
 
305
                        buf.WriteString(serverHeaders)
 
306
                        want = 9
 
307
                        find = "Via"
 
308
                }
 
309
                h, err := r.ReadMIMEHeader()
 
310
                if err != nil {
 
311
                        b.Fatal(err)
 
312
                }
 
313
                if len(h) != want {
 
314
                        b.Fatalf("wrong number of headers: got %d, want %d", len(h), want)
 
315
                }
 
316
                if _, ok := h[find]; !ok {
 
317
                        b.Fatalf("did not find key %s", find)
 
318
                }
 
319
        }
 
320
}
 
321
 
 
322
func BenchmarkUncommon(b *testing.B) {
 
323
        b.ReportAllocs()
 
324
        var buf bytes.Buffer
 
325
        br := bufio.NewReader(&buf)
 
326
        r := NewReader(br)
 
327
        for i := 0; i < b.N; i++ {
 
328
                buf.WriteString("uncommon-header-for-benchmark: foo\r\n\r\n")
 
329
                h, err := r.ReadMIMEHeader()
 
330
                if err != nil {
 
331
                        b.Fatal(err)
 
332
                }
 
333
                if _, ok := h["Uncommon-Header-For-Benchmark"]; !ok {
 
334
                        b.Fatal("Missing result header.")
 
335
                }
 
336
        }
 
337
}