endpoint_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package push_test
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "time"
  7. "github.com/gnacho/netpulse/server-go/internal/push"
  8. )
  9. func TestValidateEndpoint(t *testing.T) {
  10. valid := []string{
  11. "https://push.example/abc",
  12. "https://fcm.googleapis.com/wp/abc",
  13. "https://8.8.8.8/x",
  14. "https://[2001:4860:4860::8888]/x",
  15. "https://push.example:8443/x",
  16. }
  17. for _, ep := range valid {
  18. if err := push.ValidateEndpoint(ep); err != nil {
  19. t.Errorf("ValidateEndpoint(%q) = %v, want nil", ep, err)
  20. }
  21. }
  22. invalid := []string{
  23. "",
  24. "http://push.example/x",
  25. "ftp://push.example/x",
  26. "https:///x",
  27. "https://user:pass@push.example/x",
  28. "https://localhost/x",
  29. "https://foo.localhost/x",
  30. "https://127.0.0.1/x",
  31. "https://[::1]/x",
  32. "https://10.0.0.1/x",
  33. "https://172.16.0.1/x",
  34. "https://192.168.1.1/x",
  35. "https://169.254.169.254/latest/meta-data",
  36. "https://0.0.0.0/x",
  37. "https://100.64.0.1/x",
  38. "https://198.18.0.1/x",
  39. "https://192.0.2.1/x",
  40. "https://198.51.100.1/x",
  41. "https://203.0.113.1/x",
  42. "https://240.0.0.1/x",
  43. "https://[::ffff:127.0.0.1]/x",
  44. "https://[fc00::1]/x",
  45. "https://[fe80::1]/x",
  46. "https://[2001:db8::1]/x",
  47. "https://[64:ff9b::7f00:1]/x",
  48. "https://push.example:99999/x",
  49. }
  50. for _, ep := range invalid {
  51. if err := push.ValidateEndpoint(ep); err == nil {
  52. t.Errorf("ValidateEndpoint(%q) = nil, want error", ep)
  53. }
  54. }
  55. }
  56. func TestNotifierSkipsPrivateEndpoint(t *testing.T) {
  57. d, _ := openDB(t)
  58. ua := newBrowserSub(t)
  59. hit := make(chan struct{}, 1)
  60. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  61. hit <- struct{}{}
  62. w.WriteHeader(http.StatusCreated)
  63. }))
  64. defer srv.Close()
  65. if _, err := push.UpsertSubscription(d, push.Subscription{
  66. Endpoint: srv.URL, Auth: ua.authB, P256dh: ua.pubB,
  67. }); err != nil {
  68. t.Fatalf("upsert: %v", err)
  69. }
  70. n := newNotifier(t, d)
  71. n.Notify(testEvent())
  72. select {
  73. case <-hit:
  74. t.Fatal("el notifier alcanzo un endpoint http de loopback")
  75. case <-time.After(500 * time.Millisecond):
  76. }
  77. }