gopher_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package gopher_test
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "within.website/gopher"
  9. )
  10. func hello(w gopher.ResponseWriter, r *gopher.Request) {
  11. w.WriteInfo("Hello World!")
  12. }
  13. func Example_client() {
  14. res, err := gopher.Get("gopher://gopher.floodgap.com")
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. fmt.Print(res.Dir.ToText())
  19. }
  20. func Example_server() {
  21. gopher.HandleFunc("/hello", hello)
  22. log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
  23. }
  24. func Example_fileserver() {
  25. gopher.Handle("/", gopher.FileServer(gopher.Dir("/tmp")))
  26. log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
  27. }
  28. func TestGet(t *testing.T) {
  29. assert := assert.New(t)
  30. res, err := gopher.Get("gopher://localhost:7000/1hello")
  31. assert.NoError(err)
  32. assert.Len(res.Dir.Items, 1)
  33. assert.Equal(res.Dir.Items[0].Type, gopher.INFO)
  34. assert.Equal(res.Dir.Items[0].Description, "Hello World!")
  35. out, err := res.Dir.ToText()
  36. assert.NoError(err)
  37. assert.Equal(string(out), "iHello World!\t\terror.host\t1\r\n")
  38. }
  39. func TestFileServer(t *testing.T) {
  40. assert := assert.New(t)
  41. res, err := gopher.Get("gopher://localhost:7000/")
  42. assert.NoError(err)
  43. assert.Len(res.Dir.Items, 1)
  44. json, err := res.Dir.ToJSON()
  45. assert.Nil(err)
  46. log.Println(string(json))
  47. assert.JSONEq(string(json), `{"items":[{"type":"0","description":"hello.txt","selector":"hello.txt","host":"127.0.0.1","port":7000,"extras":[]}]}`)
  48. }
  49. func TestParseItemNull(t *testing.T) {
  50. assert := assert.New(t)
  51. item, err := gopher.ParseItem("")
  52. assert.Nil(item)
  53. assert.Error(err)
  54. }
  55. func TestParseItem(t *testing.T) {
  56. assert := assert.New(t)
  57. item, err := gopher.ParseItem("0foo\t/foo\tlocalhost\t70\r\n")
  58. assert.NoError(err)
  59. assert.NotNil(item)
  60. assert.Equal(item, &gopher.Item{
  61. Type: gopher.FILE,
  62. Description: "foo",
  63. Selector: "/foo",
  64. Host: "localhost",
  65. Port: 70,
  66. Extras: []string{},
  67. })
  68. }
  69. func TestParseItemMarshal(t *testing.T) {
  70. assert := assert.New(t)
  71. data := "0foo\t/foo\tlocalhost\t70\r\n"
  72. item, err := gopher.ParseItem(data)
  73. assert.NoError(err)
  74. assert.NotNil(item)
  75. assert.Equal(item, &gopher.Item{
  76. Type: gopher.FILE,
  77. Description: "foo",
  78. Selector: "/foo",
  79. Host: "localhost",
  80. Port: 70,
  81. Extras: []string{},
  82. })
  83. data1, err := item.MarshalText()
  84. assert.Nil(err)
  85. assert.Equal(data, string(data1))
  86. }
  87. func TestParseItemMarshalIdempotency(t *testing.T) {
  88. assert := assert.New(t)
  89. data := "0"
  90. item, err := gopher.ParseItem(data)
  91. assert.NoError(err)
  92. assert.NotNil(item)
  93. data1, err := item.MarshalText()
  94. assert.Nil(err)
  95. item1, err := gopher.ParseItem(string(data1))
  96. assert.NoError(err)
  97. assert.NotNil(item1)
  98. assert.Equal(item, item1)
  99. }
  100. func TestMain(m *testing.M) {
  101. gopher.Handle("/", gopher.FileServer(gopher.Dir("./testdata")))
  102. gopher.HandleFunc("/hello", hello)
  103. go func() {
  104. log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
  105. }()
  106. os.Exit(m.Run())
  107. }