summaryrefslogtreecommitdiff
path: root/spartan
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-04-29 17:38:07 -0600
committertjpcc <tjp@ctrl-c.club>2023-04-29 17:38:07 -0600
commitfcea3099cb2dce7f953e46389f83b6f9b58bef86 (patch)
treea8b5963c85e87a24555ba7aa71477365b8f58662 /spartan
parent039c58c9d00a4a5886fa99d7c7d472e6d02d6a67 (diff)
tests and bugfixes to spartan request parsing
Diffstat (limited to 'spartan')
-rw-r--r--spartan/request.go4
-rw-r--r--spartan/request_test.go45
2 files changed, 47 insertions, 2 deletions
diff --git a/spartan/request.go b/spartan/request.go
index 331b68c..ca1159b 100644
--- a/spartan/request.go
+++ b/spartan/request.go
@@ -37,7 +37,7 @@ func ParseRequest(rdr io.Reader) (*gus.Request, int, error) {
if !ok {
return nil, 0, InvalidRequestLine
}
- path, rest, ok := strings.Cut(line, " ")
+ path, rest, ok := strings.Cut(rest, " ")
if !ok {
return nil, 0, InvalidRequestLine
}
@@ -46,7 +46,7 @@ func ParseRequest(rdr io.Reader) (*gus.Request, int, error) {
return nil, 0, InvalidRequestLineEnding
}
- contentlen, err := strconv.Atoi(line[:len(line)-2])
+ contentlen, err := strconv.Atoi(rest[:len(rest)-2])
if err != nil {
return nil, 0, err
}
diff --git a/spartan/request_test.go b/spartan/request_test.go
new file mode 100644
index 0000000..bffecef
--- /dev/null
+++ b/spartan/request_test.go
@@ -0,0 +1,45 @@
+package spartan_test
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "tildegit.org/tjp/gus/spartan"
+)
+
+func TestParseRequest(t *testing.T) {
+ tests := []struct {
+ requestLine string
+ host string
+ path string
+ clen int
+ }{
+ {
+ requestLine: "foobar.ninja /baz/quux 0\r\n",
+ host: "foobar.ninja",
+ path: "/baz/quux",
+ clen: 0,
+ },
+ {
+ requestLine: "foo.bar / 12\r\n",
+ host: "foo.bar",
+ path: "/",
+ clen: 12,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.requestLine, func(t *testing.T) {
+ request, clen, err := spartan.ParseRequest(bytes.NewBufferString(test.requestLine))
+ require.Nil(t, err)
+
+ assert.Equal(t, test.host, request.Host)
+ assert.Equal(t, test.path, request.Path)
+ assert.Equal(t, test.path, request.RawPath)
+ assert.Equal(t, test.clen, clen)
+ })
+ }
+}