2016-09-14 04:17:31 +00:00
|
|
|
package gopher_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-03-31 14:02:21 +00:00
|
|
|
"net"
|
2016-09-14 06:42:30 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2020-07-21 02:07:12 +00:00
|
|
|
"time"
|
2016-09-14 04:17:31 +00:00
|
|
|
|
2016-09-16 12:15:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-03-31 14:02:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"git.mills.io/prologic/go-gopher"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testHost string = "localhost"
|
|
|
|
testPort int
|
2016-09-14 04:17:31 +00:00
|
|
|
)
|
|
|
|
|
2022-03-31 14:02:21 +00:00
|
|
|
func pickUnusedPort() (int, error) {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
port := l.Addr().(*net.TCPAddr).Port
|
|
|
|
if err := l.Close(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:07:12 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2022-03-31 14:02:21 +00:00
|
|
|
port, err := pickUnusedPort()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error finding a free port: %s", err)
|
|
|
|
}
|
|
|
|
testPort = port
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
gopher.Handle("/", gopher.FileServer(gopher.Dir("./testdata")))
|
|
|
|
gopher.HandleFunc("/hello", hello)
|
|
|
|
log.Printf("Test server starting on :%d\n", testPort)
|
|
|
|
log.Fatal(gopher.ListenAndServe(fmt.Sprintf(":%d", testPort), nil))
|
|
|
|
}()
|
2020-07-21 02:07:12 +00:00
|
|
|
|
|
|
|
// Because it can take some time for the server to spin up
|
|
|
|
// the tests are inconsistent - they'll fail if the server isn't
|
|
|
|
// ready, but pass otherwise. This problem seems more pronounced
|
|
|
|
// when running via the makefile.
|
|
|
|
//
|
|
|
|
// It seems like there should be a better way to do this
|
|
|
|
for attempts := 3; attempts > 0; attempts-- {
|
2022-03-31 14:02:21 +00:00
|
|
|
_, err := gopher.Get(fmt.Sprintf("gopher://%s:%d", testHost, testPort))
|
2020-07-21 02:07:12 +00:00
|
|
|
if err == nil {
|
2022-03-31 14:02:21 +00:00
|
|
|
log.Println("Server ready")
|
2020-07-21 02:07:12 +00:00
|
|
|
break
|
|
|
|
}
|
2022-03-31 14:02:21 +00:00
|
|
|
log.Printf("Server not ready, going to try again in a sec. %v\n", err)
|
2022-01-31 13:41:20 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2020-07-21 02:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:17:31 +00:00
|
|
|
func hello(w gopher.ResponseWriter, r *gopher.Request) {
|
|
|
|
w.WriteInfo("Hello World!")
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:32:16 +00:00
|
|
|
func Example_client() {
|
2016-09-14 04:17:31 +00:00
|
|
|
res, err := gopher.Get("gopher://gopher.floodgap.com")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Print(res.Dir.ToText())
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:32:16 +00:00
|
|
|
func Example_server() {
|
2016-09-14 04:17:31 +00:00
|
|
|
gopher.HandleFunc("/hello", hello)
|
|
|
|
log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
|
|
|
|
}
|
2016-09-14 04:23:24 +00:00
|
|
|
|
2016-09-14 04:32:16 +00:00
|
|
|
func Example_fileserver() {
|
2016-09-14 04:23:24 +00:00
|
|
|
gopher.Handle("/", gopher.FileServer(gopher.Dir("/tmp")))
|
|
|
|
log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
|
|
|
|
}
|
2016-09-14 06:42:30 +00:00
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2016-09-16 12:15:34 +00:00
|
|
|
assert := assert.New(t)
|
2022-03-31 14:02:21 +00:00
|
|
|
require := require.New(t)
|
2016-09-16 12:15:34 +00:00
|
|
|
|
2022-03-31 14:02:21 +00:00
|
|
|
res, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/1hello", testHost, testPort))
|
|
|
|
require.NoError(err)
|
2017-06-23 07:08:43 +00:00
|
|
|
assert.Len(res.Dir.Items, 1)
|
|
|
|
assert.Equal(res.Dir.Items[0].Type, gopher.INFO)
|
|
|
|
assert.Equal(res.Dir.Items[0].Description, "Hello World!")
|
2018-04-16 02:48:30 +00:00
|
|
|
|
|
|
|
out, err := res.Dir.ToText()
|
2022-03-31 14:02:21 +00:00
|
|
|
require.NoError(err)
|
2018-04-16 02:48:30 +00:00
|
|
|
assert.Equal(string(out), "iHello World!\t\terror.host\t1\r\n")
|
2017-06-23 07:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileServer(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-03-31 14:02:21 +00:00
|
|
|
require := require.New(t)
|
2017-06-23 07:08:43 +00:00
|
|
|
|
2022-03-31 14:02:21 +00:00
|
|
|
res, err := gopher.Get(fmt.Sprintf("gopher://%s:%d/", testHost, testPort))
|
|
|
|
require.NoError(err)
|
2018-12-30 13:29:30 +00:00
|
|
|
assert.Len(res.Dir.Items, 1)
|
2017-06-23 07:08:43 +00:00
|
|
|
|
|
|
|
json, err := res.Dir.ToJSON()
|
2022-03-31 14:02:21 +00:00
|
|
|
require.NoError(err)
|
2017-06-23 07:08:43 +00:00
|
|
|
|
2018-12-30 13:29:30 +00:00
|
|
|
log.Println(string(json))
|
2020-07-21 02:07:12 +00:00
|
|
|
assert.JSONEq(
|
|
|
|
`{"items":[{"type":"0","description":"hello.txt","selector":"/hello.txt","host":"127.0.0.1","port":7000,"extras":[]}]}`,
|
|
|
|
string(json))
|
2018-04-16 02:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseItemNull(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
item, err := gopher.ParseItem("")
|
|
|
|
assert.Nil(item)
|
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseItem(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-03-31 14:02:21 +00:00
|
|
|
require := require.New(t)
|
2018-04-16 02:48:30 +00:00
|
|
|
|
|
|
|
item, err := gopher.ParseItem("0foo\t/foo\tlocalhost\t70\r\n")
|
2022-03-31 14:02:21 +00:00
|
|
|
require.NoError(err)
|
2018-04-16 02:48:30 +00:00
|
|
|
assert.NotNil(item)
|
|
|
|
assert.Equal(item, &gopher.Item{
|
|
|
|
Type: gopher.FILE,
|
|
|
|
Description: "foo",
|
|
|
|
Selector: "/foo",
|
|
|
|
Host: "localhost",
|
|
|
|
Port: 70,
|
|
|
|
Extras: []string{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseItemMarshal(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-03-31 14:02:21 +00:00
|
|
|
require := require.New(t)
|
2018-04-16 02:48:30 +00:00
|
|
|
|
|
|
|
data := "0foo\t/foo\tlocalhost\t70\r\n"
|
|
|
|
item, err := gopher.ParseItem(data)
|
2022-03-31 14:02:21 +00:00
|
|
|
require.NoError(err)
|
2018-04-16 02:48:30 +00:00
|
|
|
assert.NotNil(item)
|
|
|
|
assert.Equal(item, &gopher.Item{
|
|
|
|
Type: gopher.FILE,
|
|
|
|
Description: "foo",
|
|
|
|
Selector: "/foo",
|
|
|
|
Host: "localhost",
|
|
|
|
Port: 70,
|
|
|
|
Extras: []string{},
|
|
|
|
})
|
|
|
|
|
|
|
|
data1, err := item.MarshalText()
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(data, string(data1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseItemMarshalIdempotency(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-03-31 14:02:21 +00:00
|
|
|
require := require.New(t)
|
2018-04-16 02:48:30 +00:00
|
|
|
|
|
|
|
data := "0"
|
|
|
|
item, err := gopher.ParseItem(data)
|
2022-03-31 14:02:21 +00:00
|
|
|
require.NoError(err)
|
2018-04-16 02:48:30 +00:00
|
|
|
assert.NotNil(item)
|
|
|
|
|
|
|
|
data1, err := item.MarshalText()
|
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
item1, err := gopher.ParseItem(string(data1))
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(item1)
|
|
|
|
assert.Equal(item, item1)
|
2016-09-14 06:42:30 +00:00
|
|
|
}
|