From ff05d62013906f3086b452bfeda3e0d5b9b7a541 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 9 Jan 2023 16:40:24 -0700 Subject: Initial commit. some basics: - minimal README - some TODOs - server and request handler framework - contribs: file serving, request logging - server examples - CI setup --- gemini/response_test.go | 151 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 gemini/response_test.go (limited to 'gemini/response_test.go') diff --git a/gemini/response_test.go b/gemini/response_test.go new file mode 100644 index 0000000..3e1f41f --- /dev/null +++ b/gemini/response_test.go @@ -0,0 +1,151 @@ +package gemini_test + +import ( + "bytes" + "errors" + "io" + "testing" + + "tildegit.org/tjp/gus/gemini" +) + +func TestBuildResponses(t *testing.T) { + table := []struct { + name string + response *gemini.Response + status gemini.Status + meta string + body string + }{ + { + name: "input response", + response: gemini.Input("prompt here"), + status: gemini.StatusInput, + meta: "prompt here", + }, + { + name: "sensitive input response", + response: gemini.SensitiveInput("password please"), + status: gemini.StatusSensitiveInput, + meta: "password please", + }, + { + name: "success response", + response: gemini.Success("text/gemini", bytes.NewBufferString("body text here")), + status: gemini.StatusSuccess, + meta: "text/gemini", + body: "body text here", + }, + { + name: "temporary redirect", + response: gemini.Redirect("/foo/bar"), + status: gemini.StatusTemporaryRedirect, + meta: "/foo/bar", + }, + { + name: "permanent redirect", + response: gemini.PermanentRedirect("/baz/qux"), + status: gemini.StatusPermanentRedirect, + meta: "/baz/qux", + }, + { + name: "fail response", + response: gemini.Failure(errors.New("a failure")), + status: gemini.StatusTemporaryFailure, + meta: "a failure", + }, + { + name: "server unavailable", + response: gemini.Unavailable("server unavailable"), + status: gemini.StatusServerUnavailable, + meta: "server unavailable", + }, + { + name: "cgi error", + response: gemini.CGIError("some cgi error msg"), + status: gemini.StatusCGIError, + meta: "some cgi error msg", + }, + { + name: "proxy error", + response: gemini.ProxyError("upstream's full"), + status: gemini.StatusProxyError, + meta: "upstream's full", + }, + { + name: "rate limiting", + response: gemini.SlowDown(15), + status: gemini.StatusSlowDown, + meta: "15", + }, + { + name: "permanent failure", + response: gemini.PermanentFailure(errors.New("wut r u doin")), + status: gemini.StatusPermanentFailure, + meta: "wut r u doin", + }, + { + name: "not found", + response: gemini.NotFound("nope"), + status: gemini.StatusNotFound, + meta: "nope", + }, + { + name: "gone", + response: gemini.Gone("all out of that"), + status: gemini.StatusGone, + meta: "all out of that", + }, + { + name: "refuse proxy", + response: gemini.RefuseProxy("no I don't think I will"), + status: gemini.StatusProxyRequestRefused, + meta: "no I don't think I will", + }, + { + name: "bad request", + response: gemini.BadRequest("that don't make no sense"), + status: gemini.StatusBadRequest, + meta: "that don't make no sense", + }, + { + name: "require cert", + response: gemini.RequireCert("cert required"), + status: gemini.StatusClientCertificateRequired, + meta: "cert required", + }, + { + name: "cert auth failure", + response: gemini.CertAuthFailure("you can't see that"), + status: gemini.StatusCertificateNotAuthorized, + meta: "you can't see that", + }, + { + name: "invalid cert", + response: gemini.CertInvalid("bad cert dude"), + status: gemini.StatusCertificateNotValid, + meta: "bad cert dude", + }, + } + + for _, test := range table { + t.Run(test.name, func(t *testing.T) { + if test.response.Status != test.status { + t.Errorf("expected status %d, got %d", test.status, test.response.Status) + } + if test.response.Meta != test.meta { + t.Errorf("expected meta %q, got %q", test.meta, test.response.Meta) + } + + responseBytes, err := io.ReadAll(test.response) + if err != nil { + t.Fatalf("error reading response body: %q", err.Error()) + } + + body := string(bytes.SplitN(responseBytes, []byte("\r\n"), 2)[1]) + if body != test.body { + t.Errorf("expected body %q, got %q", test.body, body) + } + }) + } +} -- cgit v1.2.3