ws/internal/http/upgrade/request_test.go

167 lines
3.0 KiB
Go
Raw Normal View History

2021-05-14 15:19:02 +00:00
package upgrade
import (
"bytes"
2018-09-29 12:39:12 +00:00
"io"
"testing"
)
2018-09-29 12:39:12 +00:00
func TestEOFSocket(t *testing.T) {
2021-05-14 15:19:02 +00:00
socket := &bytes.Buffer{}
_, err := Parse(socket)
if err == nil {
t.Fatalf("Empty socket expected EOF, got no error")
} else if err != io.ErrUnexpectedEOF {
t.Fatalf("Empty socket expected EOF, got '%s'", err)
}
}
2018-09-29 12:39:12 +00:00
func TestInvalidRequestLine(t *testing.T) {
2021-05-14 15:19:02 +00:00
socket := &bytes.Buffer{}
2018-09-29 12:39:12 +00:00
cases := []struct {
Reqline string
HasError bool
}{
2018-09-29 12:39:12 +00:00
{"abc", true},
{"a c", true},
{"a c", true},
{"a c", true},
{"a b c", true},
{"GET invaliduri HTTP/1.1", true},
{"GET /validuri HTTP/1.1", false},
{"POST /validuri HTTP/1.1", true},
{"PUT /validuri HTTP/1.1", true},
{"DELETE /validuri HTTP/1.1", true},
{"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},
{"GET / HTTP/1.1", false},
{"GET / HTTP/2", false},
}
for ti, tc := range cases {
socket.Reset()
2018-09-29 12:39:12 +00:00
socket.Write([]byte(tc.Reqline))
socket.Write([]byte("\r\n\r\n"))
_, err := Parse(socket)
if !tc.HasError {
// no error -> ok
if err == nil {
continue
2018-09-29 12:39:12 +00:00
// error for the end of the request -> ok
} else if _, ok := err.(*IncompleteRequest); ok {
continue
}
t.Errorf("[%d] Expected no error", ti)
}
// missing required error -> error
if tc.HasError && err == nil {
t.Errorf("[%d] Expected error", ti)
continue
}
2018-09-29 12:39:12 +00:00
ir, ok := err.(*InvalidRequest)
// not InvalidRequest err -> error
if !ok || ir.Field != "Request-Line" {
t.Errorf("[%d] expected InvalidRequest", ti)
continue
}
}
2018-05-04 06:53:15 +00:00
}
2018-09-29 12:39:12 +00:00
func TestInvalidHost(t *testing.T) {
2018-05-04 06:53:15 +00:00
2018-09-29 12:39:12 +00:00
requestLine := []byte("GET / HTTP/1.1\r\n")
2018-05-04 06:53:15 +00:00
2021-05-14 15:19:02 +00:00
socket := &bytes.Buffer{}
2018-09-29 12:39:12 +00:00
cases := []struct {
2018-05-04 06:53:15 +00:00
Host string
HasError bool
}{
2018-09-29 12:39:12 +00:00
{"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},
2018-05-04 06:53:15 +00:00
}
for ti, tc := range cases {
socket.Reset()
socket.Write(requestLine)
2018-09-29 12:39:12 +00:00
socket.Write([]byte("Host: "))
socket.Write([]byte(tc.Host))
socket.Write([]byte("\r\n\r\n"))
2018-05-04 06:53:15 +00:00
_, err := Parse(socket)
if !tc.HasError {
// no error -> ok
if err == nil {
continue
2018-09-29 12:39:12 +00:00
// error for the end of the request -> ok
2018-05-04 06:53:15 +00:00
} 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
2018-09-29 12:39:12 +00:00
ir, ok := err.(*InvalidRequest)
2018-05-04 06:53:15 +00:00
// not InvalidRequest err -> error
if ok && ir.Field != "Host" {
t.Errorf("[%d] expected InvalidRequest", ti)
continue
}
}
2018-09-29 12:39:12 +00:00
}