summaryrefslogtreecommitdiff
path: root/contrib/cgi/cgi_test.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-01-25 08:35:56 -0700
committertjpcc <tjp@ctrl-c.club>2023-01-25 08:35:56 -0700
commit32e45e3557e49cc868aa4437ef0aa56ab6470be8 (patch)
tree8d70ccf4c208124a0d35b209723a5210bcb7c0b7 /contrib/cgi/cgi_test.go
parent997514292a8492d2291643e91081f3e790eefbaf (diff)
test coverage for the cgi contrib
Diffstat (limited to 'contrib/cgi/cgi_test.go')
-rw-r--r--contrib/cgi/cgi_test.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/contrib/cgi/cgi_test.go b/contrib/cgi/cgi_test.go
new file mode 100644
index 0000000..c265050
--- /dev/null
+++ b/contrib/cgi/cgi_test.go
@@ -0,0 +1,81 @@
+package cgi_test
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "strconv"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "tildegit.org/tjp/gus"
+ "tildegit.org/tjp/gus/contrib/cgi"
+ "tildegit.org/tjp/gus/gemini"
+)
+
+func TestCGIDirectory(t *testing.T) {
+ tlsconf, err := gemini.FileTLS("testdata/server.crt", "testdata/server.key")
+ require.Nil(t, err)
+
+ handler := cgi.CGIDirectory("/cgi-bin", "./testdata")
+ server, err := gemini.NewServer(context.Background(), nil, tlsconf, "tcp", "127.0.0.1:0", handler)
+ require.Nil(t, err)
+
+ go func() { assert.Nil(t, server.Serve()) }()
+ defer server.Close()
+
+ tests := []struct {
+ requestPath string
+ responseCode gus.Status
+ responseBody string
+ }{
+ {
+ requestPath: "/cgi-bin/hello_world",
+ responseCode: gemini.StatusSuccess,
+ responseBody: "hello, world!\n",
+ },
+ {
+ requestPath: "/cgi-bin/server.key",
+ responseCode: gemini.StatusNotFound,
+ },
+ {
+ requestPath: "/cgi-bin/non-existent",
+ responseCode: gemini.StatusNotFound,
+ },
+ {
+ requestPath: "/cgi-bin/fails",
+ responseCode: gemini.StatusCGIError,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.requestPath, func(t *testing.T) {
+ conn, err := tls.Dial(
+ server.Network(),
+ server.Address(),
+ &tls.Config{InsecureSkipVerify: true},
+ )
+ require.Nil(t, err)
+
+ _, err = fmt.Fprintf(conn, "gemini://%s%s\r\n", server.Address(), test.requestPath)
+ require.Nil(t, err)
+
+ response, err := io.ReadAll(conn)
+ require.Nil(t, err)
+
+ code, err := strconv.Atoi(string(response[:2]))
+ if assert.Nil(t, err) {
+ assert.Equal(t, test.responseCode, gus.Status(code))
+ }
+
+ _, body, found := strings.Cut(string(response), "\r\n")
+ if assert.True(t, found) && test.responseBody != "" {
+ assert.Equal(t, test.responseBody, body)
+ }
+ })
+ }
+}