From 32e45e3557e49cc868aa4437ef0aa56ab6470be8 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Wed, 25 Jan 2023 08:35:56 -0700 Subject: test coverage for the cgi contrib --- contrib/cgi/cgi_test.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 contrib/cgi/cgi_test.go (limited to 'contrib/cgi/cgi_test.go') 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) + } + }) + } +} -- cgit v1.2.3