[MIRROR: https://git.mills.io/prologic/go-gopher] Gopher (RFC 1436) protocol library for the Go (Golang) programming language supporting both client and server
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

178 行
3.8 KiB

  1. package gopher_test
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "testing"
  7. "time"
  8. "git.mills.io/prologic/go-gopher"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestMain(m *testing.M) {
  12. ch := startTestServer()
  13. defer stopTestServer(ch)
  14. // Because it can take some time for the server to spin up
  15. // the tests are inconsistent - they'll fail if the server isn't
  16. // ready, but pass otherwise. This problem seems more pronounced
  17. // when running via the makefile.
  18. //
  19. // It seems like there should be a better way to do this
  20. for attempts := 3; attempts > 0; attempts-- {
  21. _, err := gopher.Get("gopher://localhost:7000")
  22. if err == nil {
  23. fmt.Println("Server ready")
  24. break
  25. }
  26. fmt.Printf("Server not ready, going to try again in a sec. %v", err)
  27. time.Sleep(1 * time.Second)
  28. }
  29. /////
  30. code := m.Run()
  31. os.Exit(code)
  32. }
  33. func hello(w gopher.ResponseWriter, r *gopher.Request) {
  34. w.WriteInfo("Hello World!")
  35. }
  36. func startTestServer() chan bool {
  37. quit := make(chan bool)
  38. go func() {
  39. for {
  40. select {
  41. case <-quit:
  42. return
  43. default:
  44. gopher.Handle("/", gopher.FileServer(gopher.Dir("./testdata")))
  45. gopher.HandleFunc("/hello", hello)
  46. log.Println("Test server starting on 7000")
  47. err := gopher.ListenAndServe("localhost:7000", nil)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. }
  52. }
  53. }()
  54. return quit
  55. }
  56. func stopTestServer(c chan bool) {
  57. c <- true
  58. }
  59. func Example_client() {
  60. res, err := gopher.Get("gopher://gopher.floodgap.com")
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. fmt.Print(res.Dir.ToText())
  65. }
  66. func Example_server() {
  67. gopher.HandleFunc("/hello", hello)
  68. log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
  69. }
  70. func Example_fileserver() {
  71. gopher.Handle("/", gopher.FileServer(gopher.Dir("/tmp")))
  72. log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
  73. }
  74. func TestGet(t *testing.T) {
  75. assert := assert.New(t)
  76. res, err := gopher.Get("gopher://localhost:7000/1hello")
  77. assert.NoError(err)
  78. assert.Len(res.Dir.Items, 1)
  79. assert.Equal(res.Dir.Items[0].Type, gopher.INFO)
  80. assert.Equal(res.Dir.Items[0].Description, "Hello World!")
  81. out, err := res.Dir.ToText()
  82. assert.NoError(err)
  83. assert.Equal(string(out), "iHello World!\t\terror.host\t1\r\n")
  84. }
  85. func TestFileServer(t *testing.T) {
  86. assert := assert.New(t)
  87. res, err := gopher.Get("gopher://localhost:7000/")
  88. assert.NoError(err)
  89. assert.Len(res.Dir.Items, 1)
  90. json, err := res.Dir.ToJSON()
  91. assert.Nil(err)
  92. log.Println(string(json))
  93. assert.JSONEq(
  94. `{"items":[{"type":"0","description":"hello.txt","selector":"/hello.txt","host":"127.0.0.1","port":7000,"extras":[]}]}`,
  95. string(json))
  96. }
  97. func TestParseItemNull(t *testing.T) {
  98. assert := assert.New(t)
  99. item, err := gopher.ParseItem("")
  100. assert.Nil(item)
  101. assert.Error(err)
  102. }
  103. func TestParseItem(t *testing.T) {
  104. assert := assert.New(t)
  105. item, err := gopher.ParseItem("0foo\t/foo\tlocalhost\t70\r\n")
  106. assert.NoError(err)
  107. assert.NotNil(item)
  108. assert.Equal(item, &gopher.Item{
  109. Type: gopher.FILE,
  110. Description: "foo",
  111. Selector: "/foo",
  112. Host: "localhost",
  113. Port: 70,
  114. Extras: []string{},
  115. })
  116. }
  117. func TestParseItemMarshal(t *testing.T) {
  118. assert := assert.New(t)
  119. data := "0foo\t/foo\tlocalhost\t70\r\n"
  120. item, err := gopher.ParseItem(data)
  121. assert.NoError(err)
  122. assert.NotNil(item)
  123. assert.Equal(item, &gopher.Item{
  124. Type: gopher.FILE,
  125. Description: "foo",
  126. Selector: "/foo",
  127. Host: "localhost",
  128. Port: 70,
  129. Extras: []string{},
  130. })
  131. data1, err := item.MarshalText()
  132. assert.Nil(err)
  133. assert.Equal(data, string(data1))
  134. }
  135. func TestParseItemMarshalIdempotency(t *testing.T) {
  136. assert := assert.New(t)
  137. data := "0"
  138. item, err := gopher.ParseItem(data)
  139. assert.NoError(err)
  140. assert.NotNil(item)
  141. data1, err := item.MarshalText()
  142. assert.Nil(err)
  143. item1, err := gopher.ParseItem(string(data1))
  144. assert.NoError(err)
  145. assert.NotNil(item1)
  146. assert.Equal(item, item1)
  147. }