From e56ff09fc817e3be1f4dd5ba35ba79f5e066429a Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 4 May 2018 08:53:15 +0200 Subject: [PATCH] add Host tests --- internal/http/upgrade/request/header_check.go | 4 + internal/http/upgrade/request/request_test.go | 77 +++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/internal/http/upgrade/request/header_check.go b/internal/http/upgrade/request/header_check.go index 9b41bb7..086600c 100644 --- a/internal/http/upgrade/request/header_check.go +++ b/internal/http/upgrade/request/header_check.go @@ -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] diff --git a/internal/http/upgrade/request/request_test.go b/internal/http/upgrade/request/request_test.go index 66e4e3c..e7efa3c 100644 --- a/internal/http/upgrade/request/request_test.go +++ b/internal/http/upgrade/request/request_test.go @@ -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 + } + + } } \ No newline at end of file