PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/http/header_test.go

http://github.com/petar/GoHTTP
Go | 81 lines | 74 code | 4 blank | 3 comment | 3 complexity | 64bc31cfa0119ebdfe7fe489b10cd01e MD5 | raw file
 1// Copyright 2011 The Go Authors.  All rights reserved.
 2// Use of this source code is governed by a BSD-style
 3// license that can be found in the LICENSE file.
 4
 5package http
 6
 7import (
 8	"bytes"
 9	"testing"
10)
11
12var headerWriteTests = []struct {
13	h        Header
14	exclude  map[string]bool
15	expected string
16}{
17	{Header{}, nil, ""},
18	{
19		Header{
20			"Content-Type":   {"text/html; charset=UTF-8"},
21			"Content-Length": {"0"},
22		},
23		nil,
24		"Content-Length: 0\r\nContent-Type: text/html; charset=UTF-8\r\n",
25	},
26	{
27		Header{
28			"Content-Length": {"0", "1", "2"},
29		},
30		nil,
31		"Content-Length: 0\r\nContent-Length: 1\r\nContent-Length: 2\r\n",
32	},
33	{
34		Header{
35			"Expires":          {"-1"},
36			"Content-Length":   {"0"},
37			"Content-Encoding": {"gzip"},
38		},
39		map[string]bool{"Content-Length": true},
40		"Content-Encoding: gzip\r\nExpires: -1\r\n",
41	},
42	{
43		Header{
44			"Expires":          {"-1"},
45			"Content-Length":   {"0", "1", "2"},
46			"Content-Encoding": {"gzip"},
47		},
48		map[string]bool{"Content-Length": true},
49		"Content-Encoding: gzip\r\nExpires: -1\r\n",
50	},
51	{
52		Header{
53			"Expires":          {"-1"},
54			"Content-Length":   {"0"},
55			"Content-Encoding": {"gzip"},
56		},
57		map[string]bool{"Content-Length": true, "Expires": true, "Content-Encoding": true},
58		"",
59	},
60	{
61		Header{
62			"Nil":          nil,
63			"Empty":        {},
64			"Blank":        {""},
65			"Double-Blank": {"", ""},
66		},
67		nil,
68		"Blank: \r\nDouble-Blank: \r\nDouble-Blank: \r\n",
69	},
70}
71
72func TestHeaderWrite(t *testing.T) {
73	var buf bytes.Buffer
74	for i, test := range headerWriteTests {
75		test.h.WriteSubset(&buf, test.exclude)
76		if buf.String() != test.expected {
77			t.Errorf("#%d:\n got: %q\nwant: %q", i, buf.String(), test.expected)
78		}
79		buf.Reset()
80	}
81}