summaryrefslogtreecommitdiff
path: root/gemini/roundtrip_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'gemini/roundtrip_test.go')
-rw-r--r--gemini/roundtrip_test.go80
1 files changed, 54 insertions, 26 deletions
diff --git a/gemini/roundtrip_test.go b/gemini/roundtrip_test.go
index 4bac239..4f48e47 100644
--- a/gemini/roundtrip_test.go
+++ b/gemini/roundtrip_test.go
@@ -9,56 +9,84 @@ import (
"net/url"
"testing"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
"tildegit.org/tjp/gus"
"tildegit.org/tjp/gus/gemini"
)
func TestRoundTrip(t *testing.T) {
tlsConf, err := gemini.FileTLS("./testdata/server.crt", "./testdata/server.key")
- if err != nil {
- t.Fatalf("FileTLS(): %s", err.Error())
- }
+ require.Nil(t, err)
handler := func(ctx context.Context, req *gus.Request) *gus.Response {
return gemini.Success("text/gemini", bytes.NewBufferString("you've found my page"))
}
server, err := gemini.NewServer(context.Background(), nil, tlsConf, "tcp", "127.0.0.1:0", handler)
- if err != nil {
- t.Fatalf("NewServer(): %s", err.Error())
- }
+ require.Nil(t, err)
go server.Serve()
defer server.Close()
u, err := url.Parse(fmt.Sprintf("gemini://%s/test", server.Address()))
- if err != nil {
- t.Fatalf("url.Parse: %s", err.Error())
- }
+ require.Nil(t, err)
cli := gemini.NewClient(testClientTLS())
response, err := cli.RoundTrip(&gus.Request{URL: u})
- if err != nil {
- t.Fatalf("RoundTrip(): %s", err.Error())
- }
+ require.Nil(t, err)
- if response.Status != gemini.StatusSuccess {
- t.Errorf("response status: expected %d, got %d", gemini.StatusSuccess, response.Status)
- }
- if response.Meta != "text/gemini" {
- t.Errorf("response meta: expected \"text/gemini\", got %q", response.Meta)
- }
+ assert.Equal(t, gemini.StatusSuccess, response.Status)
+ assert.Equal(t, "text/gemini", response.Meta)
- if response.Body == nil {
- t.Fatal("succcess response has nil body")
- }
+ require.NotNil(t, response.Body)
body, err := io.ReadAll(response.Body)
- if err != nil {
- t.Fatalf("ReadAll: %s", err.Error())
- }
- if string(body) != "you've found my page" {
- t.Errorf("response body: expected \"you've found my page\", got %q", string(body))
+ require.Nil(t, err)
+
+ assert.Equal(t, "you've found my page", string(body))
+}
+
+func TestTitanRequest(t *testing.T) {
+ tlsConf, err := gemini.FileTLS("./testdata/server.crt", "./testdata/server.key")
+ require.Nil(t, err)
+
+ invoked := false
+ handler := func(ctx context.Context, request *gus.Request) *gus.Response {
+ invoked = true
+
+ body := ctx.Value(gemini.TitanRequestBody)
+ if !assert.NotNil(t, body) {
+ return gemini.Success("", nil)
+ }
+
+ bodyBytes, err := io.ReadAll(body.(io.Reader))
+ require.Nil(t, err)
+
+ assert.Equal(t, "the request body\n", string(bodyBytes))
+ return gemini.Success("", nil)
}
+
+ server, err := gemini.NewServer(context.Background(), nil, tlsConf, "tcp", "127.0.0.1:0", handler)
+ require.Nil(t, err)
+
+ go server.Serve()
+ defer server.Close()
+
+ conn, err := tls.Dial(server.Network(), server.Address(), testClientTLS())
+ require.Nil(t, err)
+
+ _, err = fmt.Fprintf(
+ conn,
+ "titan://%s/foobar;size=17;mime=text/plain\r\nthe request body\n",
+ server.Address(),
+ )
+ require.Nil(t, err)
+
+ _, err = io.ReadAll(conn)
+ require.Nil(t, err)
+
+ assert.True(t, invoked)
}
func testClientTLS() *tls.Config {