external_subscription_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package sub
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. )
  8. func TestDoFetchSubscriptionLinks_RejectsOversizedBody(t *testing.T) {
  9. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. _, _ = w.Write([]byte(strings.Repeat("a", subscriptionMaxBytes+1)))
  11. }))
  12. defer srv.Close()
  13. links, err := doFetchSubscriptionLinks(srv.URL)
  14. if err != errSubscriptionBodyTooLarge {
  15. t.Fatalf("err = %v, want errSubscriptionBodyTooLarge", err)
  16. }
  17. if links != nil {
  18. t.Fatalf("links = %v, want nil", links)
  19. }
  20. }
  21. func TestDoFetchSubscriptionLinks_AcceptsBodyAtLimit(t *testing.T) {
  22. link := "vless://example"
  23. body := link + "\n" + strings.Repeat("#", subscriptionMaxBytes-len(link)-1)
  24. if len(body) != subscriptionMaxBytes {
  25. t.Fatalf("fixture size = %d, want %d", len(body), subscriptionMaxBytes)
  26. }
  27. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  28. _, _ = w.Write([]byte(body))
  29. }))
  30. defer srv.Close()
  31. links, err := doFetchSubscriptionLinks(srv.URL)
  32. if err != nil {
  33. t.Fatalf("unexpected err: %v", err)
  34. }
  35. if len(links) != 1 || links[0] != link {
  36. t.Fatalf("links = %v, want [%q]", links, link)
  37. }
  38. }