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.GeminiCGIDirectory("/cgi-bin", "./testdata") server, err := gemini.NewServer(context.Background(), "localhost", "tcp", "127.0.0.1:0", handler, nil, tlsconf) 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) } }) } }