add Host tests

This commit is contained in:
xdrm-brackets 2018-05-04 08:53:15 +02:00
parent dc510ad5d9
commit e56ff09fc8
2 changed files with 81 additions and 0 deletions

View File

@ -17,6 +17,10 @@ func (r *T) extractHostPort(bb header.HeaderValue) error {
return &InvalidRequest{"Host", fmt.Sprintf("expected single value, got %d", len(bb))}
}
if len(bb[0]) <= 3 {
return &InvalidRequest{"Host", fmt.Sprintf("expected non-empty value (got %d bytes)", len(bb[0]))}
}
split := strings.Split(string(bb[0]), ":")
r.host = split[0]

View File

@ -58,6 +58,10 @@ func TestInvalidRequestLine(t *testing.T){
{ "OPTIONS /validuri HTTP/1.1", true },
{ "UNKNOWN /validuri HTTP/1.1", true },
{ "GET / HTTP", true },
{ "GET / HTTP/", true },
{ "GET / 1.1", true },
{ "GET / 1", true },
{ "GET / HTTP/52", true },
{ "GET / HTTP/1.", true },
{ "GET / HTTP/.1", true },
@ -102,5 +106,78 @@ func TestInvalidRequestLine(t *testing.T){
}
}
func TestInvalidHost(t *testing.T){
requestLine := []byte( "GET / HTTP/1.1\r\n" )
socket := new(bytes.Buffer)
cases := []struct{
Host string
HasError bool
}{
{ "1", true },
{ "12", true },
{ "123", true },
{ "1234", false },
{ "singlevalue", false },
{ "multi value", true },
{ "singlevalue:1", false },
{ "singlevalue:", true },
{ "singlevalue:x", true },
{ "xx:x", true },
{ ":xxx", true },
{ "xxx:", true },
{ "a:12", false },
{ "google.com", false },
{ "8.8.8.8", false },
{ "google.com:8080", false },
{ "8.8.8.8:8080", false },
}
for ti, tc := range cases {
socket.Reset()
socket.Write(requestLine)
socket.Write( []byte("Host: ") )
socket.Write( []byte(tc.Host) )
socket.Write( []byte("\r\n\r\n") )
_, err := Parse(socket)
if !tc.HasError {
// no error -> ok
if err == nil {
continue
// error for the end of the request -> ok
} else if _, ok := err.(*IncompleteRequest); ok {
continue
}
t.Errorf("[%d] Expected no error; %s", ti, err)
}
// missing required error -> error
if tc.HasError && err == nil {
t.Errorf("[%d] Expected error", ti)
continue
}
// check if InvalidRequest
ir, ok := err.(*InvalidRequest);
// not InvalidRequest err -> error
if ok && ir.Field != "Host" {
t.Errorf("[%d] expected InvalidRequest", ti)
continue
}
}
}