2021-05-14 15:19:02 +00:00
|
|
|
package upgrade
|
2018-05-04 06:41:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-09-29 12:39:12 +00:00
|
|
|
"io"
|
2018-05-04 06:41:40 +00:00
|
|
|
"testing"
|
|
|
|
)
|
2018-09-29 12:39:12 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// // 1. Parse request
|
2018-05-04 06:41:40 +00:00
|
|
|
// req, _ := request.Parse(s)
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// // 3. Build response
|
2018-05-04 06:41:40 +00:00
|
|
|
// res := req.BuildResponse()
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// // 4. Write into socket
|
2018-05-04 06:41:40 +00:00
|
|
|
// _, err := res.Send(s)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("Upgrade write error: %s", err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if res.GetStatusCode() != 101 {
|
|
|
|
// s.Close()
|
|
|
|
// return nil, fmt.Errorf("Upgrade error (HTTP %d)\n", res.GetStatusCode())
|
|
|
|
// }
|
|
|
|
|
2018-09-29 12:39:12 +00:00
|
|
|
func TestEOFSocket(t *testing.T) {
|
2018-05-04 06:41:40 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
socket := &bytes.Buffer{}
|
2018-05-04 06:41:40 +00:00
|
|
|
|
|
|
|
_, 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) {
|
2018-05-04 06:41:40 +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:41:40 +00:00
|
|
|
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},
|
2018-05-04 06:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
2018-05-04 06:41:40 +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:41:40 +00:00
|
|
|
} 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)
|
2018-05-04 06:41:40 +00:00
|
|
|
|
|
|
|
// 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-05-04 06:41:40 +00:00
|
|
|
|
2018-09-29 12:39:12 +00:00
|
|
|
}
|