2016-09-14 04:17:31 +00:00
|
|
|
package gopher_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-09-14 06:42:30 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2016-09-14 04:17:31 +00:00
|
|
|
|
|
|
|
"github.com/prologic/go-gopher"
|
2016-09-16 12:15:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
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)
|
|
|
|
|
2016-09-14 06:42:30 +00:00
|
|
|
res, err := gopher.Get("gopher://localhost:7000/1hello")
|
2016-09-16 12:15:34 +00:00
|
|
|
assert.Nil(err)
|
2016-09-14 06:42:30 +00:00
|
|
|
|
|
|
|
b, err := res.Dir.ToText()
|
2016-09-16 12:15:34 +00:00
|
|
|
assert.Nil(err)
|
2016-09-14 06:42:30 +00:00
|
|
|
|
|
|
|
t.Logf("res: %s", string(b))
|
|
|
|
|
2016-09-16 12:15:34 +00:00
|
|
|
assert.Len(res.Dir, 1)
|
2016-09-14 06:42:30 +00:00
|
|
|
|
2016-09-16 12:15:34 +00:00
|
|
|
assert.Equal(res.Dir[0].Type, gopher.INFO)
|
|
|
|
assert.Equal(res.Dir[0].Description, "Hello World!")
|
2016-09-14 06:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
gopher.HandleFunc("/hello", hello)
|
|
|
|
go func() {
|
|
|
|
log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
|
|
|
|
}()
|
|
|
|
|
2016-09-16 12:15:34 +00:00
|
|
|
os.Exit(m.Run())
|
2016-09-14 06:42:30 +00:00
|
|
|
}
|