๐Ÿ’พ Archived View for source.community โ€บ ckaznocha โ€บ gemini โ€บ blob โ€บ main โ€บ response_test.go captured on 2023-04-26 at 13:23:14. Gemini links have been rewritten to link to archived content

View Raw

More Information

โฌ…๏ธ Previous capture (2023-01-29)

โžก๏ธ Next capture (2024-02-05)

-=-=-=-=-=-=-

                                                         .
,-. ,-. . . ,-. ,-. ,-.    ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . .
`-. | | | | |   |   |-'    |   | | | | | | | | | | | | | |  | |
`-' `-' `-^ '   `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-|
                                                             /|
                                                            `-'

Profile for ckaznocha

ckaznocha / gemini

git clone https://source.community/ckaznocha/gemini.git

Branches

Log

Tree

/response_test.go (main)

โ†‘ /

blob

View raw contents of /response_test.go (main)

โ”€โ”€โ”€โ”€โ•ฎ
   1โ”‚ package gemini_test
   2โ”‚ 
   3โ”‚ import (
   4โ”‚ 	"context"
   5โ”‚ 	"crypto/tls"
   6โ”‚ 	"fmt"
   7โ”‚ 	"io"
   8โ”‚ 	"mime"
   9โ”‚ 	"net"
  10โ”‚ 	"strings"
  11โ”‚ 	"testing"
  12โ”‚ 	"time"
  13โ”‚ 
  14โ”‚ 	"source.community/ckaznocha/gemini"
  15โ”‚ 	"source.community/ckaznocha/gemini/geminitest"
  16โ”‚ )
  17โ”‚ 
  18โ”‚ func Test_response_Input(t *testing.T) {
  19โ”‚ 	t.Parallel()
  20โ”‚ 
  21โ”‚ 	type args struct {
  22โ”‚ 		prompt      string
  23โ”‚ 		isSensitive bool
  24โ”‚ 	}
  25โ”‚ 
  26โ”‚ 	tests := []struct {
  27โ”‚ 		name string
  28โ”‚ 		want string
  29โ”‚ 		args args
  30โ”‚ 	}{
  31โ”‚ 		{
  32โ”‚ 			name: "",
  33โ”‚ 			args: args{
  34โ”‚ 				prompt: "Hello",
  35โ”‚ 			},
  36โ”‚ 			want: "10 Hello\r\n",
  37โ”‚ 		},
  38โ”‚ 		{
  39โ”‚ 			name: "",
  40โ”‚ 			args: args{
  41โ”‚ 				prompt:      "Hello",
  42โ”‚ 				isSensitive: true,
  43โ”‚ 			},
  44โ”‚ 			want: "11 Hello\r\n",
  45โ”‚ 		},
  46โ”‚ 	}
  47โ”‚ 
  48โ”‚ 	for _, tt := range tests {
  49โ”‚ 		tt := tt
  50โ”‚ 		t.Run(tt.name, func(t *testing.T) {
  51โ”‚ 			t.Parallel()
  52โ”‚ 
  53โ”‚ 			s := geminitest.NewServer(t, gemini.HandlerFunc(func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
  54โ”‚ 				w.Input(ctx, tt.args.prompt, tt.args.isSensitive)
  55โ”‚ 			}))
  56โ”‚ 
  57โ”‚ 			defer s.Close()
  58โ”‚ 
  59โ”‚ 			conn, err := tls.DialWithDialer(
  60โ”‚ 				&net.Dialer{Timeout: 1 * time.Second},
  61โ”‚ 				"tcp",
  62โ”‚ 				s.Addr,
  63โ”‚ 				&tls.Config{
  64โ”‚ 					InsecureSkipVerify: true,
  65โ”‚ 				},
  66โ”‚ 			)
  67โ”‚ 			if err != nil {
  68โ”‚ 				t.Fatalf("Unable to dial: error = %v", err)
  69โ”‚ 			}
  70โ”‚ 
  71โ”‚ 			if _, err = fmt.Fprint(conn, "gemini://localhost.com/\r\n"); err != nil {
  72โ”‚ 				t.Fatalf("Unable to write request: error = %v", err)
  73โ”‚ 			}
  74โ”‚ 
  75โ”‚ 			got, err := io.ReadAll(conn)
  76โ”‚ 			if err != nil {
  77โ”‚ 				t.Fatalf("Unable to read response: error = %v", err)
  78โ”‚ 			}
  79โ”‚ 
  80โ”‚ 			if string(got) != tt.want {
  81โ”‚ 				t.Fatalf("*response.Input(%s, %t) = %s, want %s", tt.args.prompt, tt.args.isSensitive, string(got), tt.want)
  82โ”‚ 			}
  83โ”‚ 		})
  84โ”‚ 	}
  85โ”‚ }
  86โ”‚ 
  87โ”‚ func Test_response_Redirect(t *testing.T) {
  88โ”‚ 	t.Parallel()
  89โ”‚ 
  90โ”‚ 	type args struct {
  91โ”‚ 		redirectURL string
  92โ”‚ 		isPermanant bool
  93โ”‚ 	}
  94โ”‚ 
  95โ”‚ 	tests := []struct {
  96โ”‚ 		name string
  97โ”‚ 		want string
  98โ”‚ 		args args
  99โ”‚ 	}{
 100โ”‚ 		{
 101โ”‚ 			name: "",
 102โ”‚ 			args: args{
 103โ”‚ 				redirectURL: "gemini://foo.bar",
 104โ”‚ 				isPermanant: false,
 105โ”‚ 			},
 106โ”‚ 			want: "30 gemini://foo.bar\r\n",
 107โ”‚ 		},
 108โ”‚ 		{
 109โ”‚ 			name: "",
 110โ”‚ 			args: args{
 111โ”‚ 				redirectURL: "gemini://foo.bar",
 112โ”‚ 				isPermanant: true,
 113โ”‚ 			},
 114โ”‚ 			want: "31 gemini://foo.bar\r\n",
 115โ”‚ 		},
 116โ”‚ 	}
 117โ”‚ 
 118โ”‚ 	for _, tt := range tests {
 119โ”‚ 		tt := tt
 120โ”‚ 		t.Run(tt.name, func(t *testing.T) {
 121โ”‚ 			t.Parallel()
 122โ”‚ 
 123โ”‚ 			s := geminitest.NewServer(t, gemini.HandlerFunc(func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
 124โ”‚ 				w.Redirect(ctx, tt.args.redirectURL, tt.args.isPermanant)
 125โ”‚ 			}))
 126โ”‚ 
 127โ”‚ 			defer s.Close()
 128โ”‚ 
 129โ”‚ 			conn, err := tls.DialWithDialer(
 130โ”‚ 				&net.Dialer{Timeout: 1 * time.Second},
 131โ”‚ 				"tcp",
 132โ”‚ 				s.Addr,
 133โ”‚ 				&tls.Config{
 134โ”‚ 					InsecureSkipVerify: true,
 135โ”‚ 				},
 136โ”‚ 			)
 137โ”‚ 			if err != nil {
 138โ”‚ 				t.Fatalf("Unable to dial: error = %v", err)
 139โ”‚ 			}
 140โ”‚ 
 141โ”‚ 			if _, err = fmt.Fprint(conn, "gemini://localhost.com/\r\n"); err != nil {
 142โ”‚ 				t.Fatalf("Unable to write request: error = %v", err)
 143โ”‚ 			}
 144โ”‚ 
 145โ”‚ 			got, err := io.ReadAll(conn)
 146โ”‚ 			if err != nil {
 147โ”‚ 				t.Fatalf("Unable to read response: error = %v", err)
 148โ”‚ 			}
 149โ”‚ 
 150โ”‚ 			if string(got) != tt.want {
 151โ”‚ 				t.Fatalf("*response.Redirect(%s, %t) = %s, want %s", tt.args.redirectURL, tt.args.isPermanant, string(got), tt.want)
 152โ”‚ 			}
 153โ”‚ 		})
 154โ”‚ 	}
 155โ”‚ }
 156โ”‚ 
 157โ”‚ func Test_response_Success(t *testing.T) {
 158โ”‚ 	t.Parallel()
 159โ”‚ 
 160โ”‚ 	type args struct {
 161โ”‚ 		body     io.Reader
 162โ”‚ 		mimeType string
 163โ”‚ 	}
 164โ”‚ 
 165โ”‚ 	tests := []struct {
 166โ”‚ 		name string
 167โ”‚ 		args args
 168โ”‚ 		want string
 169โ”‚ 	}{
 170โ”‚ 		{
 171โ”‚ 			name: "With mime type and body",
 172โ”‚ 			args: args{
 173โ”‚ 				mimeType: mime.FormatMediaType("text/gemini", map[string]string{"charset": "utf-8", "lang": "en"}),
 174โ”‚ 				body:     strings.NewReader("Hello, Gemini!"),
 175โ”‚ 			},
 176โ”‚ 			want: "20 text/gemini; charset=utf-8; lang=en\r\nHello, Gemini!",
 177โ”‚ 		},
 178โ”‚ 		{
 179โ”‚ 			name: "With body only",
 180โ”‚ 			args: args{
 181โ”‚ 				mimeType: "",
 182โ”‚ 				body:     strings.NewReader("Hello, Gemini!"),
 183โ”‚ 			},
 184โ”‚ 			want: "20 \r\nHello, Gemini!",
 185โ”‚ 		},
 186โ”‚ 		{
 187โ”‚ 			name: "With mime type only",
 188โ”‚ 			args: args{
 189โ”‚ 				mimeType: mime.FormatMediaType("text/gemini", map[string]string{"charset": "utf-8", "lang": "en"}),
 190โ”‚ 				body:     nil,
 191โ”‚ 			},
 192โ”‚ 			want: "20 text/gemini; charset=utf-8; lang=en\r\n",
 193โ”‚ 		},
 194โ”‚ 		{
 195โ”‚ 			name: "With no mime type or body",
 196โ”‚ 			args: args{
 197โ”‚ 				mimeType: "",
 198โ”‚ 				body:     nil,
 199โ”‚ 			},
 200โ”‚ 			want: "20 \r\n",
 201โ”‚ 		},
 202โ”‚ 	}
 203โ”‚ 
 204โ”‚ 	for _, tt := range tests {
 205โ”‚ 		tt := tt
 206โ”‚ 		t.Run(tt.name, func(t *testing.T) {
 207โ”‚ 			t.Parallel()
 208โ”‚ 
 209โ”‚ 			s := geminitest.NewServer(t, gemini.HandlerFunc(func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
 210โ”‚ 				gotWriter := w.Success(ctx, tt.args.mimeType)
 211โ”‚ 
 212โ”‚ 				if tt.args.body != nil {
 213โ”‚ 					_, err := io.Copy(gotWriter, tt.args.body)
 214โ”‚ 					if err != nil {
 215โ”‚ 						t.Fatal(err)
 216โ”‚ 					}
 217โ”‚ 				}
 218โ”‚ 			}))
 219โ”‚ 
 220โ”‚ 			defer s.Close()
 221โ”‚ 
 222โ”‚ 			conn, err := tls.DialWithDialer(
 223โ”‚ 				&net.Dialer{Timeout: 1 * time.Second},
 224โ”‚ 				"tcp",
 225โ”‚ 				s.Addr,
 226โ”‚ 				&tls.Config{
 227โ”‚ 					InsecureSkipVerify: true,
 228โ”‚ 				},
 229โ”‚ 			)
 230โ”‚ 			if err != nil {
 231โ”‚ 				t.Fatalf("Unable to dial: error = %v", err)
 232โ”‚ 			}
 233โ”‚ 
 234โ”‚ 			if _, err = fmt.Fprint(conn, "gemini://localhost.com/\r\n"); err != nil {
 235โ”‚ 				t.Fatalf("Unable to write request: error = %v", err)
 236โ”‚ 			}
 237โ”‚ 
 238โ”‚ 			got, err := io.ReadAll(conn)
 239โ”‚ 			if err != nil {
 240โ”‚ 				t.Fatalf("Unable to read response: error = %v", err)
 241โ”‚ 			}
 242โ”‚ 
 243โ”‚ 			if string(got) != tt.want {
 244โ”‚ 				t.Fatalf("*response.Success(%s) = %s, want %s", tt.args.mimeType, string(got), tt.want)
 245โ”‚ 			}
 246โ”‚ 		})
 247โ”‚ 	}
 248โ”‚ }
 249โ”‚ 
 250โ”‚ func Test_response_Failure(t *testing.T) {
 251โ”‚ 	t.Parallel()
 252โ”‚ 
 253โ”‚ 	type args struct {
 254โ”‚ 		msg  string
 255โ”‚ 		code gemini.StatusCode
 256โ”‚ 	}
 257โ”‚ 
 258โ”‚ 	tests := []struct {
 259โ”‚ 		name string
 260โ”‚ 		args args
 261โ”‚ 		want string
 262โ”‚ 	}{
 263โ”‚ 		{
 264โ”‚ 			name: "",
 265โ”‚ 			args: args{
 266โ”‚ 				code: gemini.StatusTemporaryFailure,
 267โ”‚ 				msg:  "oops",
 268โ”‚ 			},
 269โ”‚ 			want: "40 oops\r\n",
 270โ”‚ 		},
 271โ”‚ 		{
 272โ”‚ 			name: "",
 273โ”‚ 			args: args{
 274โ”‚ 				code: gemini.StatusPermanentFailure,
 275โ”‚ 				msg:  "oops",
 276โ”‚ 			},
 277โ”‚ 			want: "50 oops\r\n",
 278โ”‚ 		},
 279โ”‚ 		{
 280โ”‚ 			name: "",
 281โ”‚ 			args: args{
 282โ”‚ 				code: gemini.StatusTemporaryRedirect,
 283โ”‚ 				msg:  "oops",
 284โ”‚ 			},
 285โ”‚ 			want: "50 oops\r\n",
 286โ”‚ 		},
 287โ”‚ 	}
 288โ”‚ 
 289โ”‚ 	for _, tt := range tests {
 290โ”‚ 		tt := tt
 291โ”‚ 		t.Run(tt.name, func(t *testing.T) {
 292โ”‚ 			t.Parallel()
 293โ”‚ 
 294โ”‚ 			s := geminitest.NewServer(t, gemini.HandlerFunc(func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
 295โ”‚ 				w.Failure(ctx, tt.args.code, tt.args.msg)
 296โ”‚ 			}))
 297โ”‚ 
 298โ”‚ 			defer s.Close()
 299โ”‚ 
 300โ”‚ 			conn, err := tls.DialWithDialer(
 301โ”‚ 				&net.Dialer{Timeout: 1 * time.Second},
 302โ”‚ 				"tcp",
 303โ”‚ 				s.Addr,
 304โ”‚ 				&tls.Config{
 305โ”‚ 					InsecureSkipVerify: true,
 306โ”‚ 				},
 307โ”‚ 			)
 308โ”‚ 			if err != nil {
 309โ”‚ 				t.Fatalf("Unable to dial: error = %v", err)
 310โ”‚ 			}
 311โ”‚ 
 312โ”‚ 			if _, err = fmt.Fprint(conn, "gemini://localhost.com/\r\n"); err != nil {
 313โ”‚ 				t.Fatalf("Unable to write request: error = %v", err)
 314โ”‚ 			}
 315โ”‚ 
 316โ”‚ 			got, err := io.ReadAll(conn)
 317โ”‚ 			if err != nil {
 318โ”‚ 				t.Fatalf("Unable to read response: error = %v", err)
 319โ”‚ 			}
 320โ”‚ 
 321โ”‚ 			if string(got) != tt.want {
 322โ”‚ 				t.Fatalf("*response.Failure(%d, %s) = %s, want %s", tt.args.code, tt.args.msg, string(got), tt.want)
 323โ”‚ 			}
 324โ”‚ 		})
 325โ”‚ 	}
 326โ”‚ }
โ”€โ”€โ”€โ”€โ•ฏ

ยท ยท ยท

๐Ÿก Home

FAQs

Privacy Policy

Terms & Conditions

Official Gemlog

info@source.community

ยฉ 2023 source.community