diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-01-25 08:35:56 -0700 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-01-25 08:35:56 -0700 |
commit | 32e45e3557e49cc868aa4437ef0aa56ab6470be8 (patch) | |
tree | 8d70ccf4c208124a0d35b209723a5210bcb7c0b7 /contrib | |
parent | 997514292a8492d2291643e91081f3e790eefbaf (diff) |
test coverage for the cgi contrib
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/cgi/cgi_test.go | 81 | ||||
-rwxr-xr-x | contrib/cgi/testdata/fails | 3 | ||||
-rwxr-xr-x | contrib/cgi/testdata/hello_world | 3 | ||||
-rw-r--r-- | contrib/cgi/testdata/server.crt | 18 | ||||
-rw-r--r-- | contrib/cgi/testdata/server.key | 27 |
5 files changed, 132 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) + } + }) + } +} diff --git a/contrib/cgi/testdata/fails b/contrib/cgi/testdata/fails new file mode 100755 index 0000000..0f132d4 --- /dev/null +++ b/contrib/cgi/testdata/fails @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +exit 4 diff --git a/contrib/cgi/testdata/hello_world b/contrib/cgi/testdata/hello_world new file mode 100755 index 0000000..4d15f7d --- /dev/null +++ b/contrib/cgi/testdata/hello_world @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +printf "20 text/gemini\r\nhello, world!\n" diff --git a/contrib/cgi/testdata/server.crt b/contrib/cgi/testdata/server.crt new file mode 100644 index 0000000..4e3dc74 --- /dev/null +++ b/contrib/cgi/testdata/server.crt @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC7jCCAdYCAQcwDQYJKoZIhvcNAQELBQAwPTESMBAGA1UEAwwJbG9jYWxob3N0 +MQswCQYDVQQGEwJVUzEaMBgGA1UEBwwRU2FuIEZyYW5jaXNjbywgQ0EwHhcNMjMw +MTExMjAwMDU5WhcNMjUwNDE1MjAwMDU5WjA9MRIwEAYDVQQDDAlsb2NhbGhvc3Qx +CzAJBgNVBAYTAlVTMRowGAYDVQQHDBFTYW4gRnJhbmNpc2NvLCBDQTCCASIwDQYJ +KoZIhvcNAQEBBQADggEPADCCAQoCggEBALlaPa1AxDQnMo0qQxY5/Bf7MNf1x6tN +xjkpMnQnPM+cHmmlkEhI1zwLk/LrLxwq7+OOxMTPrJglrAiDAp1uCZHjKcTMFnwO +9M5vf8LjtYBjZd8+OSHyYV37gxw7h9/Wsxl+1Yw40QaJKM9auj2xOyaDj5Ou9+yp +CfbGSpVUTnqReOVFg2QSNwEviOZu1SvAouPyO98WKoXjn7K5mxE545e4mgF1EMht +jB5kH6kXqZSUszlGA1MkX3AlDsYJIcYnDwelNvw6XTPpkT2wNehxPyD0iP4rs+W4 +5hgV8wYokpgrM3xxe0c4mop5bzrp2Hyz3WxnF7KwtJgHW/6YxhG73skCAwEAATAN +BgkqhkiG9w0BAQsFAAOCAQEAfI+UE/3d0Fb8BZ2gtv1kUh8yx75LUbpg1aOEsZdP +Rji+GkL5xiFDsm7BwqTKziAjDtjL2qtGcJJ835shsGiUSK6qJuf9C944utUvCoFm +b4aUZ8fTmN7PkwRS61nIcHaS1zkiFzUdvbquV3QWSnl9kC+yDLHT0Z535tcvCMVM +bO7JMj1sxml4Y9B/hfY7zAZJt1giSNH1iDeX2pTpmPPI40UsRn98cC8HZ0d8wFrv +yc3hKkz8E+WTgZUf7jFk/KX/T5uwu+Y85emwfbb82KIR3oqhkJIfOfpqop2duZXB +hMuO1QWEBkZ/hpfrAsN/foz8v46P9qgW8gfOfzhyBcqLvA== +-----END CERTIFICATE----- diff --git a/contrib/cgi/testdata/server.key b/contrib/cgi/testdata/server.key new file mode 100644 index 0000000..44ce348 --- /dev/null +++ b/contrib/cgi/testdata/server.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpQIBAAKCAQEAuVo9rUDENCcyjSpDFjn8F/sw1/XHq03GOSkydCc8z5weaaWQ +SEjXPAuT8usvHCrv447ExM+smCWsCIMCnW4JkeMpxMwWfA70zm9/wuO1gGNl3z45 +IfJhXfuDHDuH39azGX7VjDjRBokoz1q6PbE7JoOP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