external_subscription_test.go 1.2 KB

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