rpc_reader_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package githttp_test
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "reflect"
  9. "testing"
  10. "github.com/AaronO/go-git-http"
  11. )
  12. func TestRpcReader(t *testing.T) {
  13. tests := []struct {
  14. rpc string
  15. file string
  16. want []githttp.Event
  17. }{
  18. {
  19. rpc: "receive-pack",
  20. file: "receive-pack.0",
  21. want: []githttp.Event{
  22. (githttp.Event)(githttp.Event{
  23. Type: (githttp.EventType)(githttp.PUSH),
  24. Commit: (string)("92eef6dcb9cc198bc3ac6010c108fa482773f116"),
  25. Dir: (string)(""),
  26. Tag: (string)(""),
  27. Last: (string)("0000000000000000000000000000000000000000"),
  28. Branch: (string)("master"),
  29. Error: (error)(nil),
  30. Request: (*http.Request)(nil),
  31. }),
  32. },
  33. },
  34. // A tag using letters only.
  35. {
  36. rpc: "receive-pack",
  37. file: "receive-pack.1",
  38. want: []githttp.Event{
  39. (githttp.Event)(githttp.Event{
  40. Type: (githttp.EventType)(githttp.TAG),
  41. Commit: (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
  42. Dir: (string)(""),
  43. Tag: (string)("sometextualtag"),
  44. Last: (string)("0000000000000000000000000000000000000000"),
  45. Branch: (string)(""),
  46. Error: (error)(nil),
  47. Request: (*http.Request)(nil),
  48. }),
  49. },
  50. },
  51. // A tag containing the string "00".
  52. {
  53. rpc: "receive-pack",
  54. file: "receive-pack.2",
  55. want: []githttp.Event{
  56. (githttp.Event)(githttp.Event{
  57. Type: (githttp.EventType)(githttp.TAG),
  58. Commit: (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
  59. Dir: (string)(""),
  60. Tag: (string)("1.000.1"),
  61. Last: (string)("0000000000000000000000000000000000000000"),
  62. Branch: (string)(""),
  63. Error: (error)(nil),
  64. Request: (*http.Request)(nil),
  65. }),
  66. },
  67. },
  68. // Multiple tags containing string "00" in one git push operation.
  69. {
  70. rpc: "receive-pack",
  71. file: "receive-pack.3",
  72. want: []githttp.Event{
  73. (githttp.Event)(githttp.Event{
  74. Type: (githttp.EventType)(githttp.TAG),
  75. Commit: (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
  76. Dir: (string)(""),
  77. Tag: (string)("1.000.2"),
  78. Last: (string)("0000000000000000000000000000000000000000"),
  79. Branch: (string)(""),
  80. Error: (error)(nil),
  81. Request: (*http.Request)(nil),
  82. }),
  83. (githttp.Event)(githttp.Event{
  84. Type: (githttp.EventType)(githttp.TAG),
  85. Commit: (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
  86. Dir: (string)(""),
  87. Tag: (string)("1.000.3"),
  88. Last: (string)("0000000000000000000000000000000000000000"),
  89. Branch: (string)(""),
  90. Error: (error)(nil),
  91. Request: (*http.Request)(nil),
  92. }),
  93. (githttp.Event)(githttp.Event{
  94. Type: (githttp.EventType)(githttp.TAG),
  95. Commit: (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
  96. Dir: (string)(""),
  97. Tag: (string)("1.000.4"),
  98. Last: (string)("0000000000000000000000000000000000000000"),
  99. Branch: (string)(""),
  100. Error: (error)(nil),
  101. Request: (*http.Request)(nil),
  102. }),
  103. },
  104. },
  105. {
  106. rpc: "upload-pack",
  107. file: "upload-pack.0",
  108. want: []githttp.Event{
  109. (githttp.Event)(githttp.Event{
  110. Type: (githttp.EventType)(githttp.FETCH),
  111. Commit: (string)("a647ec2ea40ee9ca35d32232dc28de22b1537e00"),
  112. Dir: (string)(""),
  113. Tag: (string)(""),
  114. Last: (string)(""),
  115. Branch: (string)(""),
  116. Error: (error)(nil),
  117. Request: (*http.Request)(nil),
  118. }),
  119. },
  120. },
  121. {
  122. rpc: "upload-pack",
  123. file: "upload-pack.1",
  124. want: []githttp.Event{
  125. (githttp.Event)(githttp.Event{
  126. Type: (githttp.EventType)(githttp.FETCH),
  127. Commit: (string)("92eef6dcb9cc198bc3ac6010c108fa482773f116"),
  128. Dir: (string)(""),
  129. Tag: (string)(""),
  130. Last: (string)(""),
  131. Branch: (string)(""),
  132. Error: (error)(nil),
  133. Request: (*http.Request)(nil),
  134. }),
  135. },
  136. },
  137. }
  138. for _, tt := range tests {
  139. f, err := os.Open(filepath.Join("testdata", tt.file))
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. r := fragmentedReader{f}
  144. rr := &githttp.RpcReader{
  145. Reader: r,
  146. Rpc: tt.rpc,
  147. }
  148. _, err = io.Copy(ioutil.Discard, rr)
  149. if err != nil {
  150. t.Errorf("io.Copy: %v", err)
  151. }
  152. f.Close()
  153. if got := rr.Events; !reflect.DeepEqual(got, tt.want) {
  154. t.Errorf("test %q/%q:\n got: %#v\nwant: %#v\n", tt.rpc, tt.file, got, tt.want)
  155. }
  156. }
  157. }
  158. // fragmentedReader reads from R, with each Read call returning at most fragmentLen bytes even
  159. // if len(p) is greater than fragmentLen.
  160. // It purposefully adds a layer of inefficiency around R, and exists for testing purposes only.
  161. type fragmentedReader struct {
  162. R io.Reader // Underlying reader.
  163. }
  164. func (r fragmentedReader) Read(p []byte) (n int, err error) {
  165. const fragmentLen = 1
  166. if len(p) <= fragmentLen {
  167. return r.R.Read(p)
  168. }
  169. return r.R.Read(p[:fragmentLen])
  170. }