mirror of
https://github.com/golang/go
synced 2024-11-19 13:14:42 -07:00
0c72eeb121
A malformed Host header can result in a malformed HTTP request. Clean them to avoid this. Updates #11206. We may come back and make this stricter for 1.6. Change-Id: I23c7d821cd9dbf66c3c15d26750f305e3672d984 Reviewed-on: https://go-review.googlesource.com/11241 Reviewed-by: Russ Cox <rsc@golang.org>
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
// Copyright 2014 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.
|
|
|
|
// Tests of internal functions with no better homes.
|
|
|
|
package http
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestForeachHeaderElement(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want []string
|
|
}{
|
|
{"Foo", []string{"Foo"}},
|
|
{" Foo", []string{"Foo"}},
|
|
{"Foo ", []string{"Foo"}},
|
|
{" Foo ", []string{"Foo"}},
|
|
|
|
{"foo", []string{"foo"}},
|
|
{"anY-cAsE", []string{"anY-cAsE"}},
|
|
|
|
{"", nil},
|
|
{",,,, , ,, ,,, ,", nil},
|
|
|
|
{" Foo,Bar, Baz,lower,,Quux ", []string{"Foo", "Bar", "Baz", "lower", "Quux"}},
|
|
}
|
|
for _, tt := range tests {
|
|
var got []string
|
|
foreachHeaderElement(tt.in, func(v string) {
|
|
got = append(got, v)
|
|
})
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCleanHost(t *testing.T) {
|
|
tests := []struct {
|
|
in, want string
|
|
}{
|
|
{"www.google.com", "www.google.com"},
|
|
{"www.google.com foo", "www.google.com"},
|
|
{"www.google.com/foo", "www.google.com"},
|
|
{" first character is a space", ""},
|
|
}
|
|
for _, tt := range tests {
|
|
got := cleanHost(tt.in)
|
|
if tt.want != got {
|
|
t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
}
|