1
0
mirror of https://github.com/golang/go synced 2024-10-07 15:21:23 -06:00
go/src/pkg/net/http/header_test.go

125 lines
2.7 KiB
Go
Raw Normal View History

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http
import (
"bytes"
"testing"
)
var headerWriteTests = []struct {
h Header
exclude map[string]bool
expected string
}{
{Header{}, nil, ""},
{
Header{
"Content-Type": {"text/html; charset=UTF-8"},
"Content-Length": {"0"},
},
nil,
"Content-Length: 0\r\nContent-Type: text/html; charset=UTF-8\r\n",
},
{
Header{
"Content-Length": {"0", "1", "2"},
},
nil,
"Content-Length: 0\r\nContent-Length: 1\r\nContent-Length: 2\r\n",
},
{
Header{
"Expires": {"-1"},
"Content-Length": {"0"},
"Content-Encoding": {"gzip"},
},
map[string]bool{"Content-Length": true},
"Content-Encoding: gzip\r\nExpires: -1\r\n",
},
{
Header{
"Expires": {"-1"},
"Content-Length": {"0", "1", "2"},
"Content-Encoding": {"gzip"},
},
map[string]bool{"Content-Length": true},
"Content-Encoding: gzip\r\nExpires: -1\r\n",
},
{
Header{
"Expires": {"-1"},
"Content-Length": {"0"},
"Content-Encoding": {"gzip"},
},
map[string]bool{"Content-Length": true, "Expires": true, "Content-Encoding": true},
"",
},
{
Header{
"Nil": nil,
"Empty": {},
"Blank": {""},
"Double-Blank": {"", ""},
},
nil,
"Blank: \r\nDouble-Blank: \r\nDouble-Blank: \r\n",
},
}
func TestHeaderWrite(t *testing.T) {
var buf bytes.Buffer
for i, test := range headerWriteTests {
test.h.WriteSubset(&buf, test.exclude)
if buf.String() != test.expected {
t.Errorf("#%d:\n got: %q\nwant: %q", i, buf.String(), test.expected)
}
buf.Reset()
}
}
type hasTokenTest struct {
header string
token string
want bool
}
var hasTokenTests = []hasTokenTest{
{"", "", false},
{"", "foo", false},
{"foo", "foo", true},
{"foo ", "foo", true},
{" foo", "foo", true},
{" foo ", "foo", true},
{"foo,bar", "foo", true},
{"bar,foo", "foo", true},
{"bar, foo", "foo", true},
{"bar,foo, baz", "foo", true},
{"bar, foo,baz", "foo", true},
{"bar,foo, baz", "foo", true},
{"bar, foo, baz", "foo", true},
{"FOO", "foo", true},
{"FOO ", "foo", true},
{" FOO", "foo", true},
{" FOO ", "foo", true},
{"FOO,BAR", "foo", true},
{"BAR,FOO", "foo", true},
{"BAR, FOO", "foo", true},
{"BAR,FOO, baz", "foo", true},
{"BAR, FOO,BAZ", "foo", true},
{"BAR,FOO, BAZ", "foo", true},
{"BAR, FOO, BAZ", "foo", true},
{"foobar", "foo", false},
{"barfoo ", "foo", false},
}
func TestHasToken(t *testing.T) {
for _, tt := range hasTokenTests {
if hasToken(tt.header, tt.token) != tt.want {
t.Errorf("hasToken(%q, %q) = %v; want %v", tt.header, tt.token, !tt.want, tt.want)
}
}
}