Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikPelli committed Dec 27, 2024
1 parent 05ef96e commit 666d8a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ linters:
- containedctx
- decorder
- dogsled
- dupl
- durationcheck
- errchkjson
- errname
Expand Down Expand Up @@ -80,6 +79,7 @@ linters:
- copyloopvar
- cyclop
- depguard
- dupl
- dupword
- err113
- exhaustruct
Expand Down
2 changes: 1 addition & 1 deletion internal/http1parser/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestHttp1ExtractHeaders_Empty(t *testing.T) {
"\r\n"
headers, err := http1parser.Http1ExtractHeaders([]byte(http1Data))
require.NoError(t, err)
assert.Len(t, headers, 0)
assert.Empty(t, headers)
}

func TestHttp1ExtractHeaders(t *testing.T) {
Expand Down
45 changes: 12 additions & 33 deletions internal/http1parser/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@ package http1parser_test
import (
"bufio"
"bytes"
"io"
"testing"

"github.com/elazarl/goproxy/internal/http1parser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"testing"
)

func TestCanonicalRequest(t *testing.T) {
data := "POST /index.html HTTP/1.1\r\n" +
const (
_data = "POST /index.html HTTP/1.1\r\n" +
"Host: www.test.com\r\n" +
"Accept: */*\r\n" +
"Content-Length: 17\r\n" +
"lowercase: 3z\r\n" +
"\r\n" +
`{"hello":"world"}`

data2 := "GET /index.html HTTP/1.1\r\n" +
_data2 = "GET /index.html HTTP/1.1\r\n" +
"Host: www.test.com\r\n" +
"Accept: */*\r\n" +
"lowercase: 3z\r\n" +
"\r\n"
)

func TestCanonicalRequest(t *testing.T) {
// Here we are simulating two requests on the same connection
http1Data := bytes.NewReader(append([]byte(data), data2...))
http1Data := bytes.NewReader(append([]byte(_data), _data2...))

var cloned bytes.Buffer
r := bufio.NewReader(io.TeeReader(http1Data, &cloned))
Expand Down Expand Up @@ -54,14 +57,7 @@ func TestCanonicalRequest(t *testing.T) {
}

func TestNonCanonicalRequest(t *testing.T) {
http1Data := bytes.NewReader([]byte("POST /index.html HTTP/1.1\r\n" +
"Host: www.test.com\r\n" +
"Accept: */*\r\n" +
"Content-Length: 17\r\n" +
"lowercase: 3z\r\n" +
"\r\n" +
`{"hello":"world"}`),
)
http1Data := bytes.NewReader([]byte(_data))

var cloned bytes.Buffer
r := bufio.NewReader(io.TeeReader(http1Data, &cloned))
Expand All @@ -74,27 +70,11 @@ func TestNonCanonicalRequest(t *testing.T) {
}

func TestMultipleNonCanonicalRequests(t *testing.T) {
data := "POST /index.html HTTP/1.1\r\n" +
"Host: www.test.com\r\n" +
"Accept: */*\r\n" +
"Content-Length: 17\r\n" +
"lowercase: 3z\r\n" +
"\r\n" +
`{"hello":"world"}`

data2 := "GET /index.html HTTP/1.1\r\n" +
"Host: www.test.com\r\n" +
"Accept: */*\r\n" +
"lowercase: 3z\r\n" +
"\r\n"

// Here we are simulating two requests on the same connection
http1Data := bytes.NewReader(append([]byte(data), data2...))
http1Data := bytes.NewReader(append([]byte(_data), _data2...))

var cloned bytes.Buffer
r := bufio.NewReader(io.TeeReader(http1Data, &cloned))

// 1st request
req, err := http1parser.ReadRequest(true, r, &cloned)
require.NoError(t, err)
assert.NotEmpty(t, req.Header)
Expand All @@ -106,11 +86,10 @@ func TestMultipleNonCanonicalRequests(t *testing.T) {
assert.Len(t, body, 17)
require.NoError(t, req.Body.Close())

// 2nd request
req, err = http1parser.ReadRequest(true, r, &cloned)
require.NoError(t, err)
assert.NotEmpty(t, req.Header)

assert.Equal(t, 0, r.Buffered())
assert.Equal(t, 0, cloned.Len())
assert.Equal(t, 0, r.Buffered())
}

0 comments on commit 666d8a7

Please sign in to comment.