bullet_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package bullet
  2. import (
  3. "fmt"
  4. "net"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. )
  9. var standardError string = `{
  10. "error": {
  11. "cat": "~(=^‥^)",
  12. "message": "The resource could not be found.",
  13. "type": "invalid_request"
  14. }
  15. }`
  16. var standardErrorMessage string = "invalid_request: The resource could not be found."
  17. func fakeServer(statusCode int, data string) *httptest.Server {
  18. function := func(w http.ResponseWriter, r *http.Request) {
  19. w.Header().Set("Content-Type", "application/json")
  20. w.WriteHeader(statusCode)
  21. fmt.Fprint(w, data)
  22. }
  23. return httptest.NewUnstartedServer(http.HandlerFunc(function))
  24. }
  25. func TestSendNote(t *testing.T) {
  26. server := fakeServer(http.StatusOK, "")
  27. server.Start()
  28. defer server.Close()
  29. b := Bullet{token: "", baseURL: server.URL}
  30. err := b.SendNote("test", "test", "")
  31. if err != nil {
  32. t.Error(err)
  33. }
  34. }
  35. func TestSendNoteFail(t *testing.T) {
  36. server := fakeServer(http.StatusBadRequest, standardError)
  37. server.Start()
  38. defer server.Close()
  39. b := Bullet{token: "", baseURL: server.URL}
  40. err := b.SendNote("test", "test", "")
  41. if err == nil {
  42. t.Error("There should be error")
  43. }
  44. if err.Error() != standardErrorMessage {
  45. t.Error(err)
  46. }
  47. }
  48. func TestSendLink(t *testing.T) {
  49. server := fakeServer(http.StatusOK, "")
  50. server.Start()
  51. defer server.Close()
  52. b := Bullet{token: "", baseURL: server.URL}
  53. err := b.SendLink("test", "test", "url", "")
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. }
  58. func TestSendLinkFail(t *testing.T) {
  59. server := fakeServer(http.StatusBadRequest, standardError)
  60. server.Start()
  61. defer server.Close()
  62. b := Bullet{token: "", baseURL: server.URL}
  63. err := b.SendLink("test", "test", "url", "")
  64. if err == nil {
  65. t.Error("There should be error")
  66. }
  67. if err.Error() != standardErrorMessage {
  68. t.Error(err)
  69. }
  70. }
  71. func TestSendFile(t *testing.T) {
  72. l, err := net.Listen("tcp", "127.0.0.1:56022")
  73. if err != nil {
  74. t.Error(err)
  75. }
  76. response := `{
  77. "file_name": "cat.jpg",
  78. "file_type": "image/jpeg",
  79. "file_url": "https://dl.pushbulletusercontent.com/034f197bc6c37cac3cc03542659d458b/cat.jpg",
  80. "upload_url": "http://127.0.0.1:56022"}`
  81. server := fakeServer(http.StatusOK, response)
  82. server.Listener.Close()
  83. server.Listener = l
  84. server.Start()
  85. defer server.Close()
  86. b := Bullet{token: "", baseURL: server.URL}
  87. err = b.SendFile("test", "test", "./README.md", "")
  88. if err != nil {
  89. t.Error(err)
  90. }
  91. }
  92. func TestSendFileFail(t *testing.T) {
  93. server := fakeServer(http.StatusBadRequest, standardError)
  94. server.Start()
  95. defer server.Close()
  96. b := Bullet{token: "", baseURL: server.URL}
  97. err := b.SendFile("test", "test", "./README.md", "")
  98. if err == nil {
  99. t.Error("There should be error")
  100. }
  101. if err.Error() != standardErrorMessage {
  102. t.Error(err)
  103. }
  104. }
  105. func TestListDevices(t *testing.T) {
  106. devicesJSON := `{
  107. "devices": [
  108. {
  109. "active": true,
  110. "app_version": 8623,
  111. "created": 1412047948.579029,
  112. "iden": "ujpah72o0sjAoRtnM0jc",
  113. "manufacturer": "Apple",
  114. "model": "iPhone 5s (GSM)",
  115. "modified": 1412047948.579031,
  116. "nickname": "Elon Musk's iPhone",
  117. "push_token": "production:f73be0ee7877c8c7fa69b1468cde764f"
  118. }
  119. ]
  120. }`
  121. server := fakeServer(http.StatusOK, devicesJSON)
  122. server.Start()
  123. defer server.Close()
  124. b := Bullet{token: "", baseURL: server.URL}
  125. devices, err := b.ListDevices()
  126. if err != nil {
  127. t.Error(err)
  128. }
  129. if devices == nil {
  130. t.Fatal("Devices shouldn't be nil!")
  131. }
  132. expectedID := "ujpah72o0sjAoRtnM0jc"
  133. deviceID := devices.Items[0].ID
  134. if deviceID != expectedID {
  135. t.Errorf("Device ID should be %s, but is %s", expectedID, deviceID)
  136. }
  137. }