4
0

handler_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader // import "miniflux.app/v2/internal/googlereader"
  4. import (
  5. "errors"
  6. "maps"
  7. "net/http"
  8. "net/http/httptest"
  9. "net/url"
  10. "strings"
  11. "testing"
  12. )
  13. func TestCheckAndSimplifyTags(t *testing.T) {
  14. read := Stream{Type: ReadStream}
  15. keptUnread := Stream{Type: KeptUnreadStream}
  16. starred := Stream{Type: StarredStream}
  17. like := Stream{Type: LikeStream}
  18. broadcast := Stream{Type: BroadcastStream}
  19. scenarios := []struct {
  20. name string
  21. add []Stream
  22. remove []Stream
  23. expected map[StreamType]bool
  24. }{
  25. {"add read", []Stream{read}, nil, map[StreamType]bool{ReadStream: true}},
  26. {"remove read", nil, []Stream{read}, map[StreamType]bool{ReadStream: false}},
  27. {"add kept-unread", []Stream{keptUnread}, nil, map[StreamType]bool{ReadStream: false}},
  28. {"remove kept-unread", nil, []Stream{keptUnread}, map[StreamType]bool{ReadStream: true}},
  29. {"add starred", []Stream{starred}, nil, map[StreamType]bool{StarredStream: true}},
  30. {"remove starred", nil, []Stream{starred}, map[StreamType]bool{StarredStream: false}},
  31. {"add read twice", []Stream{read, read}, nil, map[StreamType]bool{ReadStream: true}},
  32. {"add read and starred", []Stream{read, starred}, nil, map[StreamType]bool{ReadStream: true, StarredStream: true}},
  33. {"add read and remove starred", []Stream{read}, []Stream{starred}, map[StreamType]bool{ReadStream: true, StarredStream: false}},
  34. {"like and broadcast are ignored", []Stream{like, broadcast}, []Stream{like, broadcast}, map[StreamType]bool{}},
  35. }
  36. for _, scenario := range scenarios {
  37. t.Run(scenario.name, func(t *testing.T) {
  38. result, err := checkAndSimplifyTags(scenario.add, scenario.remove)
  39. if err != nil {
  40. t.Fatalf(`Unexpected error: %v`, err)
  41. }
  42. if !maps.Equal(result, scenario.expected) {
  43. t.Errorf(`Unexpected tags, got %v instead of %v`, result, scenario.expected)
  44. }
  45. })
  46. }
  47. }
  48. func TestCheckAndSimplifyTagsRejectsConflicts(t *testing.T) {
  49. read := Stream{Type: ReadStream}
  50. keptUnread := Stream{Type: KeptUnreadStream}
  51. starred := Stream{Type: StarredStream}
  52. scenarios := []struct {
  53. name string
  54. add []Stream
  55. remove []Stream
  56. }{
  57. {"add and remove read", []Stream{read}, []Stream{read}},
  58. {"add read then kept-unread", []Stream{read, keptUnread}, nil},
  59. {"add kept-unread then read", []Stream{keptUnread, read}, nil},
  60. {"add and remove kept-unread", []Stream{keptUnread}, []Stream{keptUnread}},
  61. {"add and remove starred", []Stream{starred}, []Stream{starred}},
  62. }
  63. for _, scenario := range scenarios {
  64. t.Run(scenario.name, func(t *testing.T) {
  65. if result, err := checkAndSimplifyTags(scenario.add, scenario.remove); err == nil {
  66. t.Errorf(`Expected an error, got %v`, result)
  67. }
  68. })
  69. }
  70. }
  71. func TestCheckAndSimplifyTagsRejectsUnsupportedTags(t *testing.T) {
  72. for _, stream := range []Stream{
  73. {Type: ReadingListStream},
  74. {Type: BroadcastFriendsStream},
  75. {Type: LabelStream, ID: "Tech"},
  76. {Type: FeedStream, ID: "1"},
  77. {Type: NoStream},
  78. } {
  79. if _, err := checkAndSimplifyTags([]Stream{stream}, nil); err == nil {
  80. t.Errorf(`Expected an error when adding %v`, stream)
  81. }
  82. if _, err := checkAndSimplifyTags(nil, []Stream{stream}); err == nil {
  83. t.Errorf(`Expected an error when removing %v`, stream)
  84. }
  85. }
  86. }
  87. func TestCheckAndSimplifyTagsReturnsSimultaneousError(t *testing.T) {
  88. _, err := checkAndSimplifyTags([]Stream{{Type: ReadStream}, {Type: KeptUnreadStream}}, nil)
  89. if !errors.Is(err, errSimultaneously) {
  90. t.Errorf(`Expected errSimultaneously, got %v`, err)
  91. }
  92. }
  93. func TestCheckOutputFormat(t *testing.T) {
  94. newPostRequest := func(target string, form url.Values) *http.Request {
  95. r := httptest.NewRequest(http.MethodPost, target, strings.NewReader(form.Encode()))
  96. r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  97. return r
  98. }
  99. scenarios := []struct {
  100. name string
  101. request *http.Request
  102. valid bool
  103. }{
  104. {"GET with output=json", httptest.NewRequest(http.MethodGet, "/?output=json", nil), true},
  105. {"GET without output", httptest.NewRequest(http.MethodGet, "/", nil), false},
  106. {"GET with output=xml", httptest.NewRequest(http.MethodGet, "/?output=xml", nil), false},
  107. {"POST with output=json in the body", newPostRequest("/", url.Values{"output": {"json"}}), true},
  108. {"POST with output=json in the query string", newPostRequest("/?output=json", url.Values{}), true},
  109. {"POST without output", newPostRequest("/", url.Values{"T": {"token"}}), false},
  110. {"POST with output=atom", newPostRequest("/", url.Values{"output": {"atom"}}), false},
  111. }
  112. for _, scenario := range scenarios {
  113. t.Run(scenario.name, func(t *testing.T) {
  114. err := checkOutputFormat(scenario.request)
  115. if scenario.valid && err != nil {
  116. t.Errorf(`Unexpected error: %v`, err)
  117. }
  118. if !scenario.valid && err == nil {
  119. t.Error(`Expected an error`)
  120. }
  121. })
  122. }
  123. }