server_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. */
  6. package main
  7. import (
  8. "bytes"
  9. "context"
  10. "fmt"
  11. "net/http"
  12. "os"
  13. "strings"
  14. "testing"
  15. )
  16. var srv *Server
  17. func TestMain(m *testing.M) {
  18. ctx := context.Background()
  19. srv = NewServer(ctx)
  20. ret := m.Run()
  21. os.Exit(ret)
  22. }
  23. func TestGoProxyProtocol(t *testing.T) {
  24. t.Run(
  25. "Returns expected JSON on request @v/$version.info for existing module",
  26. func(t *testing.T) {
  27. expected :=
  28. `{"version": "v2.0.0", ` +
  29. `"timestamp": "2023-03-09T01:01:35Z"}` + "\n"
  30. module := "idio.link/go/netaddr/v2"
  31. version := "v2.0.0"
  32. req, _ := http.NewRequest(
  33. "GET",
  34. fmt.Sprintf("https://%s/@v/%s.info", module, version),
  35. nil,
  36. )
  37. w := NewFakeWriter()
  38. srv.handleGoGet()(w, req)
  39. actual := w.buf.String()
  40. if actual != expected {
  41. t.Errorf("\nexpected:\n%#v\n\nactual:\n%#v", expected, actual)
  42. }
  43. },
  44. )
  45. t.Run(
  46. "Receive expected HTML on request go-get for existing module",
  47. func(t *testing.T) {
  48. expected := `<!doctype html>` +
  49. `<html>` +
  50. `<head>` +
  51. `<meta name="go-import"` +
  52. ` content="idio.link/go/netaddr git https://git.idiolink.net/idio.link/netaddr.git">` +
  53. `<meta name="go-source"` +
  54. ` content="idio.link/go/netaddr` +
  55. ` https://git.idiolink.net/idio.link/netaddr` +
  56. ` https://git.idiolink.net/idio.link/netaddr/src/master{/dir}` +
  57. ` https://git.idiolink.net/idio.link/netaddr/src/master{/dir}/{file}#L{line}` +
  58. `">` +
  59. `</head>` +
  60. `<body>` +
  61. `go get https://idio.link/go/netaddr` +
  62. `</body>` +
  63. `</html>`
  64. module := "idio.link/go/netaddr"
  65. reqURI := fmt.Sprintf("https://%s/v2?go-get=1", module)
  66. req, _ := http.NewRequest("GET", reqURI, nil)
  67. w := NewFakeWriter()
  68. srv.handleGoGet()(w, req)
  69. actual := w.buf.String()
  70. if strings.Compare(actual, expected) != 0 {
  71. t.Errorf("expected:\n%s\nactual:\n%s", expected, actual)
  72. }
  73. },
  74. )
  75. t.Run(
  76. "Receive another expected HTML on request go-get for existing module",
  77. func(t *testing.T) {
  78. expected := `<!doctype html>` +
  79. `<html>` +
  80. `<head>` +
  81. `<meta name="go-import"` +
  82. ` content="idio.link/go/netaddr git https://git.idiolink.net/idio.link/netaddr.git">` +
  83. `<meta name="go-source"` +
  84. ` content="idio.link/go/netaddr` +
  85. ` https://git.idiolink.net/idio.link/netaddr` +
  86. ` https://git.idiolink.net/idio.link/netaddr/src/master{/dir}` +
  87. ` https://git.idiolink.net/idio.link/netaddr/src/master{/dir}/{file}#L{line}` +
  88. `">` +
  89. `</head>` +
  90. `<body>` +
  91. `go get https://idio.link/go/netaddr` +
  92. `</body>` +
  93. `</html>`
  94. module := "idio.link/go/netaddr"
  95. reqURI := fmt.Sprintf("https://%s?go-get=1", module)
  96. req, _ := http.NewRequest("GET", reqURI, nil)
  97. w := NewFakeWriter()
  98. srv.handleGoGet()(w, req)
  99. actual := w.buf.String()
  100. if strings.Compare(actual, expected) != 0 {
  101. t.Errorf("expected:\n%s\nactual:\n%s", expected, actual)
  102. }
  103. },
  104. )
  105. t.Run(
  106. "Receive list of module versions on request @v/list for existing module",
  107. func(t *testing.T) {
  108. expected := `v0.0.1
  109. v1.0.0
  110. v1.1.0
  111. v1.2.0
  112. v1.2.1
  113. v1.3.0
  114. v1.4.0
  115. v1.5.0
  116. v1.6.0
  117. `
  118. module := "idio.link/go/netaddr"
  119. reqURI := fmt.Sprintf("https://%s/@v/list", module)
  120. req, _ := http.NewRequest("GET", reqURI, nil)
  121. w := NewFakeWriter()
  122. srv.handleGoGet()(w, req)
  123. actual := w.buf.String()
  124. if strings.Compare(actual, expected) != 0 {
  125. t.Errorf("expected:\n%s\nactual:\n%s", expected, actual)
  126. }
  127. },
  128. )
  129. t.Run(
  130. "Receive list of v2 module versions on request @v/list for existing module",
  131. func(t *testing.T) {
  132. expected := `v2.0.0
  133. v2.0.1
  134. v2.1.0
  135. v2.1.1
  136. `
  137. module := "idio.link/go/netaddr/v2"
  138. reqURI := fmt.Sprintf("https://%s/@v/list", module)
  139. req, _ := http.NewRequest("GET", reqURI, nil)
  140. w := NewFakeWriter()
  141. srv.handleGoGet()(w, req)
  142. actual := w.buf.String()
  143. if strings.Compare(actual, expected) != 0 {
  144. t.Errorf("expected:\n%s\nactual:\n%s", expected, actual)
  145. }
  146. },
  147. )
  148. }
  149. func NewFakeWriter() *FakeWriter {
  150. buf := make([]byte, 0, 2048)
  151. return &FakeWriter{
  152. buf: bytes.NewBuffer(buf),
  153. }
  154. }
  155. type FakeWriter struct {
  156. buf *bytes.Buffer
  157. status int
  158. }
  159. func (w *FakeWriter) Header() http.Header {
  160. h := make(map[string][]string)
  161. return h
  162. }
  163. func (w *FakeWriter) Write(b []byte) (int, error) {
  164. return w.buf.Write(b)
  165. }
  166. func (w *FakeWriter) WriteHeader(status int) {
  167. w.status = status
  168. }