Skip to content

Commit

Permalink
add redirect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ninedraft committed Aug 2, 2022
1 parent f921440 commit ca5b378
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
48 changes: 48 additions & 0 deletions gemax/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package gemax_test
import (
"context"
"embed"
"errors"
"io"
"testing"

"github.com/ninedraft/gemax/gemax"
"github.com/ninedraft/gemax/gemax/internal/tester"
"github.com/ninedraft/gemax/gemax/status"
)

//go:embed testdata/client/pages/*
Expand Down Expand Up @@ -35,3 +37,49 @@ func TestClient(test *testing.T) {
}
test.Logf("%s", data)
}

func TestClient_Redirect(test *testing.T) {
var dialer = tester.DialFS{
Prefix: "testdata/client/pages/",
FS: testClientPages,
}
var client = &gemax.Client{
Dial: dialer.Dial,
}
var ctx = context.Background()
var resp, errFetch = client.Fetch(ctx, "gemini://redirect1.com")
if errFetch != nil {
test.Errorf("unexpected fetch error: %v", errFetch)
return
}
if resp.Status != status.Success {
test.Fatalf("unexpected status code %v", resp.Status)
}
defer func() { _ = resp.Close() }()
var data, errRead = io.ReadAll(resp)
if errRead != nil {
test.Errorf("unexpected error while reading response body: %v", errRead)
return
}
test.Logf("%s", data)
}

func TestClient_InfiniteRedirect(test *testing.T) {
var dialer = tester.DialFS{
Prefix: "testdata/client/pages/",
FS: testClientPages,
}
var client = &gemax.Client{
Dial: dialer.Dial,
}
var ctx = context.Background()
var _, errFetch = client.Fetch(ctx, "gemini://redirect2.com")
switch {
case errors.Is(errFetch, gemax.ErrTooManyRedirects):
// ok
case errFetch != nil:
test.Fatalf("unexpected error %q", errFetch)
default:
test.Fatalf("an error is expected, got nil")
}
}
3 changes: 3 additions & 0 deletions gemax/testdata/client/pages/redirect1.com
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
30 gemini://success.com

# redirect
3 changes: 3 additions & 0 deletions gemax/testdata/client/pages/redirect2.com
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
30 gemini://redirect2.com

# infinite redirect

0 comments on commit ca5b378

Please sign in to comment.