Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for protocol header #869

Merged
merged 5 commits into from Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 114 additions & 0 deletions pkg/protocol/http1/req/header_test.go
Expand Up @@ -421,3 +421,117 @@ func TestRequestHeaderError(t *testing.T) {
err := ReadHeader(&rh, &er)
assert.True(t, errors.Is(err, errs.ErrNothingRead))
}

func TestReadHeader(t *testing.T) {
s := "P"
zr := mock.NewZeroCopyReader(s)
rh := protocol.RequestHeader{}
err := ReadHeader(&rh, zr)
assert.NotNil(t, err)
}

func TestParseHeaders(t *testing.T) {
rh := protocol.RequestHeader{}
_, err := parseHeaders(&rh, []byte{' '})
assert.NotNil(t, err)
}

func TestTryRead(t *testing.T) {
rh := protocol.RequestHeader{}
s := "P"
zr := mock.NewZeroCopyReader(s)
err := tryRead(&rh, zr, 0)
assert.NotNil(t, err)
}

func TestParseFirstLine(t *testing.T) {
tests := []struct {
input []byte
method string
uri string
protocol string
err error
}{
// Test case 1: n < 0
{
input: []byte("GET /path/to/resource HTTP/1.0\r\n"),
method: "GET",
uri: "/path/to/resource",
protocol: "HTTP/1.0",
err: nil,
},
// Test case 2: n == 0
{
input: []byte(" /path/to/resource HTTP/1.1\r\n"),
method: "",
uri: "",
protocol: "",
err: fmt.Errorf("requestURI cannot be empty in"),
},
// Test case 3: !bytes.Equal(b[n+1:], bytestr.StrHTTP11)
{
input: []byte("POST /path/to/resource HTTP/1.2\r\n"),
method: "POST",
uri: "/path/to/resource",
protocol: "HTTP/1.0",
err: nil,
},
}

for _, tc := range tests {
header := &protocol.RequestHeader{}
_, err := parseFirstLine(header, tc.input)
assert.NotNil(t, err)
}
}

func TestParse(t *testing.T) {
tests := []struct {
name string
input []byte
expected int
wantErr bool
}{
// normal test
{
name: "normal",
input: []byte("GET /path/to/resource HTTP/1.1\r\nHost: example.com\r\n\r\n"),
expected: len([]byte("GET /path/to/resource HTTP/1.1\r\nHost: example.com\r\n\r\n")),
wantErr: false,
},
// parseFirstLine error
{
name: "parseFirstLine error",
input: []byte("INVALID_LINE\r\nHost: example.com\r\n\r\n"),
expected: 0,
wantErr: true,
},
// ext.ReadRawHeaders error
{
name: "ext.ReadRawHeaders error",
input: []byte("GET /path/to/resource HTTP/1.1\r\nINVALID_HEADER\r\n\r\n"),
expected: 0,
wantErr: true,
},
// parseHeaders error
{
name: "parseHeaders error",
input: []byte("GET /path/to/resource HTTP/1.1\r\nHost: example.com\r\nINVALID_HEADER\r\n"),
expected: 0,
wantErr: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
header := &protocol.RequestHeader{}
bytesRead, err := parse(header, tc.input)
if (err != nil) != tc.wantErr {
t.Errorf("Expected error: %v, but got: %v", tc.wantErr, err)
}
if bytesRead != tc.expected {
t.Errorf("Expected bytes read: %d, but got: %d", tc.expected, bytesRead)
}
})
}
}
35 changes: 35 additions & 0 deletions pkg/protocol/multipart_test.go
Expand Up @@ -44,6 +44,7 @@ package protocol
import (
"bytes"
"mime/multipart"
"net/textproto"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -80,6 +81,15 @@ Content-Type: application/json
err = WriteMultipartForm(&w, form, "")
})

// call WriteField as twice
var body bytes.Buffer
mw := multipart.NewWriter(&body)
if err = mw.WriteField("field1", "value1"); err != nil {
t.Fatal(err)
}
err = WriteMultipartForm(&w, form, s)
assert.NotNil(t, err)

// normal test
err = WriteMultipartForm(&w, form, "foo")
if err != nil {
Expand Down Expand Up @@ -238,3 +248,28 @@ Content-Type: application/json
_, err = MarshalMultipartForm(form, " ")
assert.NotNil(t, err)
}

func TestAddFile(t *testing.T) {
t.Parallel()
bodyBuffer := &bytes.Buffer{}
w := multipart.NewWriter(bodyBuffer)
// add null file
err := AddFile(w, "test", "/test")
assert.NotNil(t, err)
}

func TestCreateMultipartHeader(t *testing.T) {
t.Parallel()

// filename == Null
hdr1 := make(textproto.MIMEHeader)
hdr1.Set("Content-Disposition", `form-data; name="test"`)
hdr1.Set("Content-Type", "application/json")
assert.DeepEqual(t, hdr1, CreateMultipartHeader("test", "", "application/json"))

// normal test
hdr2 := make(textproto.MIMEHeader)
hdr2.Set("Content-Disposition", `form-data; name="test"; filename="/test.go"`)
hdr2.Set("Content-Type", "application/json")
assert.DeepEqual(t, hdr2, CreateMultipartHeader("test", "/test.go", "application/json"))
}