bullet_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package bullet
  2. import (
  3. "fmt"
  4. "net"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. )
  9. func fakeServer(statusCode int, data string) *httptest.Server {
  10. function := func(w http.ResponseWriter, r *http.Request) {
  11. w.Header().Set("Content-Type", "application/json")
  12. w.WriteHeader(statusCode)
  13. fmt.Fprint(w, data)
  14. }
  15. return httptest.NewUnstartedServer(http.HandlerFunc(function))
  16. }
  17. func TestSendNote(t *testing.T) {
  18. server := fakeServer(http.StatusOK, "")
  19. server.Start()
  20. defer server.Close()
  21. b := Bullet{token: "", baseURL: server.URL}
  22. err := b.SendNote("test", "test")
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. }
  27. func TestSendLink(t *testing.T) {
  28. server := fakeServer(http.StatusOK, "")
  29. server.Start()
  30. defer server.Close()
  31. b := Bullet{token: "", baseURL: server.URL}
  32. err := b.SendLink("test", "test", "url")
  33. if err != nil {
  34. t.Error(err)
  35. }
  36. }
  37. func TestSendFile(t *testing.T) {
  38. l, err := net.Listen("tcp", "127.0.0.1:56022")
  39. if err != nil {
  40. t.Error(err)
  41. }
  42. response := `{
  43. "file_name": "cat.jpg",
  44. "file_type": "image/jpeg",
  45. "file_url": "https://dl.pushbulletusercontent.com/034f197bc6c37cac3cc03542659d458b/cat.jpg",
  46. "upload_url": "http://127.0.0.1:56022"}`
  47. server := fakeServer(http.StatusOK, response)
  48. server.Listener.Close()
  49. server.Listener = l
  50. server.Start()
  51. defer server.Close()
  52. b := Bullet{token: "", baseURL: server.URL}
  53. err = b.SendFile("test", "test", "./README.md")
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. }