processing_options_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestParseBase64URL(t *testing.T) {
  12. imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  13. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(imageURL))), nil)
  14. ctx, err := parsePath(context.Background(), req)
  15. if assert.Nil(t, err) {
  16. assert.Equal(t, imageURL, getImageURL(ctx))
  17. assert.Equal(t, imageTypePNG, getProcessingOptions(ctx).Format)
  18. }
  19. }
  20. func TestParseBase64URLWithoutExtension(t *testing.T) {
  21. imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  22. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/%s", base64.RawURLEncoding.EncodeToString([]byte(imageURL))), nil)
  23. ctx, err := parsePath(context.Background(), req)
  24. if assert.Nil(t, err) {
  25. assert.Equal(t, imageURL, getImageURL(ctx))
  26. assert.Equal(t, imageTypeJPEG, getProcessingOptions(ctx).Format)
  27. }
  28. }
  29. func TestParseBase64URLWithBase(t *testing.T) {
  30. oldConf := conf
  31. defer func() { conf = oldConf }()
  32. conf.BaseURL = "http://images.dev/"
  33. imageURL := "lorem/ipsum.jpg?param=value"
  34. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(imageURL))), nil)
  35. ctx, err := parsePath(context.Background(), req)
  36. if assert.Nil(t, err) {
  37. assert.Equal(t, fmt.Sprintf("%s%s", conf.BaseURL, imageURL), getImageURL(ctx))
  38. assert.Equal(t, imageTypePNG, getProcessingOptions(ctx).Format)
  39. }
  40. }
  41. func TestParseBase64URLInvalid(t *testing.T) {
  42. imageURL := "lorem/ipsum.jpg?param=value"
  43. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/%s.png", base64.RawURLEncoding.EncodeToString([]byte(imageURL))), nil)
  44. _, err := parsePath(context.Background(), req)
  45. assert.Equal(t, errInvalidImageURL, err)
  46. }
  47. func TestParsePlainURL(t *testing.T) {
  48. imageURL := "http://images.dev/lorem/ipsum.jpg"
  49. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", imageURL), nil)
  50. ctx, err := parsePath(context.Background(), req)
  51. if assert.Nil(t, err) {
  52. assert.Equal(t, imageURL, getImageURL(ctx))
  53. assert.Equal(t, imageTypePNG, getProcessingOptions(ctx).Format)
  54. }
  55. }
  56. func TestParsePlainURLWithoutExtension(t *testing.T) {
  57. imageURL := "http://images.dev/lorem/ipsum.jpg"
  58. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s", imageURL), nil)
  59. ctx, err := parsePath(context.Background(), req)
  60. if assert.Nil(t, err) {
  61. assert.Equal(t, imageURL, getImageURL(ctx))
  62. assert.Equal(t, imageTypeJPEG, getProcessingOptions(ctx).Format)
  63. }
  64. }
  65. func TestParsePlainURLEscaped(t *testing.T) {
  66. imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"
  67. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", url.PathEscape(imageURL)), nil)
  68. ctx, err := parsePath(context.Background(), req)
  69. if assert.Nil(t, err) {
  70. assert.Equal(t, imageURL, getImageURL(ctx))
  71. assert.Equal(t, imageTypePNG, getProcessingOptions(ctx).Format)
  72. }
  73. }
  74. func TestParsePlainURLWithBase(t *testing.T) {
  75. oldConf := conf
  76. defer func() { conf = oldConf }()
  77. conf.BaseURL = "http://images.dev/"
  78. imageURL := "lorem/ipsum.jpg"
  79. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", imageURL), nil)
  80. ctx, err := parsePath(context.Background(), req)
  81. if assert.Nil(t, err) {
  82. assert.Equal(t, fmt.Sprintf("%s%s", conf.BaseURL, imageURL), getImageURL(ctx))
  83. assert.Equal(t, imageTypePNG, getProcessingOptions(ctx).Format)
  84. }
  85. }
  86. func TestParsePlainURLEscapedWithBase(t *testing.T) {
  87. oldConf := conf
  88. defer func() { conf = oldConf }()
  89. conf.BaseURL = "http://images.dev/"
  90. imageURL := "lorem/ipsum.jpg?param=value"
  91. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", url.PathEscape(imageURL)), nil)
  92. ctx, err := parsePath(context.Background(), req)
  93. if assert.Nil(t, err) {
  94. assert.Equal(t, fmt.Sprintf("%s%s", conf.BaseURL, imageURL), getImageURL(ctx))
  95. assert.Equal(t, imageTypePNG, getProcessingOptions(ctx).Format)
  96. }
  97. }
  98. func TestParsePlainURLInvalid(t *testing.T) {
  99. imageURL := "lorem/ipsum.jpg?param=value"
  100. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", imageURL), nil)
  101. _, err := parsePath(context.Background(), req)
  102. assert.Equal(t, errInvalidImageURL, err)
  103. }
  104. func TestParsePlainURLEscapedInvalid(t *testing.T) {
  105. imageURL := "lorem/ipsum.jpg?param=value"
  106. req, _ := http.NewRequest("GET", fmt.Sprintf("http://example.com/unsafe/size:100:100/plain/%s@png", url.PathEscape(imageURL)), nil)
  107. _, err := parsePath(context.Background(), req)
  108. assert.Equal(t, errInvalidImageURL, err)
  109. }