http_server_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "io"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/Shopify/sarama"
  8. "github.com/Shopify/sarama/mocks"
  9. )
  10. // In normal operation, we expect one access log entry,
  11. // and one data collector entry. Let's assume both will succeed.
  12. // We should return a HTTP 200 status.
  13. func TestCollectSuccessfully(t *testing.T) {
  14. dataCollectorMock := mocks.NewSyncProducer(t, nil)
  15. dataCollectorMock.ExpectSendMessageAndSucceed()
  16. accessLogProducerMock := mocks.NewAsyncProducer(t, nil)
  17. accessLogProducerMock.ExpectInputAndSucceed()
  18. // Now, use dependency injection to use the mocks.
  19. s := &Server{
  20. DataCollector: dataCollectorMock,
  21. AccessLogProducer: accessLogProducerMock,
  22. }
  23. // The Server's Close call is important; it will call Close on
  24. // the two mock producers, which will then validate whether all
  25. // expectations are resolved.
  26. defer safeClose(t, s)
  27. req, err := http.NewRequest("GET", "http://example.com/?data", nil)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. res := httptest.NewRecorder()
  32. s.Handler().ServeHTTP(res, req)
  33. if res.Code != 200 {
  34. t.Errorf("Expected HTTP status 200, found %d", res.Code)
  35. }
  36. if string(res.Body.Bytes()) != "Your data is stored with unique identifier important/0/1" {
  37. t.Error("Unexpected response body", res.Body)
  38. }
  39. }
  40. // Now, let's see if we handle the case of not being able to produce
  41. // to the data collector properly. In this case we should return a 500 status.
  42. func TestCollectionFailure(t *testing.T) {
  43. dataCollectorMock := mocks.NewSyncProducer(t, nil)
  44. dataCollectorMock.ExpectSendMessageAndFail(sarama.ErrRequestTimedOut)
  45. accessLogProducerMock := mocks.NewAsyncProducer(t, nil)
  46. accessLogProducerMock.ExpectInputAndSucceed()
  47. s := &Server{
  48. DataCollector: dataCollectorMock,
  49. AccessLogProducer: accessLogProducerMock,
  50. }
  51. defer safeClose(t, s)
  52. req, err := http.NewRequest("GET", "http://example.com/?data", nil)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. res := httptest.NewRecorder()
  57. s.Handler().ServeHTTP(res, req)
  58. if res.Code != 500 {
  59. t.Errorf("Expected HTTP status 500, found %d", res.Code)
  60. }
  61. }
  62. // We don't expect any data collector calls because the path is wrong,
  63. // so we are not setting any expectations on the dataCollectorMock. It
  64. // will still generate an access log entry though.
  65. func TestWrongPath(t *testing.T) {
  66. dataCollectorMock := mocks.NewSyncProducer(t, nil)
  67. accessLogProducerMock := mocks.NewAsyncProducer(t, nil)
  68. accessLogProducerMock.ExpectInputAndSucceed()
  69. s := &Server{
  70. DataCollector: dataCollectorMock,
  71. AccessLogProducer: accessLogProducerMock,
  72. }
  73. defer safeClose(t, s)
  74. req, err := http.NewRequest("GET", "http://example.com/wrong?data", nil)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. res := httptest.NewRecorder()
  79. s.Handler().ServeHTTP(res, req)
  80. if res.Code != 404 {
  81. t.Errorf("Expected HTTP status 404, found %d", res.Code)
  82. }
  83. }
  84. func safeClose(t *testing.T, o io.Closer) {
  85. if err := o.Close(); err != nil {
  86. t.Error(err)
  87. }
  88. }