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) } }) } }